How to load a DLL in a second thread?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Robinychen
    New Member
    • Nov 2007
    • 8

    How to load a DLL in a second thread?

    Hi experts:

    I have a DLL created under Visual C++ 6.0 and I have an OCX which is created under VS 2005 .NET. The DLL calls this OCX. The application has two threads. If I load the DLL in the main thread, there is not a problem. Because of architectural limitation, I can not load the DLL in the main thread but just the secondary thread. However, I failed loading the DLL dynamically in the secondary thread.

    Anyone who can help? Thanks a lot in advance!

    Rob
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Nothing prevents a DLL from being loaded on a secondary thread.

    Is it LoadLibrary that fails??

    You might post your code.

    Comment

    • Robinychen
      New Member
      • Nov 2007
      • 8

      #3
      Originally posted by weaknessforcats
      Nothing prevents a DLL from being loaded on a secondary thread.

      Is it LoadLibrary that fails??

      You might post your code.
      Yes, the LoadLibrary() fails because this DLL interacts with an OCX. The OCX was initialized inside the DLL. The DLL was being loaded in a second thread.

      Thanks.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        OK, fine. The DLL needs the OCX. But it seems like the OCX is not availble on the other thread. Does not the DLL load and intiailize the OCX?

        Comment

        • Robinychen
          New Member
          • Nov 2007
          • 8

          #5
          Originally posted by weaknessforcats
          OK, fine. The DLL needs the OCX. But it seems like the OCX is not availble on the other thread. Does not the DLL load and intiailize the OCX?
          Thanks.

          We changed two things: (1) register the OCX as "Apartment Threading"; (2) Create the second thread as "User Interface Thread" (previously it was Worker Thread).

          Now we can load the DLL in the second thread and the DLL loads and initializes the OCX in the same thread without problem.

          The question is: Why does it work in this way? Could you please comment on this issue? Thanks a lot!

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            I'm not a big COM expert but the Apartment is for COM objects that require marshaling. That is, they cannot be multithreaded. Usually, these COM objects have global variables or static variables and are not set up to run on multiple threads. The Apartment guarantees that only one of the objects us running at any given time in the Apratment.

            That may have been your problem.

            If the COM object is registered as Multithreaded, then there is no marshaling and it is up to the COM object to be able to run on multiple threads.

            There is one multithreaded apartment (COINIT_MULTITH READED) or there can be many single-threaded apartments ( COINIT_APARTMEN TTHREADED) but not both at the same time.

            This is determined by how COM was initialized in your program by
            CoInitializeEx( ).

            The fact you use the phrase user interface thread leads me to believe you are using MFC. There the user interface threads are set up to handle events wirthout concern for what is going on in other threads (i.e. apartment threaded).

            Comment

            • Robinychen
              New Member
              • Nov 2007
              • 8

              #7
              Originally posted by weaknessforcats
              I'm not a big COM expert but the Apartment is for COM objects that require marshaling. That is, they cannot be multithreaded. Usually, these COM objects have global variables or static variables and are not set up to run on multiple threads. The Apartment guarantees that only one of the objects us running at any given time in the Apratment.

              That may have been your problem.

              If the COM object is registered as Multithreaded, then there is no marshaling and it is up to the COM object to be able to run on multiple threads.

              There is one multithreaded apartment (COINIT_MULTITH READED) or there can be many single-threaded apartments ( COINIT_APARTMEN TTHREADED) but not both at the same time.

              This is determined by how COM was initialized in your program by
              CoInitializeEx( ).

              The fact you use the phrase user interface thread leads me to believe you are using MFC. There the user interface threads are set up to handle events wirthout concern for what is going on in other threads (i.e. apartment threaded).
              Thanks for your explanation.

              Actually we only solved 50% of our problem. The DLL and OCX were created with Visual C++ 6.0 but our application is being created with Delphi 7.0. The project requirement is that we have to create two threads in this application. We need to load the DLL, which will be interacting with the OCX, in the secondary thread (not the main thread). At the beginning, we failed. After we changed the registration of this OCX to "Apartment Threading", the DLL can be loaded in the secondary thread now with Delphi programming. However, we can not get the messages/events from the OCX. The above description on "User Interface Thread" was our experiment under Visual C++ 6.0. We successfully got the messages/events in the second thread which was "User Interface Thread" under Visual C++ 6.0. However, we have not succeeded yet with Delphi.

              Could you please help? Thanks a lot for your kind heart!

              Comment

              • weaknessforcats
                Recognized Expert Expert
                • Mar 2007
                • 9214

                #8
                I know nothing of Delphi.

                I may be at the end of my assistance.

                Comment

                • Cucumber
                  New Member
                  • Sep 2007
                  • 90

                  #9
                  Are you creating a message loop in your second thread?

                  The visual C++ "user interface thread" has a message loop. Thats the basic difference between a user interface thread and a working thread in VC++ (afaik)

                  So if Delphi has no notion of "UI threads" vs "working threads" ( I know nothing about Delphi), you might just create a message loop in your thread and thats it.

                  Comment

                  • Robinychen
                    New Member
                    • Nov 2007
                    • 8

                    #10
                    Originally posted by Cucumber
                    Are you creating a message loop in your second thread?

                    The visual C++ "user interface thread" has a message loop. Thats the basic difference between a user interface thread and a working thread in VC++ (afaik)

                    So if Delphi has no notion of "UI threads" vs "working threads" ( I know nothing about Delphi), you might just create a message loop in your thread and thats it.
                    This problem was solved yesterday. Our solution was almost the same as yours. Anyway, thanks a lot.

                    Comment

                    Working...