Problem with casting same COMImport interface from 2 different assemblies

Tech-Archive recommends: Fix windows errors by optimizing your registry



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


.



Relevant Pages

  • Re: Plugins: flexible & safe use of different assembly versions with different features
    ... I'm not sure how you have created your assemblies, ... you would define an interface that B would implement. ... public class PlugIn: IMyInterface ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: C# Plugin system - same interface in two different assemblies...
    ... "add a reference to the appropriate assemblies to each and every project"? ... You don't have each plugin project deciding what ... its idea of the interface is. ... can't by forcing people to include them in assemblies as I've explained. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: C# Plugin system - same interface in two different assemblies...
    ... That's duplication, which it's good practice to avoid. ... At least by letting the developer define the interface you can accomplish things you can't by forcing people to include them in assemblies as I've explained. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Using .NET Component from COM Object
    ... Mike ... > interface that is used to sink the events into. ... > component it is effective hidden code, which is always very hard to debug. ... >>> name your assemblies if you're going to do COM. ...
    (microsoft.public.dotnet.framework.interop)
  • Re: Is this possible? Equivalent of C++ header file?
    ... it isn't so much that I was prepared to go through and make header ... > It is not difficult to create an interface. ... > definitions, but containing no method code), and compile it. ... >>> build version of your assemblies that have no real implementation. ...
    (microsoft.public.dotnet.languages.csharp)