Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 529
  • Last Modified:

loading dll from two different classes and calling same functions causes problems

My main application (WIN 32, MFC) loads a dll using loadlibrary and calls a function inside that dll called calculate.
A little later in processing the option price update that same application performs a loadlibrary as well and calls that same function calculate.
The reason the library is loaded twice is because the first part it is loaded is from a static library (.lib) and the second part is from the main application itself (.exe)
This architecture locks everything up.
However if the exe loads the library and calls a different function from that library everything worlks ok.
I am trying to figure out why calling the same function from a .lib and and .exe doesn't seem to work
0
leogi
Asked:
leogi
  • 3
  • 2
1 Solution
 
JimBeveridgeCommented:
There's nothing wrong with calling LoadLibrary twice. That's perfectly acceptable. The second call won't actually load the DLL twice, you'll just bump a reference count.

When you say "locks everything up", I assume you mean the app, not Windows.

Run it under the debugger, then tell Visual Studio to "Break"the application (from the Debug menu) once it hangs. Look at the call stack to see what the problem is.

Make sure you have your symbols configured for MFC and Windows so that the call stack is displayed correctly.
0
 
Deepu AbrahamR & D Engineering ManagerCommented:
Is it a multithreaded DLL?
0
 
leogiAuthor Commented:
it is mutlithreaded
0
Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

 
Deepu AbrahamR & D Engineering ManagerCommented:
Is it a COM dll? A dead lock might have occured. Have a look at this:
http://support.microsoft.com/kb/811591

Can you share that function definition which is getting called by the .lib and the .exe?
0
 
leogiAuthor Commented:
actually I found the problem

One of the places that calls the dll function called calculate is mutex protected. In that part of the code that is mutex protected, a ::SendMessage is sent to a dialog. It seems that when a thread that is not the main thread of the DLL uses SendMessage as opposed to PostMessage, it will wait for SendMessage to return to release the mutex. However the main thread seems unable to process the message in that case.

What I found is that:

1 - either I'll have to unmutex the code if it has a SendMessage in it (which I can't do because the code can't be run concurrently)
2 - either I change the SendMessage to Postmessage (which in my case I can't do because it it the CListBox::getcount() method that triggers that SendMessage)
3 - or I no longer sendmessage to the main DLL thread and instead use other variable that hold the information I am looking for when I called the CListBox::getcount() in the first place

Option 3 did the trick

I'm however still a bit unclear about why a thread that is not the main DLL thread that has a message map to process message can't seem to be able to lock/unlock the sendmessage(...) call. FYI the thread that sends the message is not alone, so they would wait on each other to send the message to the main thread that will update the dialog object. Somehow the main thread can't process the message it is receiving from the other thread





0
 
Deepu AbrahamR & D Engineering ManagerCommented:
I am glad you found the problem.I need my share ;)

To post a message to a thread's message queue and return immediately, use the PostMessage or PostThreadMessage function.Thread that did the SendMessage will block until main thread processes the message.

Or you might have to use MsgWaitForMultipleObjects() have a look at this:

http://codereflect.com/2008/09/19/when-and-how-should-we-use-msgwaitformultipleobjects/
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now