Is there some way in Windows to prevent unloading of our dll via FreeLibrary? I.e. to "pin" it in memory for the life of the process?
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().
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.
Each DLL and exe you create has a its own heap, i.e. _crtheap. The allocations and de-allocations has to happen from respective heap. That a dynamically allocated from DLL, cannot be de-allocated from executable and vice-versa.
I know it's an old thread, but there is a 'proper' way to do this:
Call GetModuleHandleEx
with the GET_MODULE_HANDLE_EX_FLAG_PIN
flag.
From MSDN:
The module stays loaded until the process is terminated, no matter how many times FreeLibrary is called.
Just in case anyone else finds this thread...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With