none
.NET to native COM object - threading models RRS feed

  • Question

  • Hi guys,

    I have a small unmanaged COM object with threading model set to "Both"
    which I try to use from .NET.
    Everything was OK until I tried to call the COM from a different thread than the thread the com object was created in. "FailedQI was detected" - "The call to QI for interface... No such interface supported..." or "Unable to cast COM object of type... This operation failed because the QueryInterface call on the COM component for the interface with IID ..."

    When I use the COM within a native application I can do whatever I want and use it from as much threads I want - no problems.

    What am I doing wrong?

    Thanks.

    In fiery flight we would leave this hall...
    Wednesday, March 25, 2009 8:55 AM

Answers

  • Why are you declaring the threading model as "Both" when you support "Free"?
    Hans Passant.
    • Marked as answer by Xaoc Monday, April 27, 2009 11:09 AM
    Wednesday, March 25, 2009 2:08 PM
    Moderator

All replies

  • Hello,

    Which kind of application do you have? Desktop or ASP.NET?

    In case of Desktop application:
    Which attribute is your Main method decorated with?
    Is it STAThreadAttribute or MTAThreadAttribute?
    That could be the reason. More info here:
    http://www.ddj.com/windows/184416820

    Vitaliy Liptchinsky http://dotnetframeworkplanet.blogspot.com/
    Wednesday, March 25, 2009 9:23 AM
  • Thanks for answering.
    It's a desktop application, STAThread-ed

    But I pick the COM up in a different MTA thread and try to use it from third thread ...

    In fiery flight we would leave this hall...
    Wednesday, March 25, 2009 9:27 AM
  • With a threading model of "Both", you have to marshal the interface pointer.  You are probably not doing this in your unmanaged code.  .NET is doing the Right Thing and hits the wall because you didn't implement proxies.  A threading model of "Free" is not the same as "Both".
    Hans Passant.
    Wednesday, March 25, 2009 12:31 PM
    Moderator
  • Thank you nobugz,

    and no, I'm not marshaling the interface pointer. Please give me directions how to do that exactly? How do I implement a proxy?
    And yes, I know Free is not Both.

    In fiery flight we would leave this hall...
    Wednesday, March 25, 2009 12:43 PM
  • You do it with CoMarshalInterThreadInterfaceInStream or IGlobalInterfaceTable.  Do you really want to make your unmanaged code fail the same way as .NET?
    Hans Passant.
    Wednesday, March 25, 2009 1:26 PM
    Moderator
  • Actually I don't.
    I want to have a singleton like COM object and use it wherever and whenever I want by .NET and W32 apps. The COM object itself worries about thread safety.
    // edit
    And just to be clear - an ordinary in-process server.

    I pick it up in the main thread (for example) and later I use it by any thread I need its functionality - the same way I use it now by win32 apps.


    In fiery flight we would leave this hall...
    Wednesday, March 25, 2009 1:47 PM
  • Why are you declaring the threading model as "Both" when you support "Free"?
    Hans Passant.
    • Marked as answer by Xaoc Monday, April 27, 2009 11:09 AM
    Wednesday, March 25, 2009 2:08 PM
    Moderator
  • Cause with "Free" I cannot use it in STA threads?

    So how can I pickup com object in MTA thread and later use it STA or MTA threads? Should I use IGlobalInterfaceTable and implement proxy to accomplish this?

    In fiery flight we would leave this hall...
    Wednesday, March 25, 2009 2:37 PM