I created an in-process COM object (DLL) using ATL. Please note that this is an object, not a control (so it does not have a window or user interface.) My problem is that I try to fire the event from the second thread and I get a "Crash" (0x8000FFFF). If I fire an event from the main thread, then I do not get an error. The second thread calls CoInitializeEx, but it does not matter. I am using the Threading Thread model, but switching to Free Threaded does not help.
What I'm trying to do from the second thread is obviously crucial. Is there an easy way to do this, or will I have to implement some hidden form of messaging?
For example, in my main object source file:
STDMETHODIMP MyObject::SomeMethod(...)
{
CreateThread(NULL, 0, ThreadProc, this, 0, NULL);
FireEvent(L"Hello, world!");
return S_OK;
}
DWORD WINAPI ThreadProc(LPVOID param)
{
CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
MyObject* comObject = reinterpret_cast<MyObject*>(param);
comObject->FireEvent(L"Hello, world!");
}
void MyObject::FireEvent(BSTR str)
{
...
pConnection->Invoke(dispid, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_METHOD, ¶ms, NULL, NULL, NULL);
}