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.
merge-requests/28/head
Nemirtingas 2019-08-18 16:19:28 +02:00
parent fdeb5912d3
commit f096a2d8a2
7 changed files with 27 additions and 24 deletions

View File

@ -7,6 +7,10 @@
#include "../detours/detours.h" #include "../detours/detours.h"
#define WIN32_LEAN_AND_MEAN
#define VC_EXTRALEAN
#include <Windows.h>
Base_Hook::Base_Hook(): Base_Hook::Base_Hook():
_hooked(false) _hooked(false)
{} {}

View File

@ -5,10 +5,6 @@
#ifndef NO_OVERLAY #ifndef NO_OVERLAY
#define WIN32_LEAN_AND_MEAN
#define VC_EXTRALEAN
#include <Windows.h>
#include <vector> #include <vector>
#include <utility> #include <utility>
@ -17,7 +13,7 @@ class Base_Hook
protected: protected:
std::vector<std::pair<void**, void*>> _hooked_funcs; std::vector<std::pair<void**, void*>> _hooked_funcs;
HMODULE _dll; void* _library;
bool _hooked; bool _hooked;
Base_Hook(const Base_Hook&) = delete; Base_Hook(const Base_Hook&) = delete;

View File

@ -166,7 +166,7 @@ DX10_Hook::DX10_Hook():
pDevice(nullptr), pDevice(nullptr),
mainRenderTargetView(nullptr) mainRenderTargetView(nullptr)
{ {
_dll = GetModuleHandle(DLL_NAME); _library = LoadLibrary(DLL_NAME);
// Hook to D3D10CreateDevice and D3D10CreateDeviceAndSwapChain so we know when it gets called. // Hook to D3D10CreateDevice and D3D10CreateDeviceAndSwapChain so we know when it gets called.
// If its called, then DX10 will be used to render the overlay. // 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"); PRINT_DEBUG("DX10 Hook removed\n");
if (_hooked) resetRenderState();
resetRenderState();
FreeLibrary(reinterpret_cast<HMODULE>(_library));
_inst = nullptr; _inst = nullptr;
} }

View File

@ -184,7 +184,7 @@ DX11_Hook::DX11_Hook():
pContext(nullptr), pContext(nullptr),
mainRenderTargetView(nullptr) mainRenderTargetView(nullptr)
{ {
_dll = GetModuleHandle(DLL_NAME); _library = LoadLibrary(DLL_NAME);
// Hook to D3D11CreateDevice and D3D11CreateDeviceAndSwapChain so we know when it gets called. // Hook to D3D11CreateDevice and D3D11CreateDeviceAndSwapChain so we know when it gets called.
// If its called, then DX11 will be used to render the overlay. // 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"); PRINT_DEBUG("DX11 Hook removed\n");
if (_hooked) resetRenderState();
resetRenderState();
FreeLibrary(reinterpret_cast<HMODULE>(_library));
_inst = nullptr; _inst = nullptr;
} }

View File

@ -137,7 +137,7 @@ DX12_Hook::DX12_Hook():
pCmdList(nullptr), pCmdList(nullptr),
pDescriptorHeap(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."); 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"); PRINT_DEBUG("DX12 Hook removed\n");
if (_hooked) resetRenderState();
resetRenderState();
FreeLibrary(reinterpret_cast<HMODULE>(_library));
_inst = nullptr; _inst = nullptr;
} }

View File

@ -20,7 +20,7 @@ bool DX9_Hook::start_hook()
IDirect3D9Ex* pD3D; IDirect3D9Ex* pD3D;
IDirect3DDevice9Ex* pDeviceEx; IDirect3DDevice9Ex* pDeviceEx;
decltype(Direct3DCreate9Ex)* Direct3DCreate9Ex = (decltype(Direct3DCreate9Ex))GetProcAddress(_dll, "Direct3DCreate9Ex"); decltype(Direct3DCreate9Ex)* Direct3DCreate9Ex = (decltype(Direct3DCreate9Ex))GetProcAddress(reinterpret_cast<HMODULE>(_library), "Direct3DCreate9Ex");
Direct3DCreate9Ex(D3D_SDK_VERSION, &pD3D); Direct3DCreate9Ex(D3D_SDK_VERSION, &pD3D);
@ -171,7 +171,7 @@ DX9_Hook::DX9_Hook():
PresentEx(nullptr), PresentEx(nullptr),
Reset(nullptr) Reset(nullptr)
{ {
_dll = GetModuleHandle(DLL_NAME); _library = LoadLibrary(DLL_NAME);
// Hook to Direct3DCreate9 and Direct3DCreate9Ex so we know when it gets called. // Hook to Direct3DCreate9 and Direct3DCreate9Ex so we know when it gets called.
// If its called, then DX9 will be used to render the overlay. // If its called, then DX9 will be used to render the overlay.
//Direct3DCreate9 = (decltype(Direct3DCreate9))GetProcAddress(_dll, "Direct3DCreate9"); //Direct3DCreate9 = (decltype(Direct3DCreate9))GetProcAddress(_dll, "Direct3DCreate9");
@ -189,7 +189,8 @@ DX9_Hook::~DX9_Hook()
{ {
PRINT_DEBUG("DX9 Hook removed\n"); 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()) //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? // maybe because D3D is already shut down when we try to free the device?
@ -198,6 +199,8 @@ DX9_Hook::~DX9_Hook()
ImGui::DestroyContext(); ImGui::DestroyContext();
} }
FreeLibrary(reinterpret_cast<HMODULE>(_library));
_inst = nullptr; _inst = nullptr;
} }

View File

@ -115,7 +115,7 @@ OpenGL_Hook::OpenGL_Hook():
initialized(false), initialized(false),
wglSwapBuffers(nullptr) wglSwapBuffers(nullptr)
{ {
_dll = GetModuleHandle(DLL_NAME); _library = LoadLibrary(DLL_NAME);
// Hook to wglMakeCurrent so we know when it gets called. // Hook to wglMakeCurrent so we know when it gets called.
// If its called, then OpenGL will be used to render the overlay. // If its called, then OpenGL will be used to render the overlay.
//wglMakeCurrent = (decltype(wglMakeCurrent))GetProcAddress(_dll, "wglMakeCurrent"); //wglMakeCurrent = (decltype(wglMakeCurrent))GetProcAddress(_dll, "wglMakeCurrent");
@ -132,12 +132,9 @@ OpenGL_Hook::~OpenGL_Hook()
{ {
PRINT_DEBUG("OpenGL Hook removed\n"); PRINT_DEBUG("OpenGL Hook removed\n");
if (_hooked) resetRenderState();
{
ImGui_ImplOpenGL3_Shutdown(); FreeLibrary(reinterpret_cast<HMODULE>(_library));
ImGui_ImplWin32_Shutdown();
ImGui::DestroyContext();
}
_inst = nullptr; _inst = nullptr;
} }