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
Solved

Unloading "load-time dynamic linked" DLLs

Posted on 1999-01-22
8
684 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

809 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