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)...