Code:
include <d3d9.h>
#include <d3dx9.h>
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include "detours.h"
HMODULE D3D9Handle = NULL;
DWORD** VtablePtr = NULL;
DWORD* VTable = NULL;
ID3DXFont* pfont = NULL;
typedef HRESULT (*oPresent) (LPDIRECT3DDEVICE9, CONST RECT* ,CONST RECT* , HWND, CONST RGNDATA* );
typedef HRESULT (*oReset)(LPDIRECT3DDEVICE9, D3DPRESENT_PARAMETERS*);
oPresent pPresent = NULL;
oReset pReset = NULL;
HRESULT myPresent(LPDIRECT3DDEVICE9 pDevice, CONST RECT* pSourceRect,CONST RECT* pDestRect, HWND hDestWindowOverride,CONST RGNDATA* pDirtyRegion)
{
RECT font_rect;
SetRect(&font_rect,20,50,500,50+23);
pfont->DrawTextA(NULL, //pSprite
"THATS THE WAY IT IS!", //your text
-1, //Count(-1 to disregard)
&font_rect, //pRect(controls positioning)
DT_LEFT|DT_NOCLIP, //Format (DT_WORDBREAK for wrapping)
0xFFFFFFFF); //Color (ARGB)
return pPresent(pDevice, pSourceRect, pDestRect, hDestWindowOverride, pDirtyRegion);
}
HRESULT myReset(LPDIRECT3DDEVICE9 pDevice, D3DPRESENT_PARAMETERS* pParam)
{
HRESULT hr = NULL;
pfont->OnLostDevice();
hr = pReset(pDevice, pParam);
pfont->OnResetDevice();
return hr;
}
DWORD** FindDevice(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) return (DWORD**)
(Base + i + 2);n = 0;
}
return(0);
}
VOID Hook(VOID)
{
D3D9Handle = GetModuleHandleA("d3d9.dll");
VtablePtr = FindDevice((DWORD)D3D9Handle,0x128000);
VTable = *VtablePtr;
pPresent = (oPresent)DetourAttach( &(PVOID&)VTable[17], myPresent);
pReset = (oReset)DetourAttach(&(PVOID&)VTable[16],myReset);
D3DXCreateFontA((LPDIRECT3DDEVICE9)VtablePtr, //D3D Device
22, //Font height
0, //Font width
FW_NORMAL, //Font Weight
1, //MipLevels
false, //Italic
DEFAULT_CHARSET, //CharSet
OUT_DEFAULT_PRECIS, //OutputPrecision
ANTIALIASED_QUALITY, //Quality
DEFAULT_PITCH|FF_DONTCARE,//PitchAndFamily
"Arial", //pFacename,
&pfont); //ppFont
}
void UnHook()
{
DetourDetach(&(PVOID&)VTable[17], &(PVOID&)myPresent);
DetourDetach(&(PVOID&)VTable[16], &(PVOID&)myReset);
return;
}
void mainthread()
{
while(1)
{
Sleep(200);
}
}
BOOL WINAPI DllMain(HINSTANCE hModule, DWORD dwReason, LPVOID lpvReserved)
{
switch(dwReason)
{
case DLL_PROCESS_ATTACH:
DisableThreadLibraryCalls( hModule );
while((!GetModuleHandleA("d3d9.dll") || !GetModuleHandleA("ClientFX.fxd")|| !GetModuleHandleA("CShell.dll")))
{Sleep(200);}//waiting for modules to load
DetourTransactionBegin();
DetourUpdateThread( GetCurrentThread() );
Hook();
DetourTransactionCommit();
//CreateThread( 0, 0, ( LPTHREAD_START_ROUTINE )Hook, 0, 0, 0 );
CreateThread( 0, 0, ( LPTHREAD_START_ROUTINE )mainthread, 0, 0, 0 );
case DLL_PROCESS_DETACH:
DetourTransactionBegin();
UnHook();
DetourTransactionCommit();
}
}
The FindDevice() method is Croner's. Also thanks to Longevity for showing me. Please help, I think Im getting close, but I can't move on with my menu until I'm able to find a working hook to render text. Any advice is appreciated.