[Snippet] Dynamically Get VTable

Posts 15 of 5 · Page 1 of 1
[Snippet] Dynamically Get VTable
I was reading a topic here http://www.mpgh.net/forum/31-c-c-pro...-logger-4.html Which makes a good point that not everybody has the same d3d9.dll version which means you can't use a static address for hooking if you want your hack to work for all version, Stephen made a very good example of searching the vTable by using a Wild card search, for an address which has a pointer to the vTable. Anyway to cut a long story short here is my Delphi version based from his idea.

[highlight=delphi]
function GetD3d9Vtable(dwIndex: DWord): Cardinal;
var
pD3D9 : PByte;
pVtable : ^DWord;
cAddress: Cardinal;
const
//Pattern Used To scan for bytes
baVtablePattern: array[0..15] of Byte = ($33,$C0,$C7,$06,$00,$00,$00,$00,$89,$86,$00,$00,$ 00,$00,$89,$86);
//Mask used in WildCard $01 notes Wild cards
baVtableMask : array[0..15] of Byte = ($00,$00,$00,$00,$01,$01,$01,$01,$00,$00,$01,$01,$ 01,$01,$00,$00);
begin
//Get Base Adress of D3D9.dll
pD3D9:= PByte(GetModuleHandleA('d3d9.dll'));
//Wild Card Search For Pointer To VTable Address
cAddress:= WildCardSearch(pD3D9,@baVtablePattern,@baVtableMas k,SizeOf(baVtablePattern),128000,False);
//If results Returned an Adress
if cAddress > 0 then
begin
//Add 4 bytes to land on real Vtable pointer address
cAddress:= cAddress + $4;
//Get Vtable Index Pointer
pVtable:= Pointer(Dword(Ptr(cAddress)^) + dwIndex * 4);
//Return as Cardinal Index position pointer
Result := pVtable^
end
else
Result:= 0;
end;
[/highlight]

How to use...
[highlight=delphi]
var
dwPresent: Cardinal;
begin
//Checking to see if game is ready, this includes to see if d3d9.dll is loaded into memory
Repeat
Sleep(500);
Until ((IsGameReadyForHook = True) and (dwThrdMain = 0));

//Call our function to get Pointer address for hooking(Index 17 = Present)
dwPresent:= GetD3d9Vtable(17);

//Example hook replace "&" with "@" otherwise forum takes it a mention
if (HookJMP(Pointer(dwPresent) , &PresentCallBack, &PresentNext)) then
...
...
...
[/highlight]

As some people know I spent a few hours wondering why my PTC method wasn't working, Its because I had a static address from debugging D3D9.dll in olly, Little did I know my D3D9.dll was updated between my last hack and this one, Hence the reason this function was created to find it dynamically. Hope it might help someone who has ran into the same problem.
DELPHI!!!!!!!!!!!!!!!!!!/

it is so complex, but i understand
Delphi is not complex, Its just well structured which means it is prone for errors if using lazy coding, Lucky C++'ers can code in a lazy style and still get away with it. Saying that its easy to tell who takes care in there C++ projects by there coding style, The lazy coders make it difficult to read while other "Clean" coders make reading C++ easy for me.
Quote Originally Posted by Departure View Post
Delphi is not complex, Its just well structured which means it is prone for errors if using lazy coding, Lucky C++'ers can code in a lazy style and still get away with it. Saying that its easy to tell who takes care in there C++ projects by there coding style, The lazy coders make it difficult to read while other "Clean" coders make reading C++ easy for me.
I code very messily
I don't I have ever seen your code flameswor10.

Updated first post to make into one function and example how you would use it.
Posts 15 of 5 · Page 1 of 1

Post a Reply

Tags for this Thread

None

Need help?