Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. COM
  4. Thread Safety Problem

Thread Safety Problem

Scheduled Pinned Locked Moved COM
helpc++comquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Suchit
    wrote on last edited by
    #1

    I have apartment threaded built VB activex control hosted on ATL dialog - they reside in thread T1. Another CComObject component in thread T2 of the same process - tries to access methods of this ActiveX control. There is an exception. I tried using Lock(), Unlock() provided by ATLModule, and also explicitly creating CComCriticalSection. But in vain. Can anyone help?

    J 1 Reply Last reply
    0
    • S Suchit

      I have apartment threaded built VB activex control hosted on ATL dialog - they reside in thread T1. Another CComObject component in thread T2 of the same process - tries to access methods of this ActiveX control. There is an exception. I tried using Lock(), Unlock() provided by ATLModule, and also explicitly creating CComCriticalSection. But in vain. Can anyone help?

      J Offline
      J Offline
      Jorgen Sigvardsson
      wrote on last edited by
      #2

      Someone wrote: Another CComObject component in thread T2 of the same process - tries to access methods of this ActiveX control. Please have a look at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/com/htm/cmf_a2c_88tp.asp[^] Suppose you have the interface pointer pCtrl in thread T1. Then you cannot use that pointer straight away in thread T2. You would have to do something like this:

      IStream* pStream;
      CoMarshalInterThreadInterfaceInStream(IID_ITheInterfaceID, pCtrl, &pStream);

      If the call is successful, pStream will be a stream containing a reference to your object pCtrl and apartment marshalling information. In thread T2, you pick up the pStream object, and unmarshal the interface pointer from the stream like this:

      ITheInterface* pCtrl2;
      // I can't remember if the stream needs to be rewound or not
      // if this code fails, please see IStream::Seek()
      CoGetInterfaceAndReleaseStream(pStream, IID_ITheInterfaceID, (void**)pCtrl2);

      The interface pointer pCtrl2 will now be accessible in thread T2. Generally speaking, the interface pointer deserialized from a stream is accessible in the thread where it was deserialized. -- Arigato gozaimashita!

      S 1 Reply Last reply
      0
      • J Jorgen Sigvardsson

        Someone wrote: Another CComObject component in thread T2 of the same process - tries to access methods of this ActiveX control. Please have a look at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/com/htm/cmf_a2c_88tp.asp[^] Suppose you have the interface pointer pCtrl in thread T1. Then you cannot use that pointer straight away in thread T2. You would have to do something like this:

        IStream* pStream;
        CoMarshalInterThreadInterfaceInStream(IID_ITheInterfaceID, pCtrl, &pStream);

        If the call is successful, pStream will be a stream containing a reference to your object pCtrl and apartment marshalling information. In thread T2, you pick up the pStream object, and unmarshal the interface pointer from the stream like this:

        ITheInterface* pCtrl2;
        // I can't remember if the stream needs to be rewound or not
        // if this code fails, please see IStream::Seek()
        CoGetInterfaceAndReleaseStream(pStream, IID_ITheInterfaceID, (void**)pCtrl2);

        The interface pointer pCtrl2 will now be accessible in thread T2. Generally speaking, the interface pointer deserialized from a stream is accessible in the thread where it was deserialized. -- Arigato gozaimashita!

        S Offline
        S Offline
        Suchit
        wrote on last edited by
        #3

        I tried this, but due to some runtime exception, changed to Global Interface Table. Now the problem is: I create instance of IGlobalInterfaceTable and marshal my interface into it using RegisterInterfaceInGlobal(..). It returns me HRESULT of Successful. Now if I Unmarshall this interface in the same thread somewhere down the line, I can successfully retriev it & call methods on this interface. Problem is that in another thread, when I create instance of Global interface table, and access the interface using global cookie, I get HRESULT of 0x8000FFFF which means catastrophic failure. The second thread calls CoInitializeEx(NULL, COINIT_APARTMENTTHREADED); and CoUninitialize(). Also it waits for a event handle. Apart from the wait statement, the thread returns immediately. (it does not have message pump).

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups