Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / June 2005

Tip: Looking for answers? Try searching our database.

.NET objects as argument to COM component

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Tobias Weltner [MVP] - 21 Jun 2005 12:30 GMT
Talking to COM objects from .NET is easy, but how do you pass .NET objects
to COM components as arguments?
This seems to be a pretty strange thing. This is what I did:

In VB6, I created an activeX DLL called "test.interop" with a single class
like this:

####
Sub getobject(ByVal obj As Object)
   MsgBox TypeName(obj)
   On Error Resume Next
   MsgBox obj.GetValue
   If Err.Number <> 0 Then
       MsgBox "Object does not support ""GetValue"""
   End If
   On Error GoTo 0
End Sub

Sub About()
   MsgBox "Hello"
End Sub
####

In VB.NET, I added the dll to the reference list. Next, I use a button and
this code:

####
Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim test As test.interop = New test.interop

Dim obj1 As String = "Hello"

Dim obj2 As aClass = New aClass

test.About()

test.getobject(obj1)

test.getobject(obj2)

End Sub

End Class

####

When I click the button, "About" is called and works fine. Talking to COM is
easy, isn't it? ;-)

Then, obj1 is passed to the com object "GetObject" method. Since it is a
System.String, it gets identified correctly as "String" and does not support
the "GetValue"-method expected by the com object, so a message pops up. All
fine.

Next, I add an instance of my own class "aClass" by passing it as an
argument to the com object.
This time, I get a "cast invalid exception".

For some reason, my class instance can't get converted to an "Object" type.
I assume it may have something to do with security checks since my object
could be used by the "unsafe" COM object to manipulate my .NET app.

However, I haven't found a way to define settings in a way that I can pass
the object.

If this example *should* work when you try it, please tell me. I am using VS
2005 Beta and am suspecting that some things have changed regarding com
security, but I can't tell so far. There must be a way to declare an object
as "safe" or something because I *can* use .NET objects like String or
Control or whatever as arguments to the COM dll.

Do you have any suggestions on how I can make this example work?

Thanks a lot!
Signature

__________________________________

Dr. Tobias Weltner
MVP Windows Server / Scriptautomation

Tobias Weltner [MVP] - 23 Jun 2005 08:19 GMT
In VS 2005, assembly isn't COM visible by default anymore. It just needs to
be changed, then all works fine (except for methods with arguments of type
paramArray)...

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.