Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

x86 Support #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions MinimalDInput8Hook/CustomHooks.cpp
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -47,7 +47,7 @@ HANDLE CreateFileA_Wrapper(
hTemplateFile);
}

HANDLE CreateFileW_Wrapper(
HANDLE WINAPI CreateFileW_Wrapper(
LPCWSTR lpFileName,
DWORD dwDesiredAccess,
DWORD dwShareMode,
Expand Down
5 changes: 2 additions & 3 deletions MinimalDInput8Hook/DInput8.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#pragma once
#include <unknwn.h>


typedef HRESULT(*DirectInput8Create_t)(
typedef HRESULT(WINAPI*DirectInput8Create_t)(
HINSTANCE hinst,
DWORD dwVersion,
REFIID riidltf,
Expand All @@ -15,7 +14,7 @@ extern HMODULE DInput8DLL;

extern "C"
{
DINPUT8_API HRESULT DirectInput8Create(
DINPUT8_API HRESULT WINAPI DirectInput8Create(
HINSTANCE hinst,
DWORD dwVersion,
REFIID riidltf,
Expand Down
12 changes: 5 additions & 7 deletions MinimalDInput8Hook/Hook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -38,29 +38,27 @@ 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)
{
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;
}

Expand Down
5 changes: 4 additions & 1 deletion MinimalDInput8Hook/MinimalDInput8Hook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
16 changes: 6 additions & 10 deletions MinimalDInput8Hook/MinimalDInput8Hook.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,11 @@
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<PropertyGroup Condition="'$(Configuration)'=='Debug'">
<LinkIncremental>true</LinkIncremental>
<TargetName>DINPUT8</TargetName>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<PropertyGroup Condition="'$(Configuration)'=='Release'">
<LinkIncremental>false</LinkIncremental>
<TargetName>DINPUT8</TargetName>
</PropertyGroup>
Expand All @@ -90,12 +84,13 @@
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;_DEBUG;MINIMALDINPUT8HOOK_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>WIN32;_DEBUG;MINIMALDINPUT8HOOK_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions);DLL_EXPORT</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>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)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
Expand All @@ -121,14 +116,15 @@
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;NDEBUG;MINIMALDINPUT8HOOK_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>WIN32;NDEBUG;MINIMALDINPUT8HOOK_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions);DLL_EXPORT</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>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)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
Expand Down