So i'm trying to make a tracer wallhack for bfbc2 but i can't seem to get this hook to work anyone have any ideas how i can fix this any knowledge is appreciated the game is running d3d9

dllmain.cpp
Code:
#include "DllMain.h"
void InitializeD3DHook(void)
{
#ifdef _DEBUG
LOG("Called InitializeD3DHook()");
#endif
D3DHook::SetLength(0x128000);
D3DHook::SetSignature((PBYTE)"\xC7\x06\x00\x00\x00\x00\x89\x86\x00\x00\x00\x00\x89\x86");
D3DHook::SetMask("xx????xx????xx");
if (!D3DHook::GetBase())
return;
if (!D3DHook::GetOffset())
return;
if (!D3DHook::GetVTable())
return;
D3DHook::Hook()->DrawIndexPrimitive();
//D3DHook::Hook()->EndScene();
}
BOOL WINAPI DllMain( HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved )
{
switch( dwReason )
{
case DLL_PROCESS_ATTACH:
#ifdef _DEBUG
LOG( "======[ LOG STARTED ]======" );
#endif
InitializeD3DHook();
break;
case DLL_PROCESS_DETACH:
#ifdef _DEBUG
LOG( "========[ LOG END ]========\n\n" );
#endif
g_pLog->Close();
break;
}
return TRUE;
}
d3dhook.cpp
Code:
#include "D3DHook.h"
#include "..\Memory\Memory.h"
namespace D3DHook
{
BYTE* m_bMask = NULL;
char* m_szMask = "";
DWORD m_dwLength = 0;
DWORD* m_vTable = NULL;
DWORD m_dwBase = 0;
DWORD m_dwOffset = 0;
LPDIRECT3DDEVICE9 m_pDevice = NULL;
ID3DXFont* m_pFont = NULL;
ID3DXLine* m_pLine = NULL;
CHook m_pHook;
//========================================================================================================================================================//
void SetD3DVTableString( std::vector< char* >& szVec )
{
szVec.clear();
szVec.push_back( "QueryInterface" );
szVec.push_back( "AddRef" );
szVec.push_back( "Release" );
szVec.push_back( "TestCooperativeLevel" );
szVec.push_back( "GetAvaibleTextureMem" );
szVec.push_back( "EvictManagedResources" );
szVec.push_back( "GetDirect3D" );
szVec.push_back( "GetDeviceCaps" );
szVec.push_back( "GetDisplayMode" );
szVec.push_back( "GetCreationParameters" );
szVec.push_back( "SetCursorProperties" );
szVec.push_back( "SetCursorPosition" );
szVec.push_back( "ShowCursor" );
szVec.push_back( "CreateAdditionalSwapChain" );
szVec.push_back( "GetSwapChain" );
szVec.push_back( "GetNumberOfSwapChains" );
szVec.push_back( "Reset" );
szVec.push_back( "Present" );
szVec.push_back( "GetBackBuffer" );
szVec.push_back( "GetRasterStatus" );
szVec.push_back( "SetDialogBoxMode" );
szVec.push_back( "SetGammaRamp" );
szVec.push_back( "GetGammaRamp" );
szVec.push_back( "CreateTexture" );
szVec.push_back( "CreateVolumeTexture" );
szVec.push_back( "CreateCubeTexture" );
szVec.push_back( "CreateVertexBuffer" );
szVec.push_back( "CreateIndexBuffer" );
szVec.push_back( "CreateRenderTarget" );
szVec.push_back( "CreateDepthStencilSurface" );
szVec.push_back( "UpdateSurface" );
szVec.push_back( "UpdateTexture" );
szVec.push_back( "GetRenderTargetData" );
szVec.push_back( "GetFronBufferData" );
szVec.push_back( "StrechRect" );
szVec.push_back( "ColorFill" );
szVec.push_back( "CreateOffscreenPlainSurface" );
szVec.push_back( "SetRenderTarget" );
szVec.push_back( "GetRenderTarget" );
szVec.push_back( "SetDepthStencilSurface" );
szVec.push_back( "GetDepthStencilSurface" );
szVec.push_back( "BeginScene" );
szVec.push_back( "EndScene" );
szVec.push_back( "Clear" );
szVec.push_back( "SetTransforM" );
szVec.push_back( "GetTransform" );
szVec.push_back( "MultiplyTransform" );
szVec.push_back( "SetViewport" );
szVec.push_back( "GetViewport" );
szVec.push_back( "SetMaterial" );
szVec.push_back( "GetMaterial" );
szVec.push_back( "SetLight" );
szVec.push_back( "GetLight" );
szVec.push_back( "LightEnable" );
szVec.push_back( "GetLightEnable" );
szVec.push_back( "SetClipPlane" );
szVec.push_back( "GetClipPlane" );
szVec.push_back( "SetRenderState" );
szVec.push_back( "GetRenderState" );
szVec.push_back( "CreateStateBlock" );
szVec.push_back( "BeginStateBlock" );
szVec.push_back( "EndStateBlock" );
szVec.push_back( "SetClipStatus" );
szVec.push_back( "GetClipStatus" );
szVec.push_back( "GetTexture" );
szVec.push_back( "SetTexture" );
szVec.push_back( "GetTextureStageState" );
szVec.push_back( "SetTextureStageState" );
szVec.push_back( "GetSamplerState" );
szVec.push_back( "SetSamplerState" );
szVec.push_back( "ValidateDevice" );
szVec.push_back( "SetPaletteEntries" );
szVec.push_back( "GetPaletteEntries" );
szVec.push_back( "SetCurrentTexturePalette" );
szVec.push_back( "GetCurrentTexturePalette" );
szVec.push_back( "SetScissorRect" );
szVec.push_back( "GetScissorRect" );
szVec.push_back( "SetSoftwareVertexProcessing" );
szVec.push_back( "GetSoftwareVertexProcessing" );
szVec.push_back( "SetNPatchMode" );
szVec.push_back( "GetNPatchMode" );
szVec.push_back( "DrawPrimitive" );
szVec.push_back( "DrawIndexPrimitive" );
szVec.push_back( "DrawPrimitiveUP" );
szVec.push_back( "DrawIndexPrimitiveUP" );
szVec.push_back( "ProcessVertices" );
szVec.push_back( "CreateVertexDeclaration" );
szVec.push_back( "SetVertexDeclaration" );
szVec.push_back( "GetVertexDeclaration" );
szVec.push_back( "SetFVF" );
szVec.push_back( "GetFVF" );
szVec.push_back( "CreateVertexShader" );
szVec.push_back( "SetVertexShader" );
szVec.push_back( "GetVertexShader" );
szVec.push_back( "SetVertexShaderConstantF" );
szVec.push_back( "GetVertexShaderConstantF" );
szVec.push_back( "SetVertexShaderConstantI" );
szVec.push_back( "GetVertexShaderConstantI" );
szVec.push_back( "SetVertexShaderConstantB" );
szVec.push_back( "GetVertexShaderConstantB" );
szVec.push_back( "SetStreamSource" );
szVec.push_back( "GetStreamSource" );
szVec.push_back( "SetStreamSourceFreq" );
szVec.push_back( "GetStreamSourceFreq" );
szVec.push_back( "SetIndices" );
szVec.push_back( "GetIndices" );
szVec.push_back( "CreatePixelShader" );
szVec.push_back( "SetPixelShader" );
szVec.push_back( "GetPixelShader" );
szVec.push_back( "SetPixelShaderConstantF" );
szVec.push_back( "GetPixelShaderConstantF" );
szVec.push_back( "SetPixelShaderConstantI" );
szVec.push_back( "GetPixelShaderConstantI" );
szVec.push_back( "SetPixelShaderConstantB" );
szVec.push_back( "GetPixelShaderConstantB" );
szVec.push_back( "DrawRectPatch" );
szVec.push_back( "DrawTriPatch" );
szVec.push_back( "DeletePatch" );
szVec.push_back( "CreateQuery" );
}
void SetDevice( LPDIRECT3DDEVICE9& pDevice )
{
m_pDevice = pDevice;
}
HRESULT GeneratePixelShader( LPDIRECT3DDEVICE9 pDevice, LPDIRECT3DPIXELSHADER9* pShader, float a, float r, float g, float b )
{
char szShaderASMBuffer[ MAX_PATH ];
ID3DXBuffer* pD3DBuffer = NULL;
sprintf( szShaderASMBuffer, "ps_3_0\ndef c0, %f, %f, %f, %f\nmov oC0,c0", r, g, b, a );
if( FAILED( D3DXAssembleShader( szShaderASMBuffer, ( strlen( szShaderASMBuffer ) + 1 ), NULL, NULL, 0, &pD3DBuffer, NULL ) ) )
return E_FAIL;
return pDevice->CreatePixelShader( ( const DWORD* )pD3DBuffer->GetBufferPointer(), pShader );
}
namespace Prototype
{
typedef HRESULT( __stdcall* EndScene_t )( LPDIRECT3DDEVICE9 );
typedef HRESULT( __stdcall* DrawIndexedPrimitive_t )( LPDIRECT3DDEVICE9, D3DPRIMITIVETYPE, INT, UINT, UINT, UINT, UINT );
EndScene_t m_EndScene;
DrawIndexedPrimitive_t m_DrawIndexPrimitive;
}
namespace Hooks
{
HRESULT __stdcall hkEndScene( LPDIRECT3DDEVICE9 pDevice )
{
SetDevice( pDevice );
if( !m_pFont && !m_pLine )
{
D3DXCreateFontA( m_pDevice, 14, 0, FW_NORMAL, 1, false, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH|FF_DONTCARE, "Arial", &m_pFont );
D3DXCreateLine( m_pDevice, &m_pLine );
m_pLine->SetAntialias( false );
m_pLine->SetPattern( 0xFFFFFFFF );
m_pLine->SetWidth( 1.f );
}
return Prototype::m_EndScene( pDevice );
}
HRESULT __stdcall hkDrawIndexPrimitive(LPDIRECT3DDEVICE9 pDevice, D3DPRIMITIVETYPE Type, INT BaseVertexIndex, UINT MinVertexIndex, UINT NumVertices, UINT startIndex, UINT PrimitiveCount)
{
LPDIRECT3DVERTEXBUFFER9 pD3D0VertexStreamBuffer = NULL;
UINT iOffsetInBytes = 0, Stride = 0;
// 0 invisible 1 visible
static IDirect3DPixelShader9 *pShader[2] = { NULL };
if (!pShader[0]){
if (FAILED(GeneratePixelShader(pDevice, &pShader[0], 255.f, 0.f, 255.f, 0.f)))
MessageBoxA(NULL, "failed to generate pixelshader 1", "DrawIndexPrimitive Hook", MB_ICONERROR);
}
if (!pShader[1]){
if (FAILED(GeneratePixelShader(pDevice, &pShader[1], 255.f, 255.f, 0.f, 0.f)))
MessageBoxA(NULL, "failed to generate pixelshader 2", "DrawIndexPrimitive Hook", MB_ICONERROR);
}
if (pDevice->GetStreamSource(0, &pD3D0VertexStreamBuffer, &iOffsetInBytes, &Stride))
{
if ((NumVertices == 806 && PrimitiveCount == 618) || (NumVertices == 36 && PrimitiveCount == 32) || (NumVertices == 98 && PrimitiveCount == 131) ||
(NumVertices == 196 && PrimitiveCount == 143) || (NumVertices == 4000 && PrimitiveCount == 4773) || (NumVertices == 1225 && PrimitiveCount == 1580) ||
(NumVertices == 1557 && PrimitiveCount == 1864)){
pDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
pDevice->SetPixelShader(pShader[1]);
Prototype::m_DrawIndexPrimitive(pDevice, Type, BaseVertexIndex, MinVertexIndex, NumVertices, startIndex, PrimitiveCount);
pDevice->SetPixelShader(pShader[0]);
pDevice->SetRenderState(D3DRS_ZENABLE, TRUE);
Prototype::m_DrawIndexPrimitive(pDevice, Type, BaseVertexIndex, MinVertexIndex, NumVertices, startIndex, PrimitiveCount);
}
}
return Prototype::m_DrawIndexPrimitive(pDevice, Type, BaseVertexIndex, MinVertexIndex, NumVertices, startIndex, PrimitiveCount);
}
}
//========================================================================================================================================================//
void CHook::DrawIndexPrimitive( void )
{
#ifdef _DEBUG
LOG( "Called D3DHook::CHook::DrawIndexPrimitive()" );
#endif
Prototype::m_DrawIndexPrimitive = ( Prototype::DrawIndexedPrimitive_t )Memory::DetourFunction( ( PBYTE )m_vTable[ VTABLE_DrawIndexPrimitive ], ( PBYTE )Hooks::hkDrawIndexPrimitive, 5 );
}
void CHook::EndScene( void )
{
#ifdef _DEBUG
LOG( "Called D3DHook::CHook::EndScene()" );
#endif
Prototype::m_EndScene = ( Prototype::EndScene_t )Memory::DetourFunction( ( PBYTE )m_vTable[ VTABLE_EndScene ], ( PBYTE )Hooks::hkEndScene, 5 );
}
CHook* Hook( void )
{
return &m_pHook;
}
void SetLength( DWORD dwLength )
{
m_dwLength = dwLength;
LOG( "Called D3DHook::SetLength() - 0x%X", dwLength );
}
void SetSignature( BYTE* bMask )
{
m_bMask = bMask;
#ifdef _DEBUG
LOG( "Called D3DHook::SetSignature()" );
#endif
}
void SetMask( char* szMask )
{
m_szMask = szMask;
#ifdef _DEBUG
LOG( "Called D3DHook::SetMask() - %s", szMask );
#endif
}
bool GetBase( void )
{
m_dwBase = ( DWORD )LoadLibraryA( "d3d9.dll" );
#ifdef _DEBUG
LOG( "Called D3DHook::GetBase()" );
LOG( "Found d3d0.dll at 0x%X", m_dwBase );
#endif
return( m_dwBase != NULL );
}
bool GetOffset( void )
{
#ifdef _DEBUG
LOG( "Called D3DHook::GetOffset()" );
#endif
if( !m_dwBase || !m_dwLength || !m_bMask || !m_szMask )
return false;
m_dwOffset = Memory::FindPattern( m_dwBase, m_dwLength, m_bMask, m_szMask );
#ifdef _DEBUG
LOG( "Found Offset at 0x%X", m_dwOffset );
#endif
return ( m_dwOffset != NULL );
}
bool GetVTable( void )
{
#ifdef _DEBUG
LOG( "Called D3DHook::GetVTable()" );
#endif
if( !m_dwOffset )
return false;
memcpy( &m_vTable, ( void* )( m_dwOffset + 2 ), 4 );
#ifdef _DEBUG
std::vector< char* > szVTable;
SetD3DVTableString( szVTable );
for( int i = 0; i < szVTable.size(); i++ )
{
LOG( "Found VTable[ %.3d ]: 0x%X - (%s)", i, m_vTable[ i ], szVTable[ i ] );
}
#endif
return ( m_vTable != NULL );
}
ID3DXFont* GetFont( int iFont )
{
switch( iFont )
{
case FONT_DEFAULT:
return m_pFont;
}
return m_pFont;
}
LPDIRECT3DDEVICE9 GetDevice( void )
{
return m_pDevice;
}
}
d3dhook.h
Code:
#ifndef __DIRECT3D_H__
#define __DIRECT3D_H__
#ifdef _WIN32
#pragma once
#endif
#include "..\stdafx.h"
#include <d3d9.h>
#include <d3dx9.h>
#pragma comment( lib, "d3d9.lib" )
#pragma comment( lib, "d3dx9.lib" )
enum D3DVtable
{
VTABLE_QueryInterface = 0,
VTABLE_AddRef,
VTABLE_Release,
VTABLE_TestCooperativeLevel,
VTABLE_GetAvaibleTextureMem,
VTABLE_EvictManagedResources,
VTABLE_GetDirect3D,
VTABLE_GetDeviceCaps,
VTABLE_GetDisplayMode,
VTABLE_GetCreationParameters,
VTABLE_SetCursorProperties,
VTABLE_SetCursorPosition,
VTABLE_ShowCursor,
VTABLE_CreateAdditionalSwapChain,
VTABLE_GetSwapChain,
VTABLE_GetNumberOfSwapChains,
VTABLE_Reset,
VTABLE_Present,
VTABLE_GetBackBuffer,
VTABLE_GetRasterStatus,
VTABLE_SetDialogBoxMode,
VTABLE_SetGammaRamp,
VTABLE_GetGammaRamp,
VTABLE_CreateTexture,
VTABLE_CreateVolumeTexture,
VTABLE_CreateCubeTexture,
VTABLE_CreateVertexBuffer,
VTABLE_CreateIndexBuffer,
VTABLE_CreateRenderTarget,
VTABLE_CreateDepthStencilSurface,
VTABLE_UpdateSurface,
VTABLE_UpdateTexture,
VTABLE_GetRenderTargetData,
VTABLE_GetFronBufferData,
VTABLE_StrechRect,
VTABLE_ColorFill,
VTABLE_CreateOffscreenPlainSurface,
VTABLE_SetRenderTarget,
VTABLE_GetRenderTarget,
VTABLE_SetDepthStencilSurface,
VTABLE_GetDepthStencilSurface,
VTABLE_BeginScene,
VTABLE_EndScene,
VTABLE_Clear,
VTABLE_SetTransforM,
VTABLE_GetTransform,
VTABLE_MultiplyTransform,
VTABLE_SetViewport,
VTABLE_GetViewport,
VTABLE_SetMaterial,
VTABLE_GetMaterial,
VTABLE_SetLight,
VTABLE_GetLight,
VTABLE_LightEnable,
VTABLE_GetLightEnable,
VTABLE_SetClipPlane,
VTABLE_GetClipPlane,
VTABLE_SetRenderState,
VTABLE_GetRenderState,
VTABLE_CreateStateBlock,
VTABLE_BeginStateBlock,
VTABLE_EndStateBlock,
VTABLE_SetClipStatus,
VTABLE_GetClipStatus,
VTABLE_GetTexture,
VTABLE_SetTexture,
VTABLE_GetTextureStageState,
VTABLE_SetTextureStageState,
VTABLE_GetSamplerState,
VTABLE_SetSamplerState,
VTABLE_ValidateDevice,
VTABLE_SetPaletteEntries,
VTABLE_GetPaletteEntries,
VTABLE_SetCurrentTexturePalette,
VTABLE_GetCurrentTexturePalette,
VTABLE_SetScissorRect,
VTABLE_GetScissorRect,
VTABLE_SetSoftwareVertexProcessing,
VTABLE_GetSoftwareVertexProcessing,
VTABLE_SetNPatchMode,
VTABLE_GetNPatchMode,
VTABLE_DrawPrimitive,
VTABLE_DrawIndexPrimitive,
VTABLE_DrawPrimitiveUP,
VTABLE_DrawIndexPrimitiveUP,
VTABLE_ProcessVertices,
VTABLE_CreateVertexDeclaration,
VTABLE_SetVertexDeclaration,
VTABLE_GetVertexDeclaration,
VTABLE_SetFVF,
VTABLE_GetFVF,
VTABLE_CreateVertexShader,
VTABLE_SetVertexShader,
VTABLE_GetVertexShader,
VTABLE_SetVertexShaderConstantF,
VTABLE_GetVertexShaderConstantF,
VTABLE_SetVertexShaderConstantI,
VTABLE_GetVertexShaderConstantI,
VTABLE_SetVertexShaderConstantB,
VTABLE_GetVertexShaderConstantB,
VTABLE_SetStreamSource,
VTABLE_GetStreamSource,
VTABLE_SetStreamSourceFreq,
VTABLE_GetStreamSourceFreq,
VTABLE_SetIndices,
VTABLE_GetIndices,
VTABLE_CreatePixelShader,
VTABLE_SetPixelShader,
VTABLE_GetPixelShader,
VTABLE_SetPixelShaderConstantF,
VTABLE_GetPixelShaderConstantF,
VTABLE_SetPixelShaderConstantI,
VTABLE_GetPixelShaderConstantI,
VTABLE_SetPixelShaderConstantB,
VTABLE_GetPixelShaderConstantB,
VTABLE_DrawRectPatch,
VTABLE_DrawTriPatch,
VTABLE_DeletePatch,
VTABLE_CreateQuery,
};
enum D3DFont
{
FONT_DEFAULT = 0,
};
namespace D3DHook
{
class CHook
{
public:
void DrawIndexPrimitive( void );
void EndScene( void );
};
extern CHook* Hook( void );
extern void SetLength( DWORD dwLength );
extern void SetSignature( BYTE* bMask );
extern void SetMask( char* szMask );
extern bool GetBase( void );
extern bool GetOffset( void );
extern bool GetVTable( void );
extern ID3DXFont* GetFont( int iFont = FONT_DEFAULT );
extern LPDIRECT3DDEVICE9 GetDevice( void );
};
#endif