COM objects in threads

Mark Hammond MarkH at
Mon Feb 5 07:30:08 EST 2001

Markus von Ehr wrote:

> Anybody can help me accessing the mp object in the thread? How
> can I set the new FileName?

I have already replied to this - there is no easy answer.

Either, you must ensure _all_ threads, including the main thread, are in the free-threading apartment.  This will involve setting sys.coinit_flags=0 before importing pythoncom for the first time, and ensuring you call CoInitializeEx(0) on each thread you create.

The only other alternative is to use pythoncom.CoMarshalInterThreadInterfaceInStream and pythoncom.CoGetInterfaceAndReleaseStream to pass a COM object between 2 apartment-model threads.  The default is apartment-model threading.  COM says it is illegal to share objects between apartments, and as you have seen, often enforces it!

You are going to have to find a reasonable reference on COM threading models, or see the "threading" appendix in "Python Programming on Win32" - that appendix was hell to write, and I am not going to attempt it again on Usenet ;-)  Deja threads (no pun intended;) on the subject may also help.


More information about the Python-list mailing list