Solved

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

Posted on 2010-11-23
6
518 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The canonical version of this article is on my web site here: http://iconoun.com/articles/collisions/ A companion presentation is available here: http://iconoun.com/articles/collisions/Unicode_Presentation.pdf
We were having a lot of "Heartbeat Alerts" in our SCOM environment, now "Heartbeat" in a SCOM environment for those of you who might not be familiar with SCOM is a packet of data sent from the agent to the management server on a regular basis, basic…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
Suggested Courses

623 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