From 4948b8d79607e0d6c597cfe39411f28ffcc64d41 Mon Sep 17 00:00:00 2001 From: TellowKrinkle Date: Fri, 1 Apr 2022 22:21:43 -0500 Subject: [PATCH] x86 Support --- MinimalDInput8Hook/CustomHooks.cpp | 8 ++++---- MinimalDInput8Hook/DInput8.h | 5 ++--- MinimalDInput8Hook/Hook.cpp | 12 +++++------- MinimalDInput8Hook/MinimalDInput8Hook.cpp | 5 ++++- MinimalDInput8Hook/MinimalDInput8Hook.vcxproj | 16 ++++++---------- 5 files changed, 21 insertions(+), 25 deletions(-) diff --git a/MinimalDInput8Hook/CustomHooks.cpp b/MinimalDInput8Hook/CustomHooks.cpp index c3ea471..a78e85d 100644 --- a/MinimalDInput8Hook/CustomHooks.cpp +++ b/MinimalDInput8Hook/CustomHooks.cpp @@ -1,7 +1,7 @@ #include "stdafx.h" #include "CustomHooks.h" -typedef HANDLE(*CreateFileA_t)( +typedef HANDLE(WINAPI*CreateFileA_t)( LPCSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, @@ -10,7 +10,7 @@ typedef HANDLE(*CreateFileA_t)( DWORD dwFlagsAndAttributes, HANDLE hTemplateFile); -typedef HANDLE(*CreateFileW_t)( +typedef HANDLE(WINAPI*CreateFileW_t)( LPCWSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, @@ -22,7 +22,7 @@ typedef HANDLE(*CreateFileW_t)( CreateFileA_t OriginalCreateFileA; CreateFileW_t OriginalCreateFileW; -HANDLE CreateFileA_Wrapper( +HANDLE WINAPI CreateFileA_Wrapper( LPCSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, @@ -47,7 +47,7 @@ HANDLE CreateFileA_Wrapper( hTemplateFile); } -HANDLE CreateFileW_Wrapper( +HANDLE WINAPI CreateFileW_Wrapper( LPCWSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, diff --git a/MinimalDInput8Hook/DInput8.h b/MinimalDInput8Hook/DInput8.h index 601e3d5..bcb29e5 100644 --- a/MinimalDInput8Hook/DInput8.h +++ b/MinimalDInput8Hook/DInput8.h @@ -1,8 +1,7 @@ #pragma once #include - -typedef HRESULT(*DirectInput8Create_t)( +typedef HRESULT(WINAPI*DirectInput8Create_t)( HINSTANCE hinst, DWORD dwVersion, REFIID riidltf, @@ -15,7 +14,7 @@ extern HMODULE DInput8DLL; extern "C" { - DINPUT8_API HRESULT DirectInput8Create( + DINPUT8_API HRESULT WINAPI DirectInput8Create( HINSTANCE hinst, DWORD dwVersion, REFIID riidltf, diff --git a/MinimalDInput8Hook/Hook.cpp b/MinimalDInput8Hook/Hook.cpp index 966bb2d..4f55f82 100644 --- a/MinimalDInput8Hook/Hook.cpp +++ b/MinimalDInput8Hook/Hook.cpp @@ -22,7 +22,7 @@ void* HookFunction_Internal(const char* DLLName, const char* FunctionName, void* BYTE* BaseAddress = (BYTE*)PEBExtendedInfo->ImageBaseAddress; // Get the Address of our NT Image headers - PIMAGE_NT_HEADERS64 FileHeader = (PIMAGE_NT_HEADERS64)(BaseAddress + pDOSHeader->e_lfanew); + PIMAGE_NT_HEADERS FileHeader = (PIMAGE_NT_HEADERS)(BaseAddress + pDOSHeader->e_lfanew); // Retrieve the import directory in which all imported dlls and functions are listed IMAGE_DATA_DIRECTORY ImportDirectory = FileHeader->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT]; @@ -38,8 +38,7 @@ void* HookFunction_Internal(const char* DLLName, const char* FunctionName, void* // Check if we found our DLL in the import table if (!strcmp(ImportDLLName, DLLName)) { - - PIMAGE_THUNK_DATA64 ImportNameTable = (PIMAGE_THUNK_DATA64)(BaseAddress + Descriptor->OriginalFirstThunk); + PIMAGE_THUNK_DATA ImportNameTable = (PIMAGE_THUNK_DATA)(BaseAddress + Descriptor->OriginalFirstThunk); int Offset = 0; // The import name table is a null terminated array, so iterate until we either found it or reach the null termination while (ImportNameTable->u1.AddressOfData != 0) @@ -47,20 +46,19 @@ void* HookFunction_Internal(const char* DLLName, const char* FunctionName, void* PIMAGE_IMPORT_BY_NAME NameImport = (PIMAGE_IMPORT_BY_NAME)(BaseAddress + ImportNameTable->u1.AddressOfData); // Null terminated function name start pointer is stored in here const char* ImportFunctionName = NameImport->Name; - if (!strcmp(FunctionName, ImportFunctionName)) { - PIMAGE_THUNK_DATA64 ImportAddressTable = (PIMAGE_THUNK_DATA64)(BaseAddress + Descriptor->FirstThunk); + PIMAGE_THUNK_DATA ImportAddressTable = (PIMAGE_THUNK_DATA)(BaseAddress + Descriptor->FirstThunk); // The import address table is in the same order as the import name table ImportAddressTable += Offset; void* OriginalAddress = (void*)ImportAddressTable->u1.AddressOfData; DWORD OldProtection; // Make the page writable to patch the pointer - VirtualProtect(ImportAddressTable, sizeof(IMAGE_THUNK_DATA64), PAGE_READWRITE, &OldProtection); + VirtualProtect(ImportAddressTable, sizeof(IMAGE_THUNK_DATA), PAGE_READWRITE, &OldProtection); ImportAddressTable->u1.AddressOfData = (ULONGLONG)NewAddress; // Restore page protection to the previous state - VirtualProtect(ImportAddressTable, sizeof(IMAGE_THUNK_DATA64), OldProtection, &OldProtection); + VirtualProtect(ImportAddressTable, sizeof(IMAGE_THUNK_DATA), OldProtection, &OldProtection); return OriginalAddress; } diff --git a/MinimalDInput8Hook/MinimalDInput8Hook.cpp b/MinimalDInput8Hook/MinimalDInput8Hook.cpp index 58a1c9d..b67e994 100644 --- a/MinimalDInput8Hook/MinimalDInput8Hook.cpp +++ b/MinimalDInput8Hook/MinimalDInput8Hook.cpp @@ -9,8 +9,11 @@ HMODULE DInput8DLL = nullptr; -DINPUT8_API HRESULT DirectInput8Create(HINSTANCE hinst, DWORD dwVersion, REFIID riidltf, LPVOID * ppvOut, LPUNKNOWN punkOuter) +DINPUT8_API HRESULT WINAPI DirectInput8Create(HINSTANCE hinst, DWORD dwVersion, REFIID riidltf, LPVOID * ppvOut, LPUNKNOWN punkOuter) { + // MSVC will mangle the name of __stdcall functions, even in C + // Workaround to avoid needing a .def file from https://stackoverflow.com/a/2805560 + #pragma comment(linker, "/EXPORT:" __FUNCTION__ "=" __FUNCDNAME__) if (OriginalFunction) { return OriginalFunction(hinst, dwVersion, riidltf, ppvOut, punkOuter); diff --git a/MinimalDInput8Hook/MinimalDInput8Hook.vcxproj b/MinimalDInput8Hook/MinimalDInput8Hook.vcxproj index 266a3f1..398d8f4 100644 --- a/MinimalDInput8Hook/MinimalDInput8Hook.vcxproj +++ b/MinimalDInput8Hook/MinimalDInput8Hook.vcxproj @@ -70,17 +70,11 @@ - - true - - + true DINPUT8 - - false - - + false DINPUT8 @@ -90,12 +84,13 @@ Level3 Disabled true - WIN32;_DEBUG;MINIMALDINPUT8HOOK_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions) + WIN32;_DEBUG;MINIMALDINPUT8HOOK_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions);DLL_EXPORT true Windows true + ntdll.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies) @@ -121,7 +116,7 @@ true true true - WIN32;NDEBUG;MINIMALDINPUT8HOOK_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions) + WIN32;NDEBUG;MINIMALDINPUT8HOOK_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions);DLL_EXPORT true @@ -129,6 +124,7 @@ true true true + ntdll.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)