Click here to Skip to main content
15,891,993 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have to load a 3rd party DLL in my project, so I use

HMODULE m_Instance;
...
m_Instance = LoadLibrary("ThirdParty.dll");


My project is yet another DLL that is called by the master application.

At some point the application destroys my DLL then recreates it.

In which time my program calls FreeLibrary to unload the ThirdParty.dll before dying.

The application recreates my DLL so somewhere in the start up sequence, I call LoadLibrary again.

The call gets stuck here and doesn't return.

As an observation, here are some things I noticed that may have something to do with the problem:

When ThirdParty.dll is loaded, a whole bunch of other DLLs are loaded by ThirdParty.dll.

When ThirdParty.dll is unloaded, I can see it being unloaded in the output window if I'm debugging. However, the rest of the DLLs that ThirdParty.dll seemed to have loaded are not listed as being unloaded.

Then, on restart (my DLL has been released and recreated, but the main application has remained), ThirdParty.dll is loaded and that's where it gets stuck.

I'm wondering if the DLLs loaded by ThirdParty.dll are not released appropriately when I call FreeLibrary.

However, that's just my guess, and I rather hope that's not the case because I can't fix what' in ThirdParty.dll.

Is there anything I can do to make sure I can load ThirdParty.dll a second time?
Posted

I'm no expert in this area, but the following ideas come to mind:
It looks like ThirdParty.dll is being naughty and not unloading its descendants. So, maybe you could unload them after you unload it. Messy X| Alternatively, why unload it at all? *If* (a big if!) you don't rely on ThirdParty.dll being reinitialised when you reload it, could you just leave it there while your dll goes away and returns? To do this, your initial load of ThirdParty.dll would need to be conditional - do a GetProcAddress() into it or something just to see if it's already there.
I may be talking out of my situpon, so feel free to shoot me down!

Best of luck,
Peter
 
Share this answer
 
Comments
PaulowniaK 7-Mar-11 22:51pm    
Thanks.

Firstly, whether my DLL gets wiped and rebuilt depends on the usage of the application. The path that it goes through in getting rebuilt is essentially the same as a full shut down followed by a full start up. So I wouldn't know when to keep the DLLs alive and when to get rid of them.

As for the descendant DLLs, I may be wrong about them. I just created a dummy project that loads and unloads ThirdPart.dll in succession. No problem there, so the descendants are just not being written to the output window.

Thought continues... ... ...
seems to me that the 3rd party library isn't getting unloaded properly (i.e its messing up) by any chance your are not loading the 3rd party library in the entry point of your library right?

Regards,

K,D
 
Share this answer
 
Comments
PaulowniaK 8-Mar-11 19:16pm    
Nope. The library is getting loaded as part of an initialization process of my DLL, but it's not directly at the entry point.
Kurt Degiorgio 9-Mar-11 5:35am    
I suggest you create a dummy application then attempt to load it and unload for a number of times while doing this ensure you don't have handle or memory leaks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900