CoInitializeEx() with COINIT_APARTMENTTHREADED or COINIT_MULTITHREADED for MS-Word automation using COM C++

  adobe, automation, c++, com, ms-word

I am trying to automate a certain scenario with MS-Word where I want to get the BeforePrint Event to get the filePath while print is initiated. The technology that is needed to get this done is COM C++. I initialize COM library by attaching to word process when it is initiated, by calling CoInitializeEx() with COINIT_APARTMENTTHREADED and things work. But when another application is launched which is doing something similar, for example, create pdf from word in Adobe Acrobat DC or any office addin which does something with BeforePrint event, the application/addin errors out with their functionality. My hunch is that something is wrong with the way COM is initialized since if I initialize COM by calling CoInitializeEx() with COINIT_MULTITHREADED everything seems to work fine.
Below are specific questions in this regards:

  1. What is different in calling CoInitializeEx() with COINIT_APARTMENTTHREADED vs COINIT_MULTITHREADED that makes it work in this scenario?
  2. With most samples online for COM C++ office automation for word, CoInitializeEx() is done with COINIT_APARTMENTTHREADED, what is the recommended way and why?
  3. Could there be issues for any other application/office addins if my application uses COINIT_MULTITHREADED for CoInitializeEx() instead of COINIT_APARTMENTTHREADED?

Links I have already referred but are not conclusive enough:
https://docs.microsoft.com/en-us/visualstudio/vsto/threading-support-in-office?view=vs-2019
https://docs.microsoft.com/en-us/windows/win32/api/objbase/ne-objbase-coinit

Source: Windows Questions C++

LEAVE A COMMENT