diff --git a/DDrawCompat/Common/Hook.cpp b/DDrawCompat/Common/Hook.cpp index 0d44641..550ae1f 100644 --- a/DDrawCompat/Common/Hook.cpp +++ b/DDrawCompat/Common/Hook.cpp @@ -1,9 +1,11 @@ #define WIN32_LEAN_AND_MEAN #include +#include #include #include #include +#include #include #include #include @@ -82,16 +84,25 @@ namespace return ntHeaders; } - std::string getModuleBaseName(HMODULE module) + std::filesystem::path getModulePath(HMODULE module) { char path[MAX_PATH] = {}; GetModuleFileName(module, path, sizeof(path)); - const char* lastBackSlash = strrchr(path, '\\'); - const char* baseName = lastBackSlash ? lastBackSlash + 1 : path; - return baseName; + return path; } - void hookFunction(const char* funcName, void*& origFuncPtr, void* newFuncPtr) + std::string funcAddrToStr(void* funcPtr) + { + std::ostringstream oss; + HMODULE module = nullptr; + GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, + static_cast(funcPtr), &module); + oss << getModulePath(module).string() << "+0x" << std::hex << + reinterpret_cast(funcPtr) - reinterpret_cast(module); + return oss.str(); + } + + void hookFunction(void*& origFuncPtr, void* newFuncPtr, const char* funcName) { const auto it = findOrigFunc(origFuncPtr); if (it != g_hookedFunctions.end()) @@ -100,27 +111,29 @@ namespace return; } + char origFuncPtrStr[20] = {}; + if (!funcName) + { + sprintf_s(origFuncPtrStr, "%p", origFuncPtr); + funcName = origFuncPtrStr; + } + void* const hookedFuncPtr = origFuncPtr; + HMODULE module = nullptr; + GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, + static_cast(hookedFuncPtr), &module); + + Compat::LogDebug() << "Hooking function: " << funcName << " (" << funcAddrToStr(hookedFuncPtr) << ')'; DetourTransactionBegin(); const bool attachSuccessful = NO_ERROR == DetourAttach(&origFuncPtr, newFuncPtr); const bool commitSuccessful = NO_ERROR == DetourTransactionCommit(); if (!attachSuccessful || !commitSuccessful) { - if (funcName) - { - Compat::LogDebug() << "ERROR: Failed to hook a function: " << funcName; - } - else - { - Compat::LogDebug() << "ERROR: Failed to hook a function: " << origFuncPtr; - } + Compat::LogDebug() << "ERROR: Failed to hook a function: " << funcName; return; } - HMODULE module = nullptr; - GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, - reinterpret_cast(hookedFuncPtr), &module); g_hookedFunctions.emplace( std::make_pair(hookedFuncPtr, HookedFunctionInfo{ module, origFuncPtr, newFuncPtr })); } @@ -155,13 +168,13 @@ namespace Compat continue; } - std::string moduleBaseName(getModuleBaseName(module)); + std::string moduleBaseName(getModulePath(module).filename().string()); if (0 != _stricmp(moduleBaseName.c_str(), moduleName)) { Compat::Log() << "Disabling external hook to " << funcName << " in " << moduleBaseName; static std::list origFuncs; origFuncs.push_back(hookFunc); - hookFunction(origFuncs.back(), newFunc); + hookFunction(origFuncs.back(), newFunc, funcName); } } } @@ -293,9 +306,9 @@ namespace Compat return proc ? *proc : nullptr; } - void hookFunction(void*& origFuncPtr, void* newFuncPtr) + void hookFunction(void*& origFuncPtr, void* newFuncPtr, const char* funcName) { - ::hookFunction(nullptr, origFuncPtr, newFuncPtr); + ::hookFunction(origFuncPtr, newFuncPtr, funcName); } void hookFunction(HMODULE module, const char* funcName, void*& origFuncPtr, void* newFuncPtr) @@ -308,7 +321,7 @@ namespace Compat } origFuncPtr = procAddr; - ::hookFunction(funcName, origFuncPtr, newFuncPtr); + ::hookFunction(origFuncPtr, newFuncPtr, funcName); } void hookFunction(const char* moduleName, const char* funcName, void*& origFuncPtr, void* newFuncPtr) @@ -327,7 +340,7 @@ namespace Compat FARPROC* func = findProcAddressInIat(module, importedModuleName, funcName); if (func) { - Compat::LogDebug() << "Hooking function via IAT: " << funcName; + Compat::LogDebug() << "Hooking function via IAT: " << funcName << " (" << funcAddrToStr(*func) << ')'; DWORD oldProtect = 0; VirtualProtect(func, sizeof(func), PAGE_READWRITE, &oldProtect); *func = static_cast(newFuncPtr); diff --git a/DDrawCompat/Common/Hook.h b/DDrawCompat/Common/Hook.h index ff8e3b7..fd21186 100644 --- a/DDrawCompat/Common/Hook.h +++ b/DDrawCompat/Common/Hook.h @@ -10,7 +10,7 @@ Compat::hookFunction(#module, #func, &newFunc) #define HOOK_SHIM_FUNCTION(func, newFunc) \ Compat::hookFunction( \ - reinterpret_cast(Compat::getOrigFuncPtr()), newFunc); + reinterpret_cast(Compat::getOrigFuncPtr()), newFunc, #func); namespace Compat @@ -27,7 +27,7 @@ namespace Compat FARPROC* findProcAddressInIat(HMODULE module, const char* importedModuleName, const char* procName); FARPROC getProcAddress(HMODULE module, const char* procName); FARPROC getProcAddressFromIat(HMODULE module, const char* importedModuleName, const char* procName); - void hookFunction(void*& origFuncPtr, void* newFuncPtr); + void hookFunction(void*& origFuncPtr, void* newFuncPtr, const char* funcName); void hookFunction(HMODULE module, const char* funcName, void*& origFuncPtr, void* newFuncPtr); void hookFunction(const char* moduleName, const char* funcName, void*& origFuncPtr, void* newFuncPtr); void hookIatFunction(HMODULE module, const char* importedModuleName, const char* funcName, void* newFuncPtr); diff --git a/DDrawCompat/Common/VtableHookVisitor.h b/DDrawCompat/Common/VtableHookVisitor.h index 4d53164..28e263f 100644 --- a/DDrawCompat/Common/VtableHookVisitor.h +++ b/DDrawCompat/Common/VtableHookVisitor.h @@ -42,9 +42,9 @@ public: m_origVtable.*ptr = m_srcVtable.*ptr; if (m_origVtable.*ptr && s_compatVtable.*ptr) { - Compat::LogDebug() << "Hooking function: " << FuncNameVisitor::getFuncName(); Compat::hookFunction(reinterpret_cast(m_origVtable.*ptr), - getThreadSafeFuncPtr(s_compatVtable.*ptr)); + getThreadSafeFuncPtr(s_compatVtable.*ptr), + FuncNameVisitor::getFuncName()); } } diff --git a/DDrawCompat/DDraw/ActivateAppHandler.cpp b/DDrawCompat/DDraw/ActivateAppHandler.cpp index 7e23b3b..7506537 100644 --- a/DDrawCompat/DDraw/ActivateAppHandler.cpp +++ b/DDrawCompat/DDraw/ActivateAppHandler.cpp @@ -74,7 +74,7 @@ namespace DDraw if ((flags & DDSCL_FULLSCREEN) && !isDdWndProcHooked) { g_origDdWndProc = reinterpret_cast(GetWindowLongPtr(hwnd, GWLP_WNDPROC)); - Compat::hookFunction(reinterpret_cast(g_origDdWndProc), ddWndProc); + Compat::hookFunction(reinterpret_cast(g_origDdWndProc), ddWndProc, "ddWndProc"); isDdWndProcHooked = true; } } diff --git a/DDrawCompat/Gdi/PaintHandlers.cpp b/DDrawCompat/Gdi/PaintHandlers.cpp index cd69030..573e5f1 100644 --- a/DDrawCompat/Gdi/PaintHandlers.cpp +++ b/DDrawCompat/Gdi/PaintHandlers.cpp @@ -86,7 +86,8 @@ namespace g_currentUser32WndProc->oldWndProc = wndProc; g_currentUser32WndProc->oldWndProcTrampoline = wndProc; Compat::hookFunction(reinterpret_cast(g_currentUser32WndProc->oldWndProcTrampoline), - g_currentUser32WndProc->newWndProc); + g_currentUser32WndProc->newWndProc, + g_currentUser32WndProc->procName.c_str()); } } }