views:

264

answers:

3

I am doing a BHO (extension for IE) that receives events on other thread. When I access the DOM from that other thread, IE crashes. Is it possible to make the DOM accessed from the same thread as the main BHO thread so that it does not crash?

It seems like a general COM multithreading problem, which I don't understand much.

+1  A: 

I don't know much about IE extensions, but it sounds like some COM object needs to be marked a Single Threaded Apartment, so that the COM runtime system ensures that it is run on the same thread which called it initially. If you can't alter the other object, you could probably route your calls to the DOM through a separate COM object marked as STA to achieve the same effect. Hope this helps... I know a bit about COM multithreading, but not much about IE extensions.

Nick
+3  A: 

Look into using CoMarshalInterface or CoMarshalInterThreadInterfaceInStream

These will give you a wrapped interface to an STA COM object that is thread safe.

Gerald
That does it! Thank you so much!So in the main thread, call CoMarshalInterThreadInterfaceInStream, pass the stream pointer to the secondary thread, and on the secondary thread call CoGetInterfaceAndReleaseStream to get a *proxy* to the object to be called.
yuku
A: 

ah, fun fun fun multithreading with COM.

Gerald's answer looks right if you want to transfer an interface pointer from one thread to another exactly once. I've found that the GIT (global interface table) is a big help for this kind of thing if you're in a multithreaded system... basically you don't keep around interface pointers but rather DWORD cookies used by the GIT to get an appropriately-marshaled interface pointer for whatever thread you are using it. (you have to register the object in question with the GIT first, and unregister it later when you are done or your object is finished)

Jason S