Reputation: 11
I am writing a Win32 C++ DLL that uses the COM object(B.dll) which is made in C#. This dll(A.dll) provides CMyComObject class which creates a COM object and access to it. Here is my code.
void CMyComObject::CMyComObject()
{
HRESULT result = CoInitialize(NULL);
...
result = CoCreateInstance(CLSID_COMDLL, NULL, CLSCTX_INPROC_SERVER, IID_COMDLL, reinterpret_cast<void**>(&MyComObject));
}
void CMyComObject::~CMyComObject()
{
..
CoUninitialize();
..
}
And then, here is a client program that loads A.dll and access to the COM object. This program creates several threads which load A.dll and create a COM object concurrently.
In this case, Is this correct to use CoInitialize() function or Should I use CoInitializeEx() function with COINIT_MULTITHREADED parameter? Or Is there any mistake I did? (I registered B.dll by commanding "reg_asm.exe B.dll B.tlb /codebase")
Sorry for my poor English.
Thanks.
Upvotes: 0
Views: 735
Reputation: 69724
You are supposed to use CoInitialize[Ex]
/CoUninitialize
before and after any COM activity on that thread, and your choice between CoInitialize
and CoInitializeEx
with specific parameters depends on whether you prefer STA or MTA mode for your thread.
Having said that, your initialization:
Summing it all once again, your initialization:
CoInitialize
and CoUninitialize
calls out of constructor and associate it with thread codeCoUninitialize
call, including releasing your MyComObject
pointer.Upvotes: 1