In this scenario, I would recommend against adding the reference through the "Add References" dialog in Visual Studio.
Instead, use the Type Library importer to generate the reference from the command line, and then set a reference to that in your project normally (it will be a .NET assembly).
Once you've done that, you'll have two sets of types, the set of interfaces that the two classes implement, as well as one of the class definitions of the implementation of those interfaces.
Instead of using the constructor to create this class, you would use the GetTypeFromCLSID
method on the Type
class to get the .NET Type
for the COM object.
Then, you would use that Type
in a call to Activator.CreateInstance
(since all COM objects have default constructors, this will work) and cast the result to the interface definition, like so:
// The CLSID of the implementation you want to use.
Guid clsid = ...;
// Get the type.
Type type = Type.GetTypeFromCLSID(clsid);
// Create and cast to your interface.
var i = (IMyComInterface) Activator.CreateInstance(type);
When Activator.CreateInstance
comes across a type that is a COM object, it creates a System.__ComObject
(the root of all COM interop instances) which the CLR has support for (like calling IUnkown::QueryInterface
when casting, like in the above code).
Also, if you are up for it, you could define the interfaces in your code, and then cast to those, you don't necessarily need the Type Library importer to generate them.