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

  • 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...


    - Rory
    Wednesday, May 27, 2009 6:22 PM


  • The documentation seems to say that this is taken care of automatically:

    http://msdn.microsoft.com/en-us/library/8bwh56xe.aspx (emphasis added)
    The runtime creates exactly one RCW for each COM object, regardless of the number of references that exist on that object. The runtime maintains a single RCW per process for each object. If you create an RCW in one application domain or apartment, and then pass a reference to another application domain or apartment, a proxy to the first object will be used.

    Whenever the COM object and the managed thread are in incompatible apartments, all calls on the object are made through a COM-created proxy.
    • Proposed as answer by Mattias Sjögren Friday, May 29, 2009 7:17 AM
    • Marked as answer by Rory PS Friday, May 29, 2009 9:21 AM
    Thursday, May 28, 2009 1:06 AM