Solved D3D9 Hook cod2

Hexui Undetected CSGO Cheats Sinkicheat PUBG Cheat
Status
Not open for further replies.

rapt0r

Banned
Silenced
Jul 4, 2013
128
643
0
Hello i'm trying to get this vtable hook to work for cod2 it's not crashing but it's also not working the code is from rN
DllMain.cpp
C++:
#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
C++:
#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;
    }
}
 
Last edited:

rN'

Jr.Hacker
Meme Tier VIP
Jan 19, 2014
340
5,268
41
Your English and grammar has been getting worse after moving back to England, eh?

You should learn C++,d3d and a proper ASM before doing anykind of stuff like this.
As an adult, you should know that learning takes time.

P.s: No offenserino
Wrote with him via steam, he has zero knowledge. I implemented him the chams part for bad company 2 and he can't debug it..
 

rapt0r

Banned
Silenced
Jul 4, 2013
128
643
0
Lol why ru always posting shit on my topics? i post for help not for criticism
 

Liduen

Hacker
Dank Tier VIP
May 19, 2013
702
8,478
33
Lol why ru always posting shit on my topics? i post for help not for criticism
Dr.Phil is right. You're requesting full working source code.
Otherwise you would have uploaded the code which does the actual hooking and you'd have given us information about where you think the problems is.
The fact that you didn't, tells us that you either don't have a clue about what you're doing or you're just too lazy to fix the problem yourself.

You don't want us to help.
You want us to give you code.
Atleast that's how it sounds to me.

If you want help, show us that you atleast tried to fix the problem yourself.
Tell us what you already tried, what worked and what didn't.
 

rapt0r

Banned
Silenced
Jul 4, 2013
128
643
0
Wrote with him via steam, he has zero knowledge. I implemented him the chams part for bad company 2 and he can't debug it..
I thought this vtable was supposed to work for every d3d9 game?
 

rapt0r

Banned
Silenced
Jul 4, 2013
128
643
0
Dr.Phil is right. You're requesting full working source code.
Otherwise you would have uploaded the code which does the actual hooking and you'd have given us information about where you think the problems is.
The fact that you didn't, tells us that you either don't have a clue about what you're doing or you're just too lazy to fix the problem yourself.

You don't want us to help.
You want us to give you code.
Atleast that's how it sounds to me.

If you want help, show us that you atleast tried to fix the problem yourself.
Tell us what you already tried, what worked and what didn't.
I'm asking for someone to give me help into how i can fix it not asking anyone to do it for me
 

Liduen

Hacker
Dank Tier VIP
May 19, 2013
702
8,478
33
i add information and still no one willing to help
Maybe that's because you basically just said "Here is my vtable hook and it doesn't work" and then you posted 370 lines of code.

No one is really motivated to skim through that because
1. it's way too much code than needed to know where the problem is and
2. rN' already told us that you have zero knowledge. If that's right or not I can't say but it gives us the impression that you're one of the guys trying to do something without really knowing what they're doing.

As for myself I never applied a vtable hook because I didn't really need it yet. But I know how it works in theory and can't imagine that it's difficult to do.
If you'd reduce the amount of code to the parts which are actually relevant for the problem I could take a guess though.
 

Liduen

Hacker
Dank Tier VIP
May 19, 2013
702
8,478
33
i add information and still no one willing to help
I recommend you writing the code again from scratch, hardocding the offset for the vtable and the offset for the function which you want to hook.
Don't do anything in your hook function but printing something to the console so you know it got called.

If there is nothing printed to screen take a look at the vtable in memory and check your hook there.
 
Status
Not open for further replies.
Attention! Before you post:

Read the How to Ask Questions Guide
99% of questions are answered in the Beginner's Guide, do it before asking a question.

No Hack Requests. Post in the correct section.  Search the forum first. Read the rules.

How to make a good post:

  • Fill out the form correctly
  • Tell us the game name & coding language
  • Post everything we need to know to help you
  • Ask specific questions, be descriptive
  • Post errors, line numbers & screenshots
  • Post code snippets using code tags
  • If it's a large project, zip it up and attach it

If you do not comply, your post may be deleted.  We want to help, please make a good post and we will do our best to help you.

Community Mods