Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>As I understand it, it CAN be done and is MEANT to be done sometimes (for example in case of dll injection by CreateRemoteThread and other methods). So,</p> <pre><code>FreeLibraryAndExitThread(hModule, 0) </code></pre> <p>will do precisely that.</p> <p>On the other hand, calling </p> <pre><code>FreeLibrary(hModule) </code></pre> <p>will not do here - from MSDN: <em>"If they were to call FreeLibrary and ExitThread separately, a race condition would exist. The library could be unloaded before ExitThread is called."</em> As a remark, ExitThread does some bookkeeping besides just returning from the thread function.</p> <p>All this assumes that Your Dll obtained the hModule itself by calling <strong>LoadLibrary</strong> from inside the loaded Dll, or rather, by calling from inside the loaded Dll the following function: </p> <pre><code>GetModuleHandleEx ( GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCTSTR)DllMain, &amp;hModule ) </code></pre> <p>This increments the reference count of the Dll so You know that if You free the library later using that handle and if the library is really unloaded then You had the last reference to it. <br> If You instead skip incrementing the Dll's reference count and obtain the hModule just from the argument to <strong>DllMain</strong> during <strong>DLL_PROCESS_ATTACH</strong> then You should not call <strong>FreeLibraryAndExitThread</strong> since the code that loaded the Dll is still using it and this module handle really isn't Yours to manage.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload