1
0
mirror of https://github.com/FunkyFr3sh/cnc-ddraw.git synced 2025-03-24 17:49:52 +01:00

improve dinput hooks with hook=3

This commit is contained in:
FunkyFr3sh 2021-05-11 21:45:38 +02:00
parent 1923e2bb0c
commit 3bc8fffe81
3 changed files with 63 additions and 25 deletions

View File

@ -1,7 +1,21 @@
#ifndef DINPUTINPUT_H #ifndef DINPUTINPUT_H
#define DINPUTINPUT_H #define DINPUTINPUT_H
#define DIRECTINPUT_VERSION 0x0800
#include <dinput.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);
extern DIRECTINPUTCREATEAPROC real_DirectInputCreateA;
extern DIRECTINPUT8CREATEPROC real_DirectInput8Create;
void dinput_hook(); void dinput_hook();
void dinput_unhook(); 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 #endif

View File

@ -1,18 +1,14 @@
#include <windows.h> #include <windows.h>
#include <dinput.h> #include "directinput.h"
#include "debug.h" #include "debug.h"
#include "hook.h" #include "hook.h"
#include "dd.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; DIRECTINPUTCREATEAPROC real_DirectInputCreateA;
static DIRECTINPUT8CREATEPROC real_DirectInput8Create; DIRECTINPUT8CREATEPROC real_DirectInput8Create;
static DICREATEDEVICEPROC real_di_CreateDevice; static DICREATEDEVICEPROC real_di_CreateDevice;
static DIDSETCOOPERATIVELEVELPROC real_did_SetCooperativeLevel; static DIDSETCOOPERATIVELEVELPROC real_did_SetCooperativeLevel;
static DIDGETDEVICEDATAPROC real_did_GetDeviceData; static DIDGETDEVICEDATAPROC real_did_GetDeviceData;
@ -68,12 +64,15 @@ static HRESULT WINAPI fake_di_CreateDevice(IDirectInputA *This, REFGUID rguid, L
return result; 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"); dprintf("DirectInputCreateA\n");
real_DirectInputCreateA = if (!real_DirectInputCreateA)
(DIRECTINPUTCREATEAPROC)GetProcAddress(GetModuleHandle("dinput.dll"), "DirectInputCreateA"); {
real_DirectInputCreateA =
(DIRECTINPUTCREATEAPROC)GetProcAddress(GetModuleHandle("dinput.dll"), "DirectInputCreateA");
}
if (!real_DirectInputCreateA) if (!real_DirectInputCreateA)
return DIERR_GENERIC; return DIERR_GENERIC;
@ -93,8 +92,11 @@ HRESULT WINAPI fake_DirectInput8Create(HINSTANCE hinst, DWORD dwVersion, REFIID
{ {
dprintf("DirectInput8Create\n"); dprintf("DirectInput8Create\n");
real_DirectInput8Create = if (!real_DirectInput8Create)
(DIRECTINPUT8CREATEPROC)GetProcAddress(GetModuleHandle("dinput8.dll"), "DirectInput8Create"); {
real_DirectInput8Create =
(DIRECTINPUT8CREATEPROC)GetProcAddress(GetModuleHandle("dinput8.dll"), "DirectInput8Create");
}
if (!real_DirectInput8Create) if (!real_DirectInput8Create)
return DIERR_GENERIC; return DIERR_GENERIC;

View File

@ -2,6 +2,7 @@
#include <windows.h> #include <windows.h>
#include <stdio.h> #include <stdio.h>
#include <psapi.h> #include <psapi.h>
#include "directinput.h"
#include "dd.h" #include "dd.h"
#include "winapi_hooks.h" #include "winapi_hooks.h"
#include "hook.h" #include "hook.h"
@ -367,21 +368,31 @@ void hook_init()
#ifdef _MSC_VER #ifdef _MSC_VER
if (!g_hook_active && g_hook_method == 3) 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) if (real_DirectInputCreateA)
real_LoadLibraryExW = (LOADLIBRARYEXWPROC)proc; {
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourAttach((PVOID*)&real_DirectInputCreateA, (PVOID)fake_DirectInputCreateA);
DetourTransactionCommit();
}
DetourTransactionBegin(); real_DirectInput8Create = (DIRECTINPUT8CREATEPROC)GetProcAddress(LoadLibraryA("dinput8.dll"), "DirectInput8Create");
DetourUpdateThread(GetCurrentThread());
DetourAttach((PVOID*)&real_LoadLibraryExW, (PVOID)fake_LoadLibraryExW); if (real_DirectInput8Create)
DetourTransactionCommit(); {
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourAttach((PVOID*)&real_DirectInput8Create, (PVOID)fake_DirectInput8Create);
DetourTransactionCommit();
}
} }
#endif #endif
g_hook_active = TRUE; 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++) for (int i = 0; g_hooks[i].module_name[0]; i++)
{ {
@ -405,10 +416,21 @@ void hook_exit()
#ifdef _MSC_VER #ifdef _MSC_VER
if (g_hook_method == 3) if (g_hook_method == 3)
{ {
DetourTransactionBegin(); if (real_DirectInputCreateA)
DetourUpdateThread(GetCurrentThread()); {
DetourDetach((PVOID*)&real_LoadLibraryExW, (PVOID)fake_LoadLibraryExW); DetourTransactionBegin();
DetourTransactionCommit(); 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 #endif