A thread that must unload the DLL in which it is executing and then terminate itself should call FreeLibraryAndExitThread instead of calling FreeLibrary and ExitThread separately. Otherwise, a race condition can occur. For details, see the Remarks section of FreeLibraryAndExitThread.What does FreeLibrary do when it unloads a DLL?
Your requirements are impossible. When FreeLibrary is called, and it unloads a Dll, it does send a message DLL_PROCESS_DETATCH to the DllMain function. statically allocated c++ objects might have their destructors run at this time.What to do when DLL hangs on unload?
I have a DLL which should be useable in any application that can load custom DLLs. This DLL needs to make some socket operations when being unloaded. Thus, this final operations are done witin ExitInstance of DLL. In addition, it has an extra cleanup function doing those operations.How does the FreeLibrary function in Win32 work?
Frees the loaded dynamic-link library (DLL) module and, if necessary, decrements its reference count. When the reference count reaches zero, the module is unloaded from the address space of the calling process and the handle is no longer valid. A handle to the loaded library module.