Solved

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

Posted on 2010-11-23
6
512 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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
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

Suggested Solutions

Title # Comments Views Activity
Sed question 2 139
eclipse apache tomcat admin console 52 193
How do i de-activate my Visual Studio 2012 Professional license 3 70
SCOM monitoring possibilities? 9 59
As with any other System Center product, the installation for the Authoring Tool can be quite a pain sometimes. This article serves to help you avoid making these mistakes and hopefully save you a ton of time on troubleshooting :)  Step 1: Make sur…
This collection of functions covers all the normal rounding methods of just about any numeric value.
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
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 …

752 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