From f096a2d8a234dfd2566ce93e0d2b12c3fe117c58 Mon Sep 17 00:00:00 2001 From: Nemirtingas Date: Sun, 18 Aug 2019 16:19:28 +0200 Subject: [PATCH] Use LoadLibrary instead of LoadModule LoadLibrary increase the reference count to the library. So we make sure this library is still loaded when we shut down the hook. --- overlay_experimental/Base_Hook.cpp | 4 ++++ overlay_experimental/Base_Hook.h | 6 +----- overlay_experimental/DX10_Hook.cpp | 7 ++++--- overlay_experimental/DX11_Hook.cpp | 7 ++++--- overlay_experimental/DX12_Hook.cpp | 7 ++++--- overlay_experimental/DX9_Hook.cpp | 9 ++++++--- overlay_experimental/OpenGL_Hook.cpp | 11 ++++------- 7 files changed, 27 insertions(+), 24 deletions(-) diff --git a/overlay_experimental/Base_Hook.cpp b/overlay_experimental/Base_Hook.cpp index b7913e5..a7d3b88 100644 --- a/overlay_experimental/Base_Hook.cpp +++ b/overlay_experimental/Base_Hook.cpp @@ -7,6 +7,10 @@ #include "../detours/detours.h" +#define WIN32_LEAN_AND_MEAN +#define VC_EXTRALEAN +#include + Base_Hook::Base_Hook(): _hooked(false) {} diff --git a/overlay_experimental/Base_Hook.h b/overlay_experimental/Base_Hook.h index 6b5443b..bb0c2dd 100644 --- a/overlay_experimental/Base_Hook.h +++ b/overlay_experimental/Base_Hook.h @@ -5,10 +5,6 @@ #ifndef NO_OVERLAY -#define WIN32_LEAN_AND_MEAN -#define VC_EXTRALEAN -#include - #include #include @@ -17,7 +13,7 @@ class Base_Hook protected: std::vector> _hooked_funcs; - HMODULE _dll; + void* _library; bool _hooked; Base_Hook(const Base_Hook&) = delete; diff --git a/overlay_experimental/DX10_Hook.cpp b/overlay_experimental/DX10_Hook.cpp index 3897902..21fae0b 100644 --- a/overlay_experimental/DX10_Hook.cpp +++ b/overlay_experimental/DX10_Hook.cpp @@ -166,7 +166,7 @@ DX10_Hook::DX10_Hook(): pDevice(nullptr), mainRenderTargetView(nullptr) { - _dll = GetModuleHandle(DLL_NAME); + _library = LoadLibrary(DLL_NAME); // Hook to D3D10CreateDevice and D3D10CreateDeviceAndSwapChain so we know when it gets called. // If its called, then DX10 will be used to render the overlay. @@ -185,8 +185,9 @@ DX10_Hook::~DX10_Hook() { PRINT_DEBUG("DX10 Hook removed\n"); - if (_hooked) - resetRenderState(); + resetRenderState(); + + FreeLibrary(reinterpret_cast(_library)); _inst = nullptr; } diff --git a/overlay_experimental/DX11_Hook.cpp b/overlay_experimental/DX11_Hook.cpp index 2bde2c0..f7b1ef5 100644 --- a/overlay_experimental/DX11_Hook.cpp +++ b/overlay_experimental/DX11_Hook.cpp @@ -184,7 +184,7 @@ DX11_Hook::DX11_Hook(): pContext(nullptr), mainRenderTargetView(nullptr) { - _dll = GetModuleHandle(DLL_NAME); + _library = LoadLibrary(DLL_NAME); // Hook to D3D11CreateDevice and D3D11CreateDeviceAndSwapChain so we know when it gets called. // If its called, then DX11 will be used to render the overlay. @@ -203,8 +203,9 @@ DX11_Hook::~DX11_Hook() { PRINT_DEBUG("DX11 Hook removed\n"); - if (_hooked) - resetRenderState(); + resetRenderState(); + + FreeLibrary(reinterpret_cast(_library)); _inst = nullptr; } diff --git a/overlay_experimental/DX12_Hook.cpp b/overlay_experimental/DX12_Hook.cpp index 6f3951a..0696994 100644 --- a/overlay_experimental/DX12_Hook.cpp +++ b/overlay_experimental/DX12_Hook.cpp @@ -137,7 +137,7 @@ DX12_Hook::DX12_Hook(): pCmdList(nullptr), pDescriptorHeap(nullptr) { - _dll = GetModuleHandle(DLL_NAME); + _library = GetModuleHandle(DLL_NAME); PRINT_DEBUG("Trying to hook DX12 but DX12_Hook is not implemented yet, please report to DEV with the game name."); @@ -156,8 +156,9 @@ DX12_Hook::~DX12_Hook() { PRINT_DEBUG("DX12 Hook removed\n"); - if (_hooked) - resetRenderState(); + resetRenderState(); + + FreeLibrary(reinterpret_cast(_library)); _inst = nullptr; } diff --git a/overlay_experimental/DX9_Hook.cpp b/overlay_experimental/DX9_Hook.cpp index 26285c3..a79771d 100644 --- a/overlay_experimental/DX9_Hook.cpp +++ b/overlay_experimental/DX9_Hook.cpp @@ -20,7 +20,7 @@ bool DX9_Hook::start_hook() IDirect3D9Ex* pD3D; IDirect3DDevice9Ex* pDeviceEx; - decltype(Direct3DCreate9Ex)* Direct3DCreate9Ex = (decltype(Direct3DCreate9Ex))GetProcAddress(_dll, "Direct3DCreate9Ex"); + decltype(Direct3DCreate9Ex)* Direct3DCreate9Ex = (decltype(Direct3DCreate9Ex))GetProcAddress(reinterpret_cast(_library), "Direct3DCreate9Ex"); Direct3DCreate9Ex(D3D_SDK_VERSION, &pD3D); @@ -171,7 +171,7 @@ DX9_Hook::DX9_Hook(): PresentEx(nullptr), Reset(nullptr) { - _dll = GetModuleHandle(DLL_NAME); + _library = LoadLibrary(DLL_NAME); // Hook to Direct3DCreate9 and Direct3DCreate9Ex so we know when it gets called. // If its called, then DX9 will be used to render the overlay. //Direct3DCreate9 = (decltype(Direct3DCreate9))GetProcAddress(_dll, "Direct3DCreate9"); @@ -189,7 +189,8 @@ DX9_Hook::~DX9_Hook() { PRINT_DEBUG("DX9 Hook removed\n"); - if (_hooked) + //resetRenderState(); + if (initialized) { //ImGui_ImplDX9_Shutdown(); This makes some games hang when Releasing the D3D9 device (pDevice->Release()) // maybe because D3D is already shut down when we try to free the device? @@ -198,6 +199,8 @@ DX9_Hook::~DX9_Hook() ImGui::DestroyContext(); } + FreeLibrary(reinterpret_cast(_library)); + _inst = nullptr; } diff --git a/overlay_experimental/OpenGL_Hook.cpp b/overlay_experimental/OpenGL_Hook.cpp index c326ab2..868e99a 100644 --- a/overlay_experimental/OpenGL_Hook.cpp +++ b/overlay_experimental/OpenGL_Hook.cpp @@ -115,7 +115,7 @@ OpenGL_Hook::OpenGL_Hook(): initialized(false), wglSwapBuffers(nullptr) { - _dll = GetModuleHandle(DLL_NAME); + _library = LoadLibrary(DLL_NAME); // Hook to wglMakeCurrent so we know when it gets called. // If its called, then OpenGL will be used to render the overlay. //wglMakeCurrent = (decltype(wglMakeCurrent))GetProcAddress(_dll, "wglMakeCurrent"); @@ -132,12 +132,9 @@ OpenGL_Hook::~OpenGL_Hook() { PRINT_DEBUG("OpenGL Hook removed\n"); - if (_hooked) - { - ImGui_ImplOpenGL3_Shutdown(); - ImGui_ImplWin32_Shutdown(); - ImGui::DestroyContext(); - } + resetRenderState(); + + FreeLibrary(reinterpret_cast(_library)); _inst = nullptr; }