Groups | Blog | Home
all groups > dotnet interop > august 2003 >

dotnet interop : C# dll calling late-bound COM via InvokeMember gives type mismatch


Joshua B. Helm
8/29/2003 10:53:49 AM
I have an extremely large (mostly VB6-based) project that makes extensive
use of late-binding COM. We are attempting to migrate slowly to .NET by
converting a piece at a time. I am attempting to write a replacement for
one of our dlls that late-binds to COM objects. I'm using Type.InvokeMember
in C# to call the late-bound COM dll. The trouble I'm having is when I
attempt to pass an object as a "set" property to the VB6 dll I end up with a
"Type Mismatch" error.

The C# code looks like:

bpiCurrent.GetType().InvokeMember("StationMonitor",
BindingFlags.SetProperty,
null,
bpiCurrent,
new object[] {this}) ;


And the VB6 code for the "StationMonitor" property:

Private WithEvents smonCurrent As clsStationMonitor
..
..
Public Property Set StationMonitor(smonNew As clsStationMonitor)
Set smonCurrent = smonNew
End Property


Note that "clsStationMonitor" is the class that is being implemented in the
new C# dll ("this").

With the property definition like this in VB6, a type mismatch exception is
thrown. If I change the parameter type to "Object" is enters the VB6
property code fine, but of course the "Set" statement fails with a type
mismatch. As a test I stopped the code in the VB6 property and called a
method on the smonNew variable and it did indeed call-back to the C# dll
properly.

So, how do I marhsal the proper type here? Any help would be greatly
appreciated.

---
- Joshua B. Helm
Technology Manager
Millbrook Incorporated

Mattias Sjögren
8/31/2003 4:55:23 PM
Joshua,

[quoted text, click to view]

Is the clsStationMonitor class interface originally defined in a VB6
DLL that you have TlbImp'ed to get access to in the C# code, or do you
have duplicate definitions of the interface?

Does it make a difference if you make the smonNew parameter ByVal?



Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Joshua B. Helm
9/11/2003 4:23:33 PM
[quoted text, click to view]

That class was originally defined in a COM VB6 based dll. We are attempting
to write a replacement for this dll in C# .NET. I'd like the .NET version
to look /exactly/ the same as the COM VB6 version of the dll to existing COM
based consumers. Ideally, I'd like the C# .NET version to be a drop-in
replacement for the dll without modifying and recompiling all the older
modules that used it.

I neglected to include that I have added the following to the C# version to
attempt to make it look the same to COM:

//COM event interface
[InterfaceTypeAttribute(ComInterfaceType.InterfaceIsDual)]
public interface _clsStationMonitor_Events
{
....
}

//COM class interface
[GuidAttribute("4E6CBC6E-6FD8-4F7D-AC69-486C9DA6D974")]
[InterfaceTypeAttribute(ComInterfaceType.InterfaceIsDual)]
public interface _clsStationMonitor
{
....
}

[ClassInterface(ClassInterfaceType.None)]
[ComSourceInterfaces(typeof(_clsStationMonitor_Events))]
public class clsStationMonitor : _clsStationMonitor
{
....
}


It seems that VB6 is not recognizing the new C# dll class as the same class
as the older COM based one -- thus the type mismatch.


[quoted text, click to view]

I can't really do this because this module can not be modified (it
implements an established interface that we can't change now).

[quoted text, click to view]

Thanks

---
- Joshua B. Helm
Technology Manager
Millbrook Incorporated

AddThis Social Bookmark Button