How to return COM object?
-
Hi, I tried to return an interface but it gave me this error:
Unable to cast COM object of type 'MyTestLib.MainTestClass' to interface type 'MyTestLib.IMainTest '. This operation failed because the QueryInterface call on the COM component for the interface with IID '{519A413A-2792-4021-847F-B7C205FFE057}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
Any idea why? Here is my IDL:
import "oaidl.idl"; import "ocidl.idl"; [ object, uuid(EC23249E-2891-4f06-9D6E-77895E74448F) ] interface IAdd : IUnknown { HRESULT SetFirstNumber(long nX1); HRESULT SetSecondNumber(long nX2); HRESULT DoTheAddition([out,retval] long *pBuffer); }; [ object, uuid(519A413A-2792-4021-847F-B7C205FFE057) ] interface IMainTest : IUnknown { HRESULT GetAddObject([out,retval] IAdd **ppObj); }; [ uuid(AA484B10-6099-4e37-AEE3-8B4ADFBE2815) ] library MyTestLib { importlib("stdole32.tlb"); [ uuid(82B76786-E89B-4e42-8C50-B7916FEC1ADC) ] coclass AddClass { [default] interface IAdd; } [ uuid(E76E5E58-F9D9-4570-AEC5-79A72E8E8743) ] coclass MainTestClass { [default] interface IMainTest; } };
Thanks for any help :)
-
Hi, I tried to return an interface but it gave me this error:
Unable to cast COM object of type 'MyTestLib.MainTestClass' to interface type 'MyTestLib.IMainTest '. This operation failed because the QueryInterface call on the COM component for the interface with IID '{519A413A-2792-4021-847F-B7C205FFE057}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
Any idea why? Here is my IDL:
import "oaidl.idl"; import "ocidl.idl"; [ object, uuid(EC23249E-2891-4f06-9D6E-77895E74448F) ] interface IAdd : IUnknown { HRESULT SetFirstNumber(long nX1); HRESULT SetSecondNumber(long nX2); HRESULT DoTheAddition([out,retval] long *pBuffer); }; [ object, uuid(519A413A-2792-4021-847F-B7C205FFE057) ] interface IMainTest : IUnknown { HRESULT GetAddObject([out,retval] IAdd **ppObj); }; [ uuid(AA484B10-6099-4e37-AEE3-8B4ADFBE2815) ] library MyTestLib { importlib("stdole32.tlb"); [ uuid(82B76786-E89B-4e42-8C50-B7916FEC1ADC) ] coclass AddClass { [default] interface IAdd; } [ uuid(E76E5E58-F9D9-4570-AEC5-79A72E8E8743) ] coclass MainTestClass { [default] interface IMainTest; } };
Thanks for any help :)
I found the problem. Apparently I am trying to be really smart about this by creating only 1 Factory and put a check in CreateInstance() to determine whether it is AddClass or MainTestClass. I was wrong because the IID check should be in DllGetClassObject(). I have created 2 different factories for these classes and it works fine. I can now return the AddClass by using GetAddObject() function. However, when I tried to use the function in AddClass (eg. SetFirstNumber()), it gave me this error:
The runtime has encountered a fatal error. The address of the error was at 0x7f628678, on thread 0x105c. The error code is 0xc0000005. This error may be a bug in the CLR or in the unsafe or non-verifiable portions of user code. Common sources of this bug include user marshaling errors for COM-interop or PInvoke, which may corrupt the stack.
Here is my code:
HRESULT __stdcall CMainTestClass::GetSamTest(IAdd **ppRetVal) { CAddClass *pAddClass = new CAddClass(); *ppRetVal = pAddClass; return S_OK; }
Thanks again for any help :)