Solved

Unloading "load-time dynamic linked" DLLs

Posted on 1999-01-22
8
691 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

628 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