Solved

Unloading "load-time dynamic linked" DLLs

Posted on 1999-01-22
8
689 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
[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
  • 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 20 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

Independent Software Vendors: 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!

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

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