Question

I'm looking for a code sample which illustrates making cross-threaded COM calls in an ATL COM Server DLL. Know one?

More details:

My component is an IE Browser Helper Object which needs to do work on a background thread. In one of my object's methods called from IE's main thread, IE gives me a pointer to the IE DOM (IHtmlWindow2) and I store it in a local member variable.

Now I want to create a worker thread which can asynchronously call methods on that COM interface pointer that I've just stored.

If it matters, my calls will happen after I return from the method where I got the interface pointer from IE, so IE (and not my code) will have control when I try to make the calls from my worker thread.

Both my component and the host application are Apartment threaded.

Anyone know a good C++ sample showing how this is done?

I'm specifically looking for a short, straightforward sample (and not just a pointer to relevant documentation) because I read this question and its answers and got intimidated at the amount of learning I'd have to do to write an implementation from scratch. Any time someone recommends to read a Don Box book before writing code, you know you're in for a long, confusing night... :-)

Was it helpful?

Solution

That's automatic in COM, it takes care of apartment requirements and will marshal the call if the interface pointer has STA affinity. You have to marshal the interface pointer before using it in the thread, ATL has the AtlMarshalPtrInProc() and AtlUnmarshalPtr() helper methods to make that easier. CoMarshalInterThreadInterfaceInStream() or IGlobalInterfaceTable if you want to do it yourself.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top