I've been battling with VStudio, Google and various other tools and websites all day and found no solution - HELP!!
I have two COM interfaces (pure COM, no ATL):
IMyClassFactory and IMyClass with corresponding implementations
I want to use them from C# BUT without registering the COM server with regsvr32. I expose the Class Factory with CoRegisterClassObject and I can successfully create objects of IMyClass with CoCreateInstance from unmanaged code.
So the C# interop...
I created a .NET wrapper with tlbimp myComServer.tlb and loaded it as a reference to my C# client.
Then, when I try to create an instance of IMyClass, I get:
An unhandled exception of type 'System.InvalidCastException' occurred in COMTestClient.exe
Additional information: Unable to cast COM object of type 'MyComServerLib.MyClass' to interface type 'MyComServerLib.IMyClass'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{9F8CBFDC-8117-4B9F-9BDC-12D2E6A92A06}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
Now, I have added traces to the QueryInterface and the only cases when I return E_NOINTERFACE are when it requests for any of Marshal-related interfaces or for IManagedObject.
How do I fix this??
EDIT: My IDL file:
import "unknwn.idl";
[
object,
uuid(...),
nonextensible,
pointer_default(unique)
]
interface IMyClass : IUnknown
{
HRESULT(SetFirstNumber)(long nX1);
HRESULT(SetSecondNumber)(long nX2);
HRESULT(DoTheAddition)([out,retval] long *pBuffer);
};
[
uuid(...)
]
library MyLib
{
importlib("stdole2.tlb");
[
uuid(...)
]
coclass IMyClassImpl
{
[default] interface IMyClass;
};
}