Hello,
I am trying to apply an EndScene hook on WoW using a VMT hook (VMT hook is just replacing the pointer in the Virtual Method Table of D3D9 to point to your own function).
Here is my Code:
All pointers are valid. I debugged and looked up every pointer. Now what happens is that my own EndScene function (hkEndScene) is never called but the original function...
Code:
void InitHook()
{
HMODULE hModule = NULL;
while( !hModule )
{
hModule = GetModuleHandleA( "d3d9.dll" );
Sleep( 100 );
}
DWORD* VTableStart = 0;
DWORD VTable = dwFindPattern((DWORD)hModule, 0x128000, (PBYTE)"\xC7\x06\x00\x00\x00\x00\x89\x86\x00\x00\x00\x00\x89\x86", "xx????xx????xx");
DWORD temp = *(DWORD*)(VTable+2);
VTableStart = (DWORD*)temp;
o_pEndScene = (EndScene_t)VTableStart[42];
Log *pLog = new Log("debug.log");
char c[50];
sprintf(c, "VTable pointer: %X", VTableStart);
pLog->Write(c);
sprintf(c, "VTable EndScene pointer: %X", VTableStart+42);
pLog->Write(c);
pLog->Write("Attempting to hook EndScene");
sprintf(c, "Before: VTableStart[42]: %X", VTableStart[42]);
pLog->Write(c);
DWORD oldRights;
pLog->Write("Unprotecting memory");
VirtualProtect(&VTableStart[42], 4, PAGE_EXECUTE_READWRITE, &oldRights);
VTableStart[42] = (DWORD)hkEndScene;
VirtualProtect(&VTableStart[42], 4, oldRights, &oldRights);
pLog->Write("Reprotecting memory");
pLog->Write("EndScene hooked");
sprintf(c, "o_pEndScene: %X", o_pEndScene);
pLog->Write(c);
sprintf(c, "hkEndScene: %X", hkEndScene);
pLog->Write(c);
sprintf(c, "After: VTableStart[42]: %X", VTableStart[42]);
pLog->Write(c);
}
The log looks like this:
Code:
VTable pointer: 5F324E08
VTable EndScene pointer: 5F324EB0
Attempting to hook EndScene
Before: VTableStart[42]: 5F34279F
Unprotecting memory
Reprotecting memory
EndScene hooked
o_pEndScene: 5F34279F
hkEndScene: 6E611000
After: VTableStart[42]: 6E611000
And here is hkEndScene:
Code:
HRESULT __stdcall hkEndScene(LPDIRECT3DDEVICE9 pDevice)
{
pLogg->Write("hkEndScene called");
const D3DCOLOR txtPink = D3DCOLOR_ARGB(255, 255, 0, 255);
DrawRect( pDevice, 10, 10, 200, 200, txtPink);
return o_pEndScene(pDevice);
}