From 3bc8fffe81409f5e7ca93e28ddc62e91d89dc7c9 Mon Sep 17 00:00:00 2001 From: FunkyFr3sh Date: Tue, 11 May 2021 21:45:38 +0200 Subject: [PATCH] improve dinput hooks with hook=3 --- inc/directinput.h | 14 ++++++++++++++ src/directinput.c | 28 +++++++++++++++------------- src/hook.c | 46 ++++++++++++++++++++++++++++++++++------------ 3 files changed, 63 insertions(+), 25 deletions(-) diff --git a/inc/directinput.h b/inc/directinput.h index 50f251d..c23ff7f 100644 --- a/inc/directinput.h +++ b/inc/directinput.h @@ -1,7 +1,21 @@ #ifndef DINPUTINPUT_H #define DINPUTINPUT_H +#define DIRECTINPUT_VERSION 0x0800 +#include + +typedef HRESULT(WINAPI* DIRECTINPUTCREATEAPROC)(HINSTANCE, DWORD, LPDIRECTINPUTA*, LPUNKNOWN); +typedef HRESULT(WINAPI* DIRECTINPUT8CREATEPROC)(HINSTANCE, DWORD, REFIID, LPDIRECTINPUT8*, LPUNKNOWN); +typedef HRESULT(WINAPI* DICREATEDEVICEPROC)(IDirectInputA*, REFGUID, LPDIRECTINPUTDEVICEA*, LPUNKNOWN); +typedef HRESULT(WINAPI* DIDSETCOOPERATIVELEVELPROC)(IDirectInputDeviceA*, HWND, DWORD); +typedef HRESULT(WINAPI* DIDGETDEVICEDATAPROC)(IDirectInputDeviceA*, DWORD, LPDIDEVICEOBJECTDATA, LPDWORD, DWORD); + +extern DIRECTINPUTCREATEAPROC real_DirectInputCreateA; +extern DIRECTINPUT8CREATEPROC real_DirectInput8Create; + void dinput_hook(); void dinput_unhook(); +HRESULT WINAPI fake_DirectInputCreateA(HINSTANCE hinst, DWORD dwVersion, LPDIRECTINPUTA* lplpDirectInput, LPUNKNOWN punkOuter); +HRESULT WINAPI fake_DirectInput8Create(HINSTANCE hinst, DWORD dwVersion, REFIID riidltf, LPDIRECTINPUT8* ppvOut, LPUNKNOWN punkOuter); #endif diff --git a/src/directinput.c b/src/directinput.c index 22a9201..6eff8e1 100644 --- a/src/directinput.c +++ b/src/directinput.c @@ -1,18 +1,14 @@ #include -#include +#include "directinput.h" #include "debug.h" #include "hook.h" #include "dd.h" -typedef HRESULT (WINAPI *DIRECTINPUTCREATEAPROC)(HINSTANCE, DWORD, LPDIRECTINPUTA*, LPUNKNOWN); -typedef HRESULT (WINAPI *DIRECTINPUT8CREATEPROC)(HINSTANCE, DWORD, REFIID, LPDIRECTINPUT8*, LPUNKNOWN); -typedef HRESULT (WINAPI *DICREATEDEVICEPROC)(IDirectInputA*, REFGUID, LPDIRECTINPUTDEVICEA *, LPUNKNOWN); -typedef HRESULT (WINAPI *DIDSETCOOPERATIVELEVELPROC)(IDirectInputDeviceA *, HWND, DWORD); -typedef HRESULT (WINAPI *DIDGETDEVICEDATAPROC)(IDirectInputDeviceA*, DWORD, LPDIDEVICEOBJECTDATA, LPDWORD, DWORD); -static DIRECTINPUTCREATEAPROC real_DirectInputCreateA; -static DIRECTINPUT8CREATEPROC real_DirectInput8Create; +DIRECTINPUTCREATEAPROC real_DirectInputCreateA; +DIRECTINPUT8CREATEPROC real_DirectInput8Create; + static DICREATEDEVICEPROC real_di_CreateDevice; static DIDSETCOOPERATIVELEVELPROC real_did_SetCooperativeLevel; static DIDGETDEVICEDATAPROC real_did_GetDeviceData; @@ -68,12 +64,15 @@ static HRESULT WINAPI fake_di_CreateDevice(IDirectInputA *This, REFGUID rguid, L return result; } -static HRESULT WINAPI fake_DirectInputCreateA(HINSTANCE hinst, DWORD dwVersion, LPDIRECTINPUTA* lplpDirectInput, LPUNKNOWN punkOuter) +HRESULT WINAPI fake_DirectInputCreateA(HINSTANCE hinst, DWORD dwVersion, LPDIRECTINPUTA* lplpDirectInput, LPUNKNOWN punkOuter) { dprintf("DirectInputCreateA\n"); - real_DirectInputCreateA = - (DIRECTINPUTCREATEAPROC)GetProcAddress(GetModuleHandle("dinput.dll"), "DirectInputCreateA"); + if (!real_DirectInputCreateA) + { + real_DirectInputCreateA = + (DIRECTINPUTCREATEAPROC)GetProcAddress(GetModuleHandle("dinput.dll"), "DirectInputCreateA"); + } if (!real_DirectInputCreateA) return DIERR_GENERIC; @@ -93,8 +92,11 @@ HRESULT WINAPI fake_DirectInput8Create(HINSTANCE hinst, DWORD dwVersion, REFIID { dprintf("DirectInput8Create\n"); - real_DirectInput8Create = - (DIRECTINPUT8CREATEPROC)GetProcAddress(GetModuleHandle("dinput8.dll"), "DirectInput8Create"); + if (!real_DirectInput8Create) + { + real_DirectInput8Create = + (DIRECTINPUT8CREATEPROC)GetProcAddress(GetModuleHandle("dinput8.dll"), "DirectInput8Create"); + } if (!real_DirectInput8Create) return DIERR_GENERIC; diff --git a/src/hook.c b/src/hook.c index bddab51..14483f4 100644 --- a/src/hook.c +++ b/src/hook.c @@ -2,6 +2,7 @@ #include #include #include +#include "directinput.h" #include "dd.h" #include "winapi_hooks.h" #include "hook.h" @@ -367,21 +368,31 @@ void hook_init() #ifdef _MSC_VER if (!g_hook_active && g_hook_method == 3) { - FARPROC proc = GetProcAddress(GetModuleHandle("kernelbase.dll"), "LoadLibraryExW"); + real_DirectInputCreateA = (DIRECTINPUTCREATEAPROC)GetProcAddress(LoadLibraryA("dinput.dll"), "DirectInputCreateA"); - if (proc) - real_LoadLibraryExW = (LOADLIBRARYEXWPROC)proc; + if (real_DirectInputCreateA) + { + DetourTransactionBegin(); + DetourUpdateThread(GetCurrentThread()); + DetourAttach((PVOID*)&real_DirectInputCreateA, (PVOID)fake_DirectInputCreateA); + DetourTransactionCommit(); + } - DetourTransactionBegin(); - DetourUpdateThread(GetCurrentThread()); - DetourAttach((PVOID*)&real_LoadLibraryExW, (PVOID)fake_LoadLibraryExW); - DetourTransactionCommit(); + real_DirectInput8Create = (DIRECTINPUT8CREATEPROC)GetProcAddress(LoadLibraryA("dinput8.dll"), "DirectInput8Create"); + + if (real_DirectInput8Create) + { + DetourTransactionBegin(); + DetourUpdateThread(GetCurrentThread()); + DetourAttach((PVOID*)&real_DirectInput8Create, (PVOID)fake_DirectInput8Create); + DetourTransactionCommit(); + } } #endif g_hook_active = TRUE; - if (g_hook_method == 4) + if (g_hook_method == 3 || g_hook_method == 4) { for (int i = 0; g_hooks[i].module_name[0]; i++) { @@ -405,10 +416,21 @@ void hook_exit() #ifdef _MSC_VER if (g_hook_method == 3) { - DetourTransactionBegin(); - DetourUpdateThread(GetCurrentThread()); - DetourDetach((PVOID*)&real_LoadLibraryExW, (PVOID)fake_LoadLibraryExW); - DetourTransactionCommit(); + if (real_DirectInputCreateA) + { + DetourTransactionBegin(); + DetourUpdateThread(GetCurrentThread()); + DetourDetach((PVOID*)&real_DirectInputCreateA, (PVOID)fake_DirectInputCreateA); + DetourTransactionCommit(); + } + + if (real_DirectInput8Create) + { + DetourTransactionBegin(); + DetourUpdateThread(GetCurrentThread()); + DetourDetach((PVOID*)&real_DirectInput8Create, (PVOID)fake_DirectInput8Create); + DetourTransactionCommit(); + } } #endif