Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does a DLL get removed if the DLL that loaded it is unloaded?

Take a standard Windows application. It loads a DLL using LoadLibrary to call a function in it (we'll call this DLL_A). That function loads another DLL (we'll call it DLL_B). The application now unloads the DLL_A DLL using FreeLibrary as it no longer requires it.

The question is: Is DLL_B still in memory and loaded?

Is this something I can depend upon, or is it undocumented?

like image 989
mj2008 Avatar asked Mar 05 '09 16:03

mj2008


People also ask

Can a DLL unload itself?

If your asking if you can safely unload/unmap a DLL loaded in a process from code in the DLL itself, the answer is no - there isn't really a safe way to do this. Think about it this way: Unloading a DLL is done by decrementing it's reference count using FreeLibrary().

How do you check if a DLL is already loaded?

Unzip it and run the executable (procexp.exe) on the target machine as an administrator. Once running, enable viewing of loaded DLLs by either pressing CTRL+D or using the View > Lower Pane View > DLLs entry from the menu bar. Select the target process in the upper pane. The lower pane should now show loaded modules.

How do I unload a DLL file?

There is no way to unload DLL . Once you load the DLL into AutoCAD, it will be locked then by AutoCAD. You have to close AutoCAD to build that again.

Are DLLs loaded at runtime?

DLL files may be explicitly loaded at run-time, a process referred to simply as run-time dynamic linking by Microsoft, by using the LoadLibrary (or LoadLibraryEx ) API function. The GetProcAddress API function is used to look up exported symbols by name, and FreeLibrary – to unload the DLL.


1 Answers

No. DLL_B will not be unloaded. The LoadLibrary() call made by DLL_A will increment the load count for DLL_B. Since there is no corresponding FreeLibrary() call for DLL_B, the refcount will not go to zero.

From the LoadLibrary() docs:

The system maintains a per-process reference count on all loaded modules. Calling LoadLibrary increments the reference count. Calling the FreeLibrary or FreeLibraryAndExitThread function decrements the reference count. The system unloads a module when its reference count reaches zero or when the process terminates (regardless of the reference count).

like image 64
Michael Burr Avatar answered Sep 28 '22 12:09

Michael Burr