Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2010-11-23
6
Medium Priority
?
526 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:Deepu Abraham
ID: 34203260
Is it a multithreaded DLL?
0
 

Author Comment

by:leogi
ID: 34205585
it is mutlithreaded
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 11

Expert Comment

by:Deepu Abraham
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:
Deepu Abraham earned 1000 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

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

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

This collection of functions covers all the normal rounding methods of just about any numeric value.
In this post we will learn different types of Android Layout and some basics of an Android App.
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…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

688 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