From dfb34d1508e90482f2e1cd29f592485e48776f9e Mon Sep 17 00:00:00 2001 From: _Sakura <97910292+0x-FADED@users.noreply.github.com> Date: Wed, 21 Aug 2024 08:48:37 +0900 Subject: [PATCH] fix sometimes d3d11 hook is being created before the game even loads the dll --- IHHook/D3D11Hook.cpp | 13 ++++++++++--- IHHook/IHHook.cpp | 33 ++++++++++++++++++++++++++++++++- IHHook/IHHook.h | 1 + IHHook/dllmain.cpp | 5 +++++ 4 files changed, 48 insertions(+), 4 deletions(-) diff --git a/IHHook/D3D11Hook.cpp b/IHHook/D3D11Hook.cpp index 8be2260..df77f46 100644 --- a/IHHook/D3D11Hook.cpp +++ b/IHHook/D3D11Hook.cpp @@ -16,15 +16,22 @@ D3D11Hook::~D3D11Hook() { bool D3D11Hook::hook() { spdlog::info("Hooking D3D11"); + auto pD3D11CreateDeviceAndSwapChain = (decltype(&::D3D11CreateDeviceAndSwapChain)) GetProcAddress(GetModuleHandleW(L"d3d11.dll"), "D3D11CreateDeviceAndSwapChain"); + + if (pD3D11CreateDeviceAndSwapChain == nullptr){ + spdlog::error("d3d11.dll is not loaded can't hook d3d11 functions"); + + return m_hooked = false; + } g_d3d11_hook = this; HWND h_wnd = GetDesktopWindow(); IDXGISwapChain* swap_chain = nullptr; ID3D11Device* device = nullptr; - D3D_FEATURE_LEVEL device_max_feature_level = D3D_FEATURE_LEVEL_9_1; + D3D_FEATURE_LEVEL device_max_feature_level; ID3D11DeviceContext* context = nullptr; - D3D_FEATURE_LEVEL feature_level = D3D_FEATURE_LEVEL_11_0; + D3D_FEATURE_LEVEL feature_level[] = {D3D_FEATURE_LEVEL_10_1, D3D_FEATURE_LEVEL_11_0}; DXGI_SWAP_CHAIN_DESC swap_chain_desc; ZeroMemory(&swap_chain_desc, sizeof(swap_chain_desc)); @@ -39,7 +46,7 @@ bool D3D11Hook::hook() { swap_chain_desc.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED; swap_chain_desc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD; spdlog::info("Creating dummy D3D11 device."); - HRESULT hr = D3D11CreateDeviceAndSwapChain(nullptr, D3D_DRIVER_TYPE_NULL, nullptr, 0, &feature_level, 1, D3D11_SDK_VERSION, &swap_chain_desc, &swap_chain, &device, &device_max_feature_level, &context); + HRESULT hr = pD3D11CreateDeviceAndSwapChain(nullptr, D3D_DRIVER_TYPE_NULL, nullptr, 0, feature_level, 1, D3D11_SDK_VERSION, &swap_chain_desc, &swap_chain, &device, &device_max_feature_level, &context); if (FAILED(hr)) { spdlog::error("Failed to create dummy D3D11 device. HRESULT={0:x} max_feature={1:x}", hr, device_max_feature_level); return false; diff --git a/IHHook/IHHook.cpp b/IHHook/IHHook.cpp index 7785888..f29344d 100644 --- a/IHHook/IHHook.cpp +++ b/IHHook/IHHook.cpp @@ -131,6 +131,36 @@ namespace IHHook { } }//InitCursorHook + typedef HMODULE(WINAPI* LoadLibraryAFn)(LPCSTR); +LoadLibraryAFn g_LoadLibraryA = NULL; + +bool IHH::isD3D11Loaded = false; +HMODULE WINAPI LoadLibraryA_Hook(LPCSTR lpFileName) +{ + if (strcmp(lpFileName, "d3d11.dll") == 0) + { + spdlog::info("LoadLibraryA_Hook: {:} is loaded", lpFileName); + g_ihhook->isD3D11Loaded = true; + } + + return g_LoadLibraryA(lpFileName); +} + +void InitLoadLibraryAHook() +{ + auto log = spdlog::get("ihhook"); + + if (MH_CreateHook(&LoadLibraryA, &LoadLibraryA_Hook, reinterpret_cast(&g_LoadLibraryA)) != MH_OK) + { + log->error("Couldn't create hook for LoadLibraryA."); + } + + if (MH_EnableHook(&LoadLibraryA) != MH_OK) + { + log->error("Couldn't enable LoadLibraryA hook."); + } +} + void Shutdown() { spdlog::debug("IHHook DLL_PROCESS_DETACH"); doShutDown = true; @@ -303,7 +333,7 @@ namespace IHHook { auto tend = std::chrono::high_resolution_clock::now(); auto durationShort = std::chrono::duration_cast(tend - tstart).count(); - spdlog::debug("IHHook::CreateHooks total time(microseconds): {}µs", durationShort); + spdlog::debug("IHHook::CreateHooks total time(microseconds): {}�s", durationShort); }//if doHooks PipeServer::StartPipeServer(); @@ -868,6 +898,7 @@ namespace IHHook { }//RebaseAddresses void IHH::CreateAllHooks() { + InitLoadLibraryAHook(); Hooks_CityHash::CreateHooks(RealBaseAddr);//TODO: rebase/convert to same style as rest, so don't have to pass in realbaseaddr Hooks_FNVHash::CreateHooks(); Hooks_Lua::CreateHooks(); diff --git a/IHHook/IHHook.h b/IHHook/IHHook.h index 189c151..7dc413c 100644 --- a/IHHook/IHHook.h +++ b/IHHook/IHHook.h @@ -143,6 +143,7 @@ namespace IHHook { void OnReset(); bool OnMessage(HWND wnd, UINT message, WPARAM w_param, LPARAM l_param); + static bool isD3D11Loaded; private: void SetupLog(); std::string GetLangVersion(); diff --git a/IHHook/dllmain.cpp b/IHHook/dllmain.cpp index f2db52a..a327407 100644 --- a/IHHook/dllmain.cpp +++ b/IHHook/dllmain.cpp @@ -12,7 +12,12 @@ HMODULE g_thisModule; extern HMODULE origDll;//dinputproxy DWORD WINAPI InitThread(LPVOID lpParameter) { + + if (g_ihhook->isD3D11Loaded == true){ + g_ihhook->Initialize(); + + } return 0; }//InitThread