From c5c47a5400b7c5a71b94a75153d19622d4d96be9 Mon Sep 17 00:00:00 2001 From: FunkyFr3sh Date: Sun, 23 May 2021 14:55:36 +0200 Subject: [PATCH] hook all dinput function --- inc/directinput.h | 7 ++++ src/directinput.c | 87 +++++++++++++++++++++++++++++++++++++++++++++++ src/hook.c | 40 ++++++++++++++++++++++ 3 files changed, 134 insertions(+) diff --git a/inc/directinput.h b/inc/directinput.h index 0940fb1..9b1d270 100644 --- a/inc/directinput.h +++ b/inc/directinput.h @@ -5,15 +5,22 @@ #include typedef HRESULT(WINAPI* DIRECTINPUTCREATEAPROC)(HINSTANCE, DWORD, LPDIRECTINPUTA*, LPUNKNOWN); +typedef HRESULT(WINAPI* DIRECTINPUTCREATEWPROC)(HINSTANCE, DWORD, LPDIRECTINPUTW*, LPUNKNOWN); +typedef HRESULT(WINAPI* DIRECTINPUTCREATEEXPROC)(HINSTANCE, DWORD, REFIID, LPDIRECTINPUT7A*, LPUNKNOWN); typedef HRESULT(WINAPI* DIRECTINPUT8CREATEPROC)(HINSTANCE, DWORD, REFIID, LPDIRECTINPUT8*, LPUNKNOWN); typedef HRESULT(WINAPI* DICREATEDEVICEPROC)(IDirectInputA*, REFGUID, LPDIRECTINPUTDEVICEA*, LPUNKNOWN); +typedef HRESULT(WINAPI* DICREATEDEVICEEXPROC)(IDirectInputA*, REFGUID, REFIID, LPDIRECTINPUTDEVICEA*, LPUNKNOWN); typedef HRESULT(WINAPI* DIDSETCOOPERATIVELEVELPROC)(IDirectInputDeviceA*, HWND, DWORD); typedef HRESULT(WINAPI* DIDGETDEVICEDATAPROC)(IDirectInputDeviceA*, DWORD, LPDIDEVICEOBJECTDATA, LPDWORD, DWORD); extern DIRECTINPUTCREATEAPROC real_DirectInputCreateA; +extern DIRECTINPUTCREATEWPROC real_DirectInputCreateW; +extern DIRECTINPUTCREATEEXPROC real_DirectInputCreateEx; extern DIRECTINPUT8CREATEPROC real_DirectInput8Create; HRESULT WINAPI fake_DirectInputCreateA(HINSTANCE hinst, DWORD dwVersion, LPDIRECTINPUTA* lplpDirectInput, LPUNKNOWN punkOuter); +HRESULT WINAPI fake_DirectInputCreateW(HINSTANCE hinst, DWORD dwVersion, LPDIRECTINPUTW* lplpDirectInput, LPUNKNOWN punkOuter); +HRESULT WINAPI fake_DirectInputCreateEx(HINSTANCE hinst, DWORD dwVersion, REFIID riidltf, LPDIRECTINPUT7A* ppvOut, 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 6a62fa5..d5267b4 100644 --- a/src/directinput.c +++ b/src/directinput.c @@ -1,5 +1,6 @@ #include +#include #include "directinput.h" #include "debug.h" #include "hook.h" @@ -7,9 +8,12 @@ DIRECTINPUTCREATEAPROC real_DirectInputCreateA; +DIRECTINPUTCREATEWPROC real_DirectInputCreateW; +DIRECTINPUTCREATEEXPROC real_DirectInputCreateEx; DIRECTINPUT8CREATEPROC real_DirectInput8Create; static DICREATEDEVICEPROC real_di_CreateDevice; +static DICREATEDEVICEEXPROC real_di_CreateDeviceEx; static DIDSETCOOPERATIVELEVELPROC real_did_SetCooperativeLevel; static DIDGETDEVICEDATAPROC real_did_GetDeviceData; @@ -31,11 +35,15 @@ static PROC hook_func(PROC *org_func, PROC new_func) static HRESULT WINAPI fake_did_SetCooperativeLevel(IDirectInputDeviceA *This, HWND hwnd, DWORD dwFlags) { + dprintf("DirectInput SetCooperativeLevel\n"); + return real_did_SetCooperativeLevel(This, hwnd, DISCL_BACKGROUND | DISCL_NONEXCLUSIVE); } static HRESULT WINAPI fake_did_GetDeviceData(IDirectInputDeviceA *This, DWORD cbObjectData, LPDIDEVICEOBJECTDATA rgdod, LPDWORD pdwInOut, DWORD dwFlags) { + //dprintf("DirectInput GetDeviceData\n"); + HRESULT result = real_did_GetDeviceData(This, cbObjectData, rgdod, pdwInOut, dwFlags); if (SUCCEEDED(result) && g_ddraw && !g_ddraw->locked) @@ -48,6 +56,8 @@ static HRESULT WINAPI fake_did_GetDeviceData(IDirectInputDeviceA *This, DWORD cb static HRESULT WINAPI fake_di_CreateDevice(IDirectInputA *This, REFGUID rguid, LPDIRECTINPUTDEVICEA * lplpDIDevice, LPUNKNOWN pUnkOuter) { + dprintf("DirectInput CreateDevice\n"); + HRESULT result = real_di_CreateDevice(This, rguid, lplpDIDevice, pUnkOuter); if (SUCCEEDED(result) && !real_did_SetCooperativeLevel) @@ -64,6 +74,26 @@ static HRESULT WINAPI fake_di_CreateDevice(IDirectInputA *This, REFGUID rguid, L return result; } +static HRESULT WINAPI fake_di_CreateDeviceEx(IDirectInputA* This, REFGUID rguid, REFIID riid, LPDIRECTINPUTDEVICEA* lplpDIDevice, LPUNKNOWN pUnkOuter) +{ + dprintf("DirectInput CreateDeviceEx\n"); + + HRESULT result = real_di_CreateDeviceEx(This, rguid, riid, lplpDIDevice, pUnkOuter); + + if (SUCCEEDED(result) && !real_did_SetCooperativeLevel) + { + real_did_SetCooperativeLevel = + (DIDSETCOOPERATIVELEVELPROC)hook_func( + (PROC*)&(*lplpDIDevice)->lpVtbl->SetCooperativeLevel, (PROC)fake_did_SetCooperativeLevel); + + real_did_GetDeviceData = + (DIDGETDEVICEDATAPROC)hook_func( + (PROC*)&(*lplpDIDevice)->lpVtbl->GetDeviceData, (PROC)fake_did_GetDeviceData); + } + + return result; +} + HRESULT WINAPI fake_DirectInputCreateA(HINSTANCE hinst, DWORD dwVersion, LPDIRECTINPUTA* lplpDirectInput, LPUNKNOWN punkOuter) { dprintf("DirectInputCreateA\n"); @@ -88,6 +118,63 @@ HRESULT WINAPI fake_DirectInputCreateA(HINSTANCE hinst, DWORD dwVersion, LPDIREC return result; } +HRESULT WINAPI fake_DirectInputCreateW(HINSTANCE hinst, DWORD dwVersion, LPDIRECTINPUTW* lplpDirectInput, LPUNKNOWN punkOuter) +{ + dprintf("DirectInputCreateW\n"); + + if (!real_DirectInputCreateW) + { + real_DirectInputCreateW = + (DIRECTINPUTCREATEWPROC)GetProcAddress(GetModuleHandle("dinput.dll"), "DirectInputCreateW"); + } + + if (!real_DirectInputCreateW) + return DIERR_GENERIC; + + HRESULT result = real_DirectInputCreateW(hinst, dwVersion, lplpDirectInput, punkOuter); + + if (SUCCEEDED(result) && !real_di_CreateDevice) + { + real_di_CreateDevice = + (DICREATEDEVICEPROC)hook_func((PROC*)&(*lplpDirectInput)->lpVtbl->CreateDevice, (PROC)fake_di_CreateDevice); + } + + return result; +} + +HRESULT WINAPI fake_DirectInputCreateEx(HINSTANCE hinst, DWORD dwVersion, REFIID riidltf, LPDIRECTINPUT7A* ppvOut, LPUNKNOWN punkOuter) +{ + dprintf("DirectInputCreateEx\n"); + + if (!real_DirectInputCreateEx) + { + real_DirectInputCreateEx = + (DIRECTINPUTCREATEEXPROC)GetProcAddress(GetModuleHandle("dinput.dll"), "DirectInputCreateEx"); + } + + if (!real_DirectInputCreateEx) + return DIERR_GENERIC; + + HRESULT result = real_DirectInputCreateEx(hinst, dwVersion, riidltf, ppvOut, punkOuter); + + if (SUCCEEDED(result) && !real_di_CreateDevice) + { + real_di_CreateDevice = + (DICREATEDEVICEPROC)hook_func((PROC*)&(*ppvOut)->lpVtbl->CreateDevice, (PROC)fake_di_CreateDevice); + } + + if (SUCCEEDED(result) && + !real_di_CreateDeviceEx && + riidltf && + (IsEqualGUID(&IID_IDirectInput7A, riidltf) || IsEqualGUID(&IID_IDirectInput7W, riidltf))) + { + real_di_CreateDeviceEx = + (DICREATEDEVICEEXPROC)hook_func((PROC*)&(*ppvOut)->lpVtbl->CreateDeviceEx, (PROC)fake_di_CreateDeviceEx); + } + + return result; +} + HRESULT WINAPI fake_DirectInput8Create(HINSTANCE hinst, DWORD dwVersion, REFIID riidltf, LPDIRECTINPUT8* ppvOut, LPUNKNOWN punkOuter) { dprintf("DirectInput8Create\n"); diff --git a/src/hook.c b/src/hook.c index e944f2d..f88db80 100644 --- a/src/hook.c +++ b/src/hook.c @@ -378,6 +378,26 @@ void hook_init() DetourTransactionCommit(); } + real_DirectInputCreateW = (DIRECTINPUTCREATEWPROC)GetProcAddress(LoadLibraryA("dinput.dll"), "DirectInputCreateW"); + + if (real_DirectInputCreateW) + { + DetourTransactionBegin(); + DetourUpdateThread(GetCurrentThread()); + DetourAttach((PVOID*)&real_DirectInputCreateW, (PVOID)fake_DirectInputCreateW); + DetourTransactionCommit(); + } + + real_DirectInputCreateEx = (DIRECTINPUTCREATEEXPROC)GetProcAddress(LoadLibraryA("dinput.dll"), "DirectInputCreateEx"); + + if (real_DirectInputCreateEx) + { + DetourTransactionBegin(); + DetourUpdateThread(GetCurrentThread()); + DetourAttach((PVOID*)&real_DirectInputCreateEx, (PVOID)fake_DirectInputCreateEx); + DetourTransactionCommit(); + } + real_DirectInput8Create = (DIRECTINPUT8CREATEPROC)GetProcAddress(LoadLibraryA("dinput8.dll"), "DirectInput8Create"); if (real_DirectInput8Create) @@ -410,6 +430,8 @@ void hook_init() void hook_early_init() { hook_patch_iat(GetModuleHandle(NULL), FALSE, "dinput.dll", "DirectInputCreateA", (PROC)fake_DirectInputCreateA); + hook_patch_iat(GetModuleHandle(NULL), FALSE, "dinput.dll", "DirectInputCreateW", (PROC)fake_DirectInputCreateW); + hook_patch_iat(GetModuleHandle(NULL), FALSE, "dinput.dll", "DirectInputCreateEx", (PROC)fake_DirectInputCreateEx); hook_patch_iat(GetModuleHandle(NULL), FALSE, "dinput8.dll", "DirectInput8Create", (PROC)fake_DirectInput8Create); } @@ -430,6 +452,22 @@ void hook_exit() DetourTransactionCommit(); } + if (real_DirectInputCreateW) + { + DetourTransactionBegin(); + DetourUpdateThread(GetCurrentThread()); + DetourDetach((PVOID*)&real_DirectInputCreateW, (PVOID)fake_DirectInputCreateW); + DetourTransactionCommit(); + } + + if (real_DirectInputCreateEx) + { + DetourTransactionBegin(); + DetourUpdateThread(GetCurrentThread()); + DetourDetach((PVOID*)&real_DirectInputCreateEx, (PVOID)fake_DirectInputCreateEx); + DetourTransactionCommit(); + } + if (real_DirectInput8Create) { DetourTransactionBegin(); @@ -444,5 +482,7 @@ void hook_exit() } hook_patch_iat(GetModuleHandle(NULL), TRUE, "dinput.dll", "DirectInputCreateA", (PROC)fake_DirectInputCreateA); + hook_patch_iat(GetModuleHandle(NULL), TRUE, "dinput.dll", "DirectInputCreateW", (PROC)fake_DirectInputCreateW); + hook_patch_iat(GetModuleHandle(NULL), TRUE, "dinput.dll", "DirectInputCreateEx", (PROC)fake_DirectInputCreateEx); hook_patch_iat(GetModuleHandle(NULL), TRUE, "dinput8.dll", "DirectInput8Create", (PROC)fake_DirectInput8Create); }