Advertisement Jump to content
Sign in to follow this  
hack_tr

Dinput Hooking Problem

This topic is 1829 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello everybody I have a problem

my source is :

#define _CRT_SECURE_NO_WARNINGS
#define DIRECTINPUT_VERSION 0x0800
#include <Windows.h>
#include <cstdio> 
#include <time.h> 
#include <dinput.h> 
DWORD WINAPI HookThread(); 
void add_log(char* format, ...); 
void* DetourFunc(PBYTE src, const PBYTE dst, const int len); 
HRESULT __stdcall hkGetDeviceState(LPDIRECTINPUTDEVICE lpDevice, DWORD cbData, LPVOID lpvData);
typedef HRESULT(__stdcall* GetDeviceState_t)(LPDIRECTINPUTDEVICE, DWORD, LPVOID); 
GetDeviceState_t pGetDeviceState;
HMODULE hModDInput8 = NULL; 
FARPROC dwGetDeviceState = NULL;
FARPROC dwDirectInput8Create = NULL;
HANDLE tmpHandle = NULL;
bool Freeze = false; 
BOOL WINAPI DllMain(HINSTANCE hinstDll,DWORD Reason,LPVOID Reserved)
{ 
	switch(Reason)
	{ 
	case DLL_PROCESS_ATTACH:
		add_log("==========LOG START==========");
		add_log("DLL Attached");
		add_log("Creating Thread...");
		tmpHandle = CreateThread(0, 0, (LPTHREAD_START_ROUTINE)&HookThread, 0, 0, 0);
		if (!tmpHandle)
		{ 
			add_log("ThreadCreation Failed!");
		}
		break;
	case DLL_PROCESS_DETACH:
		add_log("DLL Detached");
		add_log("==========LOG END==========\n\n\n");
		break;
	} 
	return 1;
} 
DWORD WINAPI HookThread(void)
{ add_log("Thread Created");
while (!hModDInput8)
{
	add_log("Searching dinput8.dll...");
	hModDInput8 = GetModuleHandle(L"dinput8.dll");
	Sleep(100);
}
add_log("Found dinput8.dll: %x !", hModDInput8);
while (!dwDirectInput8Create) {

add_log("Searching GetDeviceState...");
dwDirectInput8Create = GetProcAddress(hModDInput8, "DirectInput8Create");
Sleep(100); } add_log("Found DirectInput8Create: %x !", dwDirectInput8Create);
dwGetDeviceState = (FARPROC) ((DWORD)dwDirectInput8Create - 0x62B1);
add_log("GetDevicestate is here (DirectInput8Create - 0x62B1): %x", dwGetDeviceState);
add_log("Hooking GetDeviceState...");
pGetDeviceState = (GetDeviceState_t) DetourFunc((PBYTE) dwGetDeviceState, (PBYTE) hkGetDeviceState, 5);
add_log("Hooked GetDeviceState - Trampolin: %x - New: %x !", pGetDeviceState, hkGetDeviceState);
while (true) {
	if (GetAsyncKeyState(VK_LMENU) & 1<<15) 
	{
		Freeze = !Freeze;
		Sleep(500);
	} Sleep(10);
} return 0;
} 
void add_log(char* format, ...) 
{ 
	HANDLE filehandle;
	DWORD dwReadBytes;
char buffer[2048]; 
char writebuffer[2048];
va_list args; va_start(args, format);
vsprintf (buffer, format, args); 
filehandle = CreateFile(L"Log.txt", GENERIC_WRITE, 0, 0, OPEN_ALWAYS, 0, 0);
SetFilePointer(filehandle, 0, 0, FILE_END); 
char date[18]; _strdate(date); date[8] = ' '; 
_strtime(date+9); sprintf_s(writebuffer, 2048, "Log Added (%s): %s\r\n", date, buffer);
WriteFile(filehandle, writebuffer, strlen(writebuffer), &dwReadBytes, 0); CloseHandle(filehandle);
}
void* DetourFunc(PBYTE src, const PBYTE dst, const int len) 
{ 
	DWORD dwback; BYTE* jmp = (BYTE*)malloc(len+5);
	VirtualProtect(src, len, PAGE_READWRITE, &dwback); memcpy(jmp, src, len);
	jmp += len; jmp[0] = 0xE9; *(DWORD*)(jmp+1) = (DWORD)(src + len - jmp) - 5; src[0] = 0xE9;
	*(DWORD*)(src+1) = (DWORD)(dst - src) - 5; 
	VirtualProtect(src, len, dwback, &dwback);
	VirtualProtect(jmp-len, len+5, PAGE_EXECUTE_READWRITE, &dwback); 
	return (jmp - len);
}
HRESULT __stdcall hkGetDeviceState(LPDIRECTINPUTDEVICE lpDevice, DWORD cbData, LPVOID lpvData)
{ 
	static BYTE buffer[256];
	HRESULT temp = DI_OK;

		temp = pGetDeviceState(lpDevice, cbData, lpvData);
		DIDEVICEOBJECTDATA rgdod;
        DWORD dwInOut = 1;
		if(lpDevice->GetDeviceData( sizeof rgdod, &rgdod, &dwInOut, 0 ) == DI_OK)
			add_log("GetDevOK");
		
               
       
	return temp; 
}
add_log("GetDevOK");

my log is :

Log Added (01/19/14 14:53:51): ==========LOG START==========
Log Added (01/19/14 14:53:51): DLL Attached
Log Added (01/19/14 14:53:51): Creating Thread...
Log Added (01/19/14 14:53:51): Thread Created
Log Added (01/19/14 14:53:51): Searching dinput8.dll...
Log Added (01/19/14 14:53:51): Found dinput8.dll: b430000 !
Log Added (01/19/14 14:53:51): Searching GetDeviceState...
Log Added (01/19/14 14:53:51): Found DirectInput8Create: b43cc8e !
Log Added (01/19/14 14:53:51): GetDevicestate is here (DirectInput8Create - 0x62B1): b4369dd
Log Added (01/19/14 14:53:51): Hooking GetDeviceState...
Log Added (01/19/14 14:53:51): Hooked GetDeviceState - Trampolin: 2d847f8 - New: 612e1203 !
Log Added (01/19/14 14:53:53): DLL Detached
Log Added (01/19/14 14:53:53): ==========LOG END==========




sad.pngsad.png

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!