From 087f41ce119834341714843adf7382b27bc47d47 Mon Sep 17 00:00:00 2001 From: FunkyFr3sh Date: Thu, 29 Jun 2023 21:13:04 +0200 Subject: [PATCH] hook SetUnhandledExceptionFilter for debug build --- inc/debug.h | 1 + inc/hook.h | 2 ++ inc/winapi_hooks.h | 3 +++ src/debug.c | 4 ++++ src/dllmain.c | 4 ++-- src/hook.c | 20 +++++++++++++++++--- src/winapi_hooks.c | 10 ++++++++++ 7 files changed, 39 insertions(+), 5 deletions(-) diff --git a/inc/debug.h b/inc/debug.h index 844bb6d..31d0501 100644 --- a/inc/debug.h +++ b/inc/debug.h @@ -27,6 +27,7 @@ char* dbg_mes_to_str(int id); extern double g_dbg_frame_time; extern DWORD g_dbg_frame_count; +extern LPTOP_LEVEL_EXCEPTION_FILTER g_dbg_exception_filter; //#define _DEBUG 1 diff --git a/inc/hook.h b/inc/hook.h index c9c4400..12303d0 100644 --- a/inc/hook.h +++ b/inc/hook.h @@ -55,6 +55,7 @@ typedef HMODULE(WINAPI* LOADLIBRARYEXAPROC)(LPCSTR, HANDLE, DWORD); typedef HMODULE(WINAPI* LOADLIBRARYEXWPROC)(LPCWSTR, HANDLE, DWORD); typedef BOOL(WINAPI* GETDISKFREESPACEAPROC)(LPCSTR, LPDWORD, LPDWORD, LPDWORD, LPDWORD); typedef HRESULT(WINAPI* COCREATEINSTANCEPROC)(REFCLSID, LPUNKNOWN, DWORD, REFIID, LPVOID*); +typedef LPTOP_LEVEL_EXCEPTION_FILTER(WINAPI* SETUNHANDLEDEXCEPTIONFILTERPROC)(LPTOP_LEVEL_EXCEPTION_FILTER); extern GETCURSORPOSPROC real_GetCursorPos; extern CLIPCURSORPROC real_ClipCursor; @@ -93,6 +94,7 @@ extern LOADLIBRARYEXAPROC real_LoadLibraryExA; extern LOADLIBRARYEXWPROC real_LoadLibraryExW; extern GETDISKFREESPACEAPROC real_GetDiskFreeSpaceA; extern COCREATEINSTANCEPROC real_CoCreateInstance; +extern SETUNHANDLEDEXCEPTIONFILTERPROC real_SetUnhandledExceptionFilter; extern int g_hook_method; extern BOOL g_hook_dinput; diff --git a/inc/winapi_hooks.h b/inc/winapi_hooks.h index f84cf40..709f53a 100644 --- a/inc/winapi_hooks.h +++ b/inc/winapi_hooks.h @@ -61,4 +61,7 @@ HWND WINAPI fake_CreateWindowExA( HRESULT WINAPI fake_CoCreateInstance( REFCLSID rclsid, LPUNKNOWN pUnkOuter, DWORD dwClsContext, REFIID riid, LPVOID* ppv); +LPTOP_LEVEL_EXCEPTION_FILTER WINAPI fake_SetUnhandledExceptionFilter( + LPTOP_LEVEL_EXCEPTION_FILTER lpTopLevelExceptionFilter); + #endif diff --git a/src/debug.c b/src/debug.c index 6d9ebc2..6825342 100644 --- a/src/debug.c +++ b/src/debug.c @@ -10,6 +10,7 @@ double g_dbg_frame_time = 0; DWORD g_dbg_frame_count = 0; +LPTOP_LEVEL_EXCEPTION_FILTER g_dbg_exception_filter; static LONGLONG g_dbg_counter_start_time = 0; static double g_dbg_counter_freq = 0.0; @@ -74,6 +75,9 @@ int dbg_exception_handler(EXCEPTION_POINTERS* exception) filename); } + if (g_dbg_exception_filter) + return g_dbg_exception_filter(exception); + return EXCEPTION_EXECUTE_HANDLER; } #endif diff --git a/src/dllmain.c b/src/dllmain.c index 65be2d6..8a33069 100644 --- a/src/dllmain.c +++ b/src/dllmain.c @@ -25,10 +25,10 @@ BOOL WINAPI DllMain(HANDLE hDll, DWORD dwReason, LPVOID lpReserved) { case DLL_PROCESS_ATTACH: { -#if _DEBUG +#ifdef _DEBUG dbg_init(); TRACE("cnc-ddraw = %p\n", hDll); - SetUnhandledExceptionFilter((LPTOP_LEVEL_EXCEPTION_FILTER)dbg_exception_handler); + g_dbg_exception_filter = SetUnhandledExceptionFilter((LPTOP_LEVEL_EXCEPTION_FILTER)dbg_exception_handler); #endif g_ddraw_module = hDll; diff --git a/src/hook.c b/src/hook.c index 4ce22fd..ccc09c3 100644 --- a/src/hook.c +++ b/src/hook.c @@ -54,6 +54,7 @@ LOADLIBRARYEXAPROC real_LoadLibraryExA = LoadLibraryExA; LOADLIBRARYEXWPROC real_LoadLibraryExW = LoadLibraryExW; GETDISKFREESPACEAPROC real_GetDiskFreeSpaceA = GetDiskFreeSpaceA; COCREATEINSTANCEPROC real_CoCreateInstance = CoCreateInstance; +SETUNHANDLEDEXCEPTIONFILTERPROC real_SetUnhandledExceptionFilter = SetUnhandledExceptionFilter; static HOOKLIST g_hooks[] = { @@ -567,12 +568,14 @@ void hook_init() void hook_early_init() { - /* +#ifdef _DEBUG && _MSC_VER + hook_patch_iat(GetModuleHandle(NULL), FALSE, "kernel32.dll", "SetUnhandledExceptionFilter", (PROC)fake_SetUnhandledExceptionFilter); + DetourTransactionBegin(); DetourUpdateThread(GetCurrentThread()); - DetourAttach((PVOID*)&real_CoCreateInstance, (PVOID)fake_CoCreateInstance); + DetourAttach((PVOID*)&real_SetUnhandledExceptionFilter, (PVOID)fake_SetUnhandledExceptionFilter); DetourTransactionCommit(); - */ +#endif hook_patch_iat(GetModuleHandle(NULL), FALSE, "ole32.dll", "CoCreateInstance", (PROC)fake_CoCreateInstance); hook_patch_iat(GetModuleHandle("XIIIGame.dll"), FALSE, "ole32.dll", "CoCreateInstance", (PROC)fake_CoCreateInstance); //Hooligans @@ -633,6 +636,17 @@ void hook_exit() hook_revert((HOOKLIST*)&g_hooks); } +#ifdef _DEBUG && _MSC_VER + DetourTransactionBegin(); + DetourUpdateThread(GetCurrentThread()); + DetourDetach((PVOID*)&real_SetUnhandledExceptionFilter, (PVOID)fake_SetUnhandledExceptionFilter); + DetourTransactionCommit(); + + hook_patch_iat(GetModuleHandle(NULL), TRUE, "kernel32.dll", "SetUnhandledExceptionFilter", (PROC)fake_SetUnhandledExceptionFilter); + + real_SetUnhandledExceptionFilter(g_dbg_exception_filter); +#endif + hook_patch_iat(GetModuleHandle(NULL), TRUE, "ole32.dll", "CoCreateInstance", (PROC)fake_CoCreateInstance); hook_patch_iat(GetModuleHandle("XIIIGame.dll"), TRUE, "ole32.dll", "CoCreateInstance", (PROC)fake_CoCreateInstance); //Hooligans hook_patch_iat(GetModuleHandle(NULL), TRUE, "dinput.dll", "DirectInputCreateA", (PROC)fake_DirectInputCreateA); diff --git a/src/winapi_hooks.c b/src/winapi_hooks.c index 205eba3..25605c3 100644 --- a/src/winapi_hooks.c +++ b/src/winapi_hooks.c @@ -1076,3 +1076,13 @@ HRESULT WINAPI fake_CoCreateInstance(REFCLSID rclsid, LPUNKNOWN pUnkOuter, DWORD return real_CoCreateInstance(rclsid, pUnkOuter, dwClsContext, riid, ppv); } + +LPTOP_LEVEL_EXCEPTION_FILTER WINAPI fake_SetUnhandledExceptionFilter( + LPTOP_LEVEL_EXCEPTION_FILTER lpTopLevelExceptionFilter) +{ + LPTOP_LEVEL_EXCEPTION_FILTER old = g_dbg_exception_filter; + g_dbg_exception_filter = lpTopLevelExceptionFilter; + + return old; + //return real_SetUnhandledExceptionFilter(lpTopLevelExceptionFilter); +}