R – Adding a COM Reference with Visual Studio renames interfaces in Interop Assembly

interopnettlbimpvisual studio

I'm adding a C++ COM dll as a reference to a C# project (Visual Studio 2008). VS adds the reference however the generated interop library does not reflect the naming in the original typelib (.idl) definition. Here's what my library definition looks like:

[
   uuid(...),
   helpstring("MyLib")
]
library MyLib
{    
    [
        uuid(...),
        helpstring("MyCom CoClass")
    ]
    coclass MyComCoClass
    {
        [default] interface IMyInterface;
        interface IMyInheritedInterface;
        interface IMyBaseInterface;
    };
}

So, IMyInterface inherits from IMyInheritedInterface which in turns inherits from IMyBaseInterface. I want all of these interface available when this COM's .dll is added. In the interop assembly VS generates for the typelib above, IMyInterface becomes MyInterface. Why and is there a way around it?

Thanks

Best Answer

The typelib importer does that when an interface is the default interface for a coclass and it's only used by that coclass.

So one workaround is to add another dummy coclass to your typelib and make IMyInterface the default interface for that too.

Related Topic