B
Bob S
A class in assembly A implements a COM interface as follows :
[ComImport, Guid("CEF04FDF-FE72-11d2-87A5-00C04F6837CF"),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
internal interface IMyInterface
{
[PreserveSig]
int MyMethod();
}
class MyClass : IMyInterface
{
int MyMethod()
{
....
}
}
This interface is exposed to the unmanaged world via COM Interop.
Now, assembly B also defines IMyInterface and gets this interface from the
unmanaged world as follows :
IntPtr unk = GetMyInterface(); // Gets IMyInterface from unmanaged world
object obj = Marshal.GetObjectFromIUnkown(unk); // its really the class
MyClass from assembly A above
IMyInterface myIntf = obj as IMyInterface; // this cast does not work and
returns null becuase of separate declarations of IMyInterface in the two
assemblies !
How to get this scenario to work? One would think that .Net would recognize
that both the interfaces are the same ( based on the ComImport and Guid
attributes) and allow the cast, but it doesnt allow the cast.
It is not possible for me to use a shared assembly and put the declaration
of IMyInterface in that assembly.
So how can I solve this problem?
Thanks
Bob
[ComImport, Guid("CEF04FDF-FE72-11d2-87A5-00C04F6837CF"),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
internal interface IMyInterface
{
[PreserveSig]
int MyMethod();
}
class MyClass : IMyInterface
{
int MyMethod()
{
....
}
}
This interface is exposed to the unmanaged world via COM Interop.
Now, assembly B also defines IMyInterface and gets this interface from the
unmanaged world as follows :
IntPtr unk = GetMyInterface(); // Gets IMyInterface from unmanaged world
object obj = Marshal.GetObjectFromIUnkown(unk); // its really the class
MyClass from assembly A above
IMyInterface myIntf = obj as IMyInterface; // this cast does not work and
returns null becuase of separate declarations of IMyInterface in the two
assemblies !
How to get this scenario to work? One would think that .Net would recognize
that both the interfaces are the same ( based on the ComImport and Guid
attributes) and allow the cast, but it doesnt allow the cast.
It is not possible for me to use a shared assembly and put the declaration
of IMyInterface in that assembly.
So how can I solve this problem?
Thanks
Bob