Re: Auto-initialize threads in STA or MTA?
The right way is to intercept that library's thread creation and
make the threads it creates enter the MTA. Alas, I doubt the
library has been written with this kind of hook in mind... The
next best thing is simply to enter the MTA when you need to
do COM work on a library thread and exit it when finished.
Make sure you have a thread of your own that keeps the MTA
open, however! This assumes the library is not COM-aware
and does not enter any COM apartments on its threads.
--
=====================================
Alexander Nickolov
Microsoft MVP [VC], MCSD
email: agnickolov@mvps.org
MVP VC FAQ: http://vcfaq.mvps.org
=====================================
"Boris" <boris@gtemail.net> wrote in message
news:op.t20varkq45fww6@burk.mshome.net...
On Sat, 08 Dec 2007 16:36:42 +0200, Igor Tandetnik <itandetnik@mvps.org>
wrote:
"Boris" <boris@gtemail.net> wrote in message
news:op.t20sdnsk45fww6@burk.mshome.net
Is there a way to indicate that a new thread spawned should be put
automatically in STA or MTA (without calling CoInitialize)?
No.
[...]If you are saying the library calls a native method on your C++
class
which also just happens to implement a COM object, then it's your
responsibility to transfer the call to a thread that has properly
initialized COM if you want to do any COM-related work. E.g. for STA
Yes, that's happening. I'm basically trying to propagate events from the
C++ library to the user of the COM API. However I don't know on what
threads the event handlers (= methods in the COM objects) are invoked.
threads, a common technique is to create a hidden window in the STA
thread and post yourself a window message from the worker thread.
Currently I'm putting everything in the MTA to make sure that I can pass
around pointers to objects in the C++ library without marshalling between
COM objects. Thus I was hoping to put the threads created by the
C++ library in MTA somehow so I can continue to go the lazy way. ;)
Boris