i was looking for code to make a dll unload itself, and foud some broken code
problem was the coder was pushing a handle to the exe not the dll
here is the fixed code
void UnloadSelf(HMODULE hdl)
{
LPVOID FP_ExitThread = (LPVOID)GetProcAddress(GetModuleHandle("kernel32.dll"), "ExitThread");
__asm
{
push hdl
push FP_ExitThread
jmp dword ptr [FreeLibrary]
}
}
to get the handle to the loaded dll you can just grab it from main
example:
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
switch (fdwReason)
{
case DLL_PROCESS_ATTACH:
MessageBox(0,"hello","world",0);
UnloadSelf(hinstDLL);
break;
case DLL_PROCESS_DETACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
break;
}
return TRUE;
}
beats injecting code to call FreeLibrary