Solved

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

Posted on 2010-11-23
6
488 Views
Last Modified: 2013-12-14
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
Comment
Question by:leogi
  • 3
  • 2
6 Comments
 
LVL 7

Expert Comment

by:JimBeveridge
ID: 34201704
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
 
LVL 11

Expert Comment

by:DeepuAbrahamK
ID: 34203260
Is it a multithreaded DLL?
0
 

Author Comment

by:leogi
ID: 34205585
it is mutlithreaded
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 11

Expert Comment

by:DeepuAbrahamK
ID: 34207502
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
 

Author Comment

by:leogi
ID: 34208066
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
 
LVL 11

Accepted Solution

by:
DeepuAbrahamK earned 250 total points
ID: 34210558
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
Technology opened people to different means of presenting information, but PowerPoint remains to be above competition. Know why PPT still works today.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now