Don't use DLL_PROCESS_DETACH.
Create a new thread, use some kind on synchronization to make sure it waits until your endscene hook is done, unhook, call FreeLibraryAndExitThread function
For example:
Code:
EndSceneHook()
{
if(!TryEnterCriticalSection(&g_framelock))
return; // something else is using the framelock. exit function.
// do stuff..
LeaveCriticalSection(&g_framelock);
}
UnloadDLLFunc() // called from a new thread
{
EnterCriticalSection(&g_framelock); // call blocks until framelock aquired. ie. it's guaranteed to not be in endscene
// [unhook code]
DeleteCriticalSection(&g_framelock);
FreeLibraryAndExitThread(dllhandle, 0);
}