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. C / C++ / MFC
  4. Using COM in DLL called by .Net application

Using COM in DLL called by .Net application

Scheduled Pinned Locked Moved C / C++ / MFC
csharpquestionwpfcomsysadmin
6 Posts 3 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.
  • L Offline
    L Offline
    Leif Simon Goodwin
    wrote on last edited by
    #1

    I have a .Net/WPF application which calls methods on a DLL. One of these methods - Load() - calls CoCreateInstance to load a COM server provided by a third party. If I do all this in the Load() thread, it works fine i.e. Load() calls CoCreateInstance directly. However if Load() creates a worker thread, and then waits for completion, the CoCreateInstance call hangs. If I remove the wait, so that Load() returns before the worker thread completes, it works. The CoCreateInstance call returns AFTER the Load() function returns. :~ Anyone know what is happening? It is as if it is serialising the COM access for some reason ...

    Richard DeemingR _ 2 Replies Last reply
    0
    • L Leif Simon Goodwin

      I have a .Net/WPF application which calls methods on a DLL. One of these methods - Load() - calls CoCreateInstance to load a COM server provided by a third party. If I do all this in the Load() thread, it works fine i.e. Load() calls CoCreateInstance directly. However if Load() creates a worker thread, and then waits for completion, the CoCreateInstance call hangs. If I remove the wait, so that Load() returns before the worker thread completes, it works. The CoCreateInstance call returns AFTER the Load() function returns. :~ Anyone know what is happening? It is as if it is serialising the COM access for some reason ...

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      Could be an STA / MTA issue. Your UI thread will be STA, but a background thread will usually default to MTA. If your COM component requires STA, then your worker thread will need to be STA. Understanding and Using COM Threading Models[^] Apartments and Pumping in the CLR – cbrumme's WebLog[^] Thread.SetApartmentState Method (ApartmentState) (System.Threading)[^]


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      L 2 Replies Last reply
      0
      • L Leif Simon Goodwin

        I have a .Net/WPF application which calls methods on a DLL. One of these methods - Load() - calls CoCreateInstance to load a COM server provided by a third party. If I do all this in the Load() thread, it works fine i.e. Load() calls CoCreateInstance directly. However if Load() creates a worker thread, and then waits for completion, the CoCreateInstance call hangs. If I remove the wait, so that Load() returns before the worker thread completes, it works. The CoCreateInstance call returns AFTER the Load() function returns. :~ Anyone know what is happening? It is as if it is serialising the COM access for some reason ...

        _ Offline
        _ Offline
        _Superman_
        wrote on last edited by
        #3

        You could try calling CoInitialize[^] in the worker thread before the call to CoCreateInstance.

        «_Superman_»  _I love work. It gives me something to do between weekends.

        _Microsoft MVP (Visual C++) (October 2009 - September 2013)

        Polymorphism in C

        L 1 Reply Last reply
        0
        • _ _Superman_

          You could try calling CoInitialize[^] in the worker thread before the call to CoCreateInstance.

          «_Superman_»  _I love work. It gives me something to do between weekends.

          _Microsoft MVP (Visual C++) (October 2009 - September 2013)

          Polymorphism in C

          L Offline
          L Offline
          Leif Simon Goodwin
          wrote on last edited by
          #4

          Thanks for the comment. Yes I call CoInitialise, that is not the issue. I wish it was. :)

          1 Reply Last reply
          0
          • Richard DeemingR Richard Deeming

            Could be an STA / MTA issue. Your UI thread will be STA, but a background thread will usually default to MTA. If your COM component requires STA, then your worker thread will need to be STA. Understanding and Using COM Threading Models[^] Apartments and Pumping in the CLR – cbrumme's WebLog[^] Thread.SetApartmentState Method (ApartmentState) (System.Threading)[^]


            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

            L Offline
            L Offline
            Leif Simon Goodwin
            wrote on last edited by
            #5

            Deleted!

            1 Reply Last reply
            0
            • Richard DeemingR Richard Deeming

              Could be an STA / MTA issue. Your UI thread will be STA, but a background thread will usually default to MTA. If your COM component requires STA, then your worker thread will need to be STA. Understanding and Using COM Threading Models[^] Apartments and Pumping in the CLR – cbrumme's WebLog[^] Thread.SetApartmentState Method (ApartmentState) (System.Threading)[^]


              "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

              L Offline
              L Offline
              Leif Simon Goodwin
              wrote on last edited by
              #6

              Thanks for the comments. I have tested using a simple Win32 C++ console application to drive our DLL, and it works fine when using the worker thread to invoke COM commands to the third party component. So clearly this is something to do with the .Net application. Okay, I have solved it. A COM STA uses Windows messages to serialise access to the COM object which might not be thread safe. So when I created a worker thread, the main thread then hung waiting for the worker thread to finish, but it never did, as the main thread hanging blocked the message pump! Instead of the main thread waiting, I now make it periodically run a message pump to ensure that messages are process. That has solved the problem. Yikes. I hate COM! Thank you for your helpful post. Added later: I have also looked into removing the worker thread and marshalling the COM interfaces across threads and this works. It has the advantage that I do not have to mess around with a message pump, which is akin to spinning the tyres.

              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