A strange issue about custom interface
-
DLL-based Component CA is developed with ATL Object wizard. Its features including: 1. single-threaded 2. custom interface 3. no aggregation support CA has only one interface named IA which has a method GetData: STDMETHOD(Receive)(/*[out]*/ long* pDataLength, /*[out, size_is(*pDataLength)]*/ unsigned char** ppData); A confusing fact is that: When I use CA in a normal MFC EXE, it works well. Then I use CA in an ATL-based EXE. In that EXE I have another Componet say CB. CB creates a thread say WorkingThread(). In WorkingThread(), I call CreateInsantance() to create CA: IA* pIA = NULL; RESULT hr = CoCreateInstance(CLSID_A, NULL, CLSCTX_INPROC_SERVER, IID_IA, (void**)&pIA); It failed with a HRESULT indicating "The interface IA is not supported by CLSID_A". I have included A.h and two const definition of CLSID_A and IID_A in the ATL EXE's source, the same as I did in the normal MFC EXE. If CA is a dual interface, no such troubles. So I assume this is related to custom interface. But how to solve this problem?
-
DLL-based Component CA is developed with ATL Object wizard. Its features including: 1. single-threaded 2. custom interface 3. no aggregation support CA has only one interface named IA which has a method GetData: STDMETHOD(Receive)(/*[out]*/ long* pDataLength, /*[out, size_is(*pDataLength)]*/ unsigned char** ppData); A confusing fact is that: When I use CA in a normal MFC EXE, it works well. Then I use CA in an ATL-based EXE. In that EXE I have another Componet say CB. CB creates a thread say WorkingThread(). In WorkingThread(), I call CreateInsantance() to create CA: IA* pIA = NULL; RESULT hr = CoCreateInstance(CLSID_A, NULL, CLSCTX_INPROC_SERVER, IID_IA, (void**)&pIA); It failed with a HRESULT indicating "The interface IA is not supported by CLSID_A". I have included A.h and two const definition of CLSID_A and IID_A in the ATL EXE's source, the same as I did in the normal MFC EXE. If CA is a dual interface, no such troubles. So I assume this is related to custom interface. But how to solve this problem?
You need to make sure that you call CoInitailize for each thread that will create COM objects. Basically this function inidicates to OLE which type of threading model that thread will use when it creates COM objects.
Build a man a fire, and he will be warm for a day
Light a man on fire, and he will be warm for the rest of his life!