Does .net interop do cross-thread marshalling for COM or do I need to use CoMarshalInterThreadInterfaceInStream or similar?

StackOverflow https://stackoverflow.com/questions/917283

  •  06-09-2019
  •  | 
  •  

Question

Can anyone give me a definitive answer as to whether I need to use something like CoMarshalInterThreadInterfaceInStream, CoGetInterfaceAndReleaseStream or GlobalInterfaceTable to marshall a COM interface between threads? In this thread some say interop does it for you, some say you need to use these calls. Which is it?

Specifically I'm using various IHTMLxxx objects from mshtml while developing a Browser Helper Object within IE. I've never used methods like CoMarshalInterThreadInterfaceInStream, and never been aware of any problems this has caused. But perhaps it could be the cause of some intermittent un-repeatable problems...

thanks.

Was it helpful?

Solution

From experience, if your dll is loaded as a BHO in IE then you don't need to do any cross-thread marshalling. It seems that .NET does all the magic yourself.

What you should do though is ensure that you only use STA threads to access MSHTML objects. Any threads coming in to a BHO from IE events will automatically be STA threads but if you have other threads (e.g. background timers) then you should ensure they're STA before accessing IE objects else bad things will happen.

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