Disabling COM events from another topic

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);
  // Succeeds with S_OK
  FireEvent(L"Hello, world!");
  return S_OK;
}

DWORD WINAPI ThreadProc(LPVOID param)
{
  CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
  MyObject* comObject = reinterpret_cast<MyObject*>(param);
  // Fails with 0x8000FFFF
  comObject->FireEvent(L"Hello, world!");
}

void MyObject::FireEvent(BSTR str)
{
  ...
  // Returns 0x8000FFFF if called from ThreadProc
  // Returns S_OK if called from SomeMethod
  pConnection->Invoke(dispid, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_METHOD, &params, NULL, NULL, NULL);
}
+3
2

COM

STA (The Thread). , , , . STA , ( Thread, ).

, . , Thread. "" IUnknown CoMarshalInterThreadInterfaceInStream, CoGetInterfaceAndReleaseStream, - . , , , ( ) .

, . MTA, MTA, . , MTA STA. MTA , STA , . , .

, - "", "" , :-)) , [] ...

Btw,

MyObject* comObject = reinterpret_cast<MyObject*>(param);

MTA.

+4

, , -. , Office Object Model, , , .
, COM , CoClass .
, .

Threading COM (wikipedia):
(STA) . COM- , . STA , . ( Windows). , , .

. Message Pumping .

0

Source: https://habr.com/ru/post/1721406/


All Articles