tPresent gPresent = nullptr;
tReset gReset = nullptr;
D3DPRESENT_PARAMETERS* gPresentParams = nullptr;
HRESULT __stdcall hkdReset(IDirect3DDevice9 *d3dDev, D3DPRESENT_PARAMETERS *presentParams)
{
Log("-> hkdReset");
gPresentParams = presentParams;
HRESULT hRes = gReset(d3dDev, presentParams);
Log("<- hkdReset");
return hRes;
}
HRESULT __stdcall hkdPresent(IDirect3DDevice9 *d3dDev, CONST RECT *srcRect, CONST RECT *destRect, HWND destWindow, CONST RGNDATA *dirtyRegion)
{
Log("-> hkdPresent");
HRESULT hRes = gPresent(d3dDev, srcRect, destRect, destWindow, dirtyRegion);
Log("<- hkdPresent");
return hRes;
}
DWORD FindDevice(DWORD dwLen)
{
DWORD dwObjBase = (DWORD)LoadLibraryA("d3d9.dll");
if (dwObjBase == 0x0)
return 0x0;
while (dwObjBase++ < dwObjBase + dwLen)
{
if ((*(WORD*)(dwObjBase + 0x00)) == 0x06C7 && (*(WORD*)(dwObjBase + 0x06)) == 0x8689 && (*(WORD*)(dwObjBase + 0x0C)) == 0x8689)
{
dwObjBase += 2;
break;
}
}
return dwObjBase;
};
void initD3D9()
{
Log("-> Install D3D9-hook..");
DWORD* VTable;
*(DWORD*)&VTable = *(DWORD *)FindDevice(0x128000);
DWORD VP16, VP17;
VirtualProtect((LPVOID)&VTable[16], 4, PAGE_READWRITE, &VP16);
VirtualProtect((LPVOID)&VTable[17], 4, PAGE_READWRITE, &VP17);
gReset = (tReset)VTable[16];
gPresent = (tPresent)VTable[17];
VTable[16] = (DWORD)hkdReset;
VTable[17] = (DWORD)hkdPresent;
VirtualProtect((LPVOID)&VTable[16], 4, VP16, &VP16);
VirtualProtect((LPVOID)&VTable[17], 4, VP17, &VP17);
Log("<- Install D3D9-hook..");
}
void destD3D9()
{
Log("-> Remove D3D9-hook..");
DWORD* VTable;
*(DWORD*)&VTable = *(DWORD*)FindDevice(0x128000);
DWORD VP16, VP17;
VirtualProtect((LPVOID)&VTable[16], 4, PAGE_READWRITE, &VP16);
VirtualProtect((LPVOID)&VTable[17], 4, PAGE_READWRITE, &VP17);
VTable[16] = (DWORD)gReset;
VTable[17] = (DWORD)gPresent;
VirtualProtect((LPVOID)&VTable[16], 4, VP16, &VP16);
VirtualProtect((LPVOID)&VTable[17], 4, VP17, &VP17);
Log("<- Remove D3D9-hook..");
}