Can Dllmain use FreeLibrary?

I want to create a dll that will unload it myself in case of some condifiton, which means

BOOL APIENTRY DllMain( HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) { if (ul_reason_for_call==DLL_PROCESS_ATTACH) if (!CheckSomething()) //check if a file doesnt exists for example FreeLibrary(hModule); } 

I tried, but I could not get it to work. if there is any walk on an alternative solution. please tell me, I don’t need the process that the DLL attached to unload it, I want it to unload it myself.

+4
source share
2 answers

You cannot unload a DLL; it is not loaded yet. What is BOOL return for? If you do not want to load the DLL, return FALSE .

+14
source

If you call the FreeLibrary function in the main dll function, you should see an error message. Because the call to the FreeLibrary function succeeds. But the FreeLibrary return address frees up memory. So the process is crashing! (Access Violation).

If you want to see the source code "Dll ​​Self Unloading": Self-extracting Dll

0
source

Source: https://habr.com/ru/post/1388167/


All Articles