0

I'm using a C++ DLL with python which makes use of COM objects. I'm loading it with cdll.LoadLibray.

My application was working fine with python 2.7. Now that I'm moving to Python 3.7 my C++ DLL fails when I call: CoInitializeEx(NULL,COINIT_MULTITHREADED) with error 0x80010106: Cannot change thread mode after it is set.

By googling a bit I found some references on sys.coinit_flags = pythoncom.COINIT_MULTITHREADED but this pieces of code does not solve the issue.

It seems Python 3 is initializing COM by itself and now I cannot change the COM concurrency model.

How can I enable COINIT_MULTITHREADED?

cabbi
  • 393
  • 2
  • 12
  • CoInitialize(Ex) is supposed to be called on a thread by the developer who owns the thread. If you didn't created that thread, you're not supposed to call CoInitialize(Ex), it's too late. That being said, in general if you do (say by "safety" measure), you can discard that specific error and continue. What happens if you do? It's possible that everything works as expected – Simon Mourier Jun 06 '19 at 08:50
  • @SimonMourier I was ignoring the CoInitilize result but my dll fails in calling COM object from different threads – cabbi Jun 06 '19 at 08:57
  • You should investigate that. The whole point of threading model in COM is to make sure objects with different behavior in multi threading scenarios can cohabit safely, with marshaling between objects, having been created by same or different threads (~apartments), even in different processes, being taken care of by COM itself. But code must be written by COM rules. If it doesn't work, it's possible you configuration or code has issues. – Simon Mourier Jun 06 '19 at 09:01
  • @SimonMourier how my code works is clear to me. I need multiple threads accessing a COM object that's why I call CoInitializeEx(NULL,COINIT_MULTITHREADED). I am asking where/why/how I can let Python 3 not to initialize COM on my behalf or initialize it to host the concurrency model I need: COINIT_MULTITHREADED – cabbi Jun 06 '19 at 09:31
  • Good for you if everything is clear to you. – Simon Mourier Jun 06 '19 at 09:57

1 Answers1

0

Ok... for whoever will have this issue, after googling and trying to understand how to get Python 3 not to initialize COM or initialize it for MTA (i.e. COINIT_MULTITHREADED) I gave up and simply put CoUninitialize() in my C++ code just before calling CoInitializeEx(NULL,COINIT_MULTITHREADED) needed by my DLL.

cabbi
  • 393
  • 2
  • 12