Trouble In D3D9 Hook [Solved]
Code:
#include "d3d.h"
extern bool debugging;
typedef HRESULT (WINAPI* Reset_Prototype) (LPDIRECT3DDEVICE9, D3DPRESENT_PARAMETERS*);
typedef HRESULT (WINAPI* EndScene_Prototype) (LPDIRECT3DDEVICE9);
typedef HRESULT (WINAPI* DrawIndexedPrimitive_Prototype)(LPDIRECT3DDEVICE9, D3DPRIMITIVETYPE, INT, UINT, UINT, UINT, UINT);
int d3dinit()
{
while(!GetModuleHandleA("d3d9.dll"))
{
Sleep(50);
}
d3d9handle = (DWORD)GetModuleHandleA("d3d9.dll");
finddev(d3d9handle, 0x128000);
pEndScene = (EndScene_Prototype)(DWORD)vTable[42];
HookVFunc(vTable, 42, (DWORD*)nEndScene);
return 0;
}
HRESULT WINAPI nEndScene(LPDIRECT3DDEVICE9 pDevice)
{
MessageBoxA(NULL, "In EndScene", "Debug", MB_OK);
return pEndScene(pDevice);
}
DWORD** finddev(DWORD base, DWORD len)
{
unsigned long i = 0, n = 0;
for( i = 0; i < len; i++ )
{
if(*(BYTE *)(base+i+0x00)==0xC7)n++;
if(*(BYTE *)(base+i+0x01)==0x06)n++;
if(*(BYTE *)(base+i+0x06)==0x89)n++;
if(*(BYTE *)(base+i+0x07)==0x86)n++;
if(*(BYTE *)(base+i+0x0C)==0x89)n++;
if(*(BYTE *)(base+i+0x0D)==0x86)n++;
if(n == 6)
{
pvTable = (DWORD**)(base + i + 2);
*(DWORD*)&vTable = *(DWORD*)pvTable;
if(debugging == true)
{
char buf[64];
sprintf(buf, "EndScene: %.8X", vTable[42]);
MessageBoxA(NULL, buf, "Debug", MB_OK);
}
return 0;
}
n = 0;
}
if(debugging == true)
MessageBoxA(NULL, "device not found!", "Debug", MB_OK);
return 0;
}
DWORD* HookVFunc(DWORD* vtable, int index, DWORD *newFunction)
{
DWORD dwOldProt, *oldFunc;
VirtualProtect(&vtable[index], 4, PAGE_EXECUTE_READWRITE, &dwOldProt);
oldFunc=(DWORD*)vtable[index];
vtable[index]=(DWORD)newFunction;
VirtualProtect(&vtable[index], 4, dwOldProt, &dwOldProt);
return oldFunc;
}

