Solved

Unloading "load-time dynamic linked" DLLs

Posted on 1999-01-22
8
670 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
 
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
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.

707 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now