Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Unloading "load-time dynamic linked" DLLs

Posted on 1999-01-22
8
Medium Priority
?
699 Views
Last Modified: 2008-07-14
I have created a DLL (say A.DLL) which loads another DLL (say B.DLL) dynamically using LoadLibrary call. B.DLL loads several other DLLs (say C.DLL, D.DLL ... so on) at load time. When I call FreeLibrary in A.DLL to unload the B.DLL, the DLLs C.DLL, D.DLL, .... are not unloaded. How do I unload these libraries?
0
Comment
Question by:sridhar_dvjs
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 14

Expert Comment

by:AlexVirochovsky
ID: 1184851
0
 

Author Comment

by:sridhar_dvjs
ID: 1184852
I am using MSVC 5.0 and the code suggested can not be work. GetModuleUsage is absolete in Win32.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1184853
Did you write B.DLL?  If so, it sounds like it has a bug.  I must call FreeLibrary at some point (possibly in the DllMain() procedure) to free any library it loads with LoadLibrary().
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 14

Expert Comment

by:AlexVirochovsky
ID: 1184854
sridhar_dvjs, do you test this solution? This works in my
programme  in 16b/32b application!
And nietod is , of course, right. Must be  FreeLibrary
to each LoadLibrary.
Alex
0
 
LVL 3

Accepted Solution

by:
luisr earned 60 total points
ID: 1184855
You can try:

hmodDLL = GetModuleHandle("C.DLL");
FreeLibrary(hmodDLL);

That should unload the DLL, even if it was implicity loaded.

0
 
LVL 22

Expert Comment

by:nietod
ID: 1184856
Luisr, that is what alex suggested.

I think both of you are making a big mistake.   It sounds like there is a bug somewhere that is causing this DLL to not be unloaded.  The type of code you propose is a patch that works around the around the problem rather than fixing the problem.  That is potentially very dangerious.  Fix the problem if possible.  That is why I want to know if you wrote--and therefore can fix--"B.DLL"
0
 

Expert Comment

by:jitu
ID: 1184857
Sridhar can u say me one thing. Have you explicitly loaded C.dll and D.dll.
               If this is the case then in the PROCESS_DETACH of the Dll_main of B.dll you will have to call FreeLibrary(). This procedure should work for you.

If you have implicitly loaded C.dll and D.dll
              then the system will take care of freeing the resources. Why because Windows is also a set of dll's which loads other dll's. And when you unload the windows dll, the system frees all the other dlls by itself. If the system is mot taking care of this then it shows that their is a serious problem in the code you have written.
    It would have been nice if you could mail me your code so that I can just go thru' it.
Jitendran.
0
 

Expert Comment

by:jitu
ID: 1184858
Sridhar can u say me one thing. Have you explicitly loaded C.dll and D.dll.
               If this is the case then in the PROCESS_DETACH of the Dll_main of B.dll you will have to call FreeLibrary(). This procedure should work for you.

If you have implicitly loaded C.dll and D.dll
              then the system will take care of freeing the resources. Why because Windows is also a set of dll's which loads other dll's. And when you unload the windows dll, the system frees all the other dlls by itself. If the system is mot taking care of this then it shows that their is a serious problem in the code you have written.
    It would have been nice if you could mail me your code so that I can just go thru' it.
Jitendran.
0

Featured Post

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.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

927 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