google-site-verification: googlebaca44933768a824.html [C++] Simple VMTHook Class - Old Royal Hack Forum

Announcement

Collapse
No announcement yet.

[C++] Simple VMTHook Class

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    [C++] Simple VMTHook Class

    CVMTHook.h:
    Code:
    #pragma once
    
    #include <windows.h>
    
    class CVMTHook
    {
    private:
    	PDWORD m_pdwClassTable;
    	int m_iFunctionIndex;
    	PDWORD m_pdwFuntionPointer;
    	DWORD m_dwNewFunction;
    	DWORD m_dwOriginalFunction;
    	DWORD m_dwOldProtect;
    public:
    	DWORD Hook( DWORD new_Function, PDWORD pClass, int Index )
    	{
    		m_dwNewFunction = new_Function;
    		m_pdwClassTable = ( PDWORD ) * ( PDWORD ) pClass;
    		m_iFunctionIndex = Index;
    		m_pdwFuntionPointer = ( m_pdwClassTable + ( 0x4 * m_iFunctionIndex ) );
    		m_dwOriginalFunction = m_pdwClassTable[ m_iFunctionIndex ];
    
    		VirtualProtect( ( LPVOID ) m_pdwFuntionPointer, 4, PAGE_EXECUTE_READWRITE, &m_dwOldProtect );
    		m_pdwClassTable[ m_iFunctionIndex ] = m_dwNewFunction;
    		VirtualProtect( ( LPVOID ) m_pdwFuntionPointer, 4, m_dwOldProtect, NULL );
    
    		return m_dwOriginalFunction;
    	}
    	void UnHook( void )
    	{
    		VirtualProtect( ( LPVOID ) m_pdwFuntionPointer, 4, PAGE_EXECUTE_READWRITE, &m_dwOldProtect );
    		m_pdwClassTable[ m_iFunctionIndex ] = m_dwOriginalFunction;
    		VirtualProtect( ( LPVOID ) m_pdwFuntionPointer, 4, m_dwOldProtect, NULL );
    	}
    	void ReHook( void )
    	{
    		VirtualProtect( ( LPVOID ) m_pdwFuntionPointer, 4, PAGE_EXECUTE_READWRITE, &m_dwOldProtect );
    		m_pdwClassTable[ m_iFunctionIndex ] = m_dwNewFunction;
    		VirtualProtect( ( LPVOID ) m_pdwFuntionPointer, 4, m_dwOldProtect, NULL );
    	}
                    DWORD FunctionAddress( void )
    	{
    		return m_dwOriginalFunction;
    	}
    };
    Use it like this:
    Code:
    CVMTHook gIsDrawingLoadingImageHook;
    ...
    gIsDrawingLoadingImageHook.Hook( (DWORD)&new_IsDrawingLoadingImage, (PDWORD)g_pEngine, 28 );
    ...
    bool __stdcall new_IsDrawingLoadingImage( void )
    {
             // draw here
    	gIsDrawingLoadingImageHook.UnHook();
    	bool bRet = g_pEngine->IsDrawingLoadingImage();
    	gIsDrawingLoadingImageHook.ReHook();
    	return bRet;
    }
    OR like this:
    Code:
    CVMTHook gIsDrawingLoadingImageHook;
    typedef bool(__stdcall* IsDrawingLoadingImage_t)();
    IsDrawingLoadingImage_t IsDrawingLoadingImage;
    bool __stdcall new_IsDrawingLoadingImage( void )
    {
    	if( !IsDrawingLoadingImage )
    		IsDrawingLoadingImage = (IsDrawingLoadingImage_t)gIsDrawingLoadingImageHook.FunctionAddress();
    
             // draw here
    	return IsDrawingLoadingImage();
    }
    An advantage of this method is that you don't need to "export" the classes like IVEngineClient but you need to Unhook it before you call the original function and to ReHook it after the call as long as you don't create a pointer to the original function and call the original function directly.
    Anyway have phun!!1

    sign109
    I 0x90 you!

    #2
    aVitamin rides again!
    lolmaoman: Germans are born with a lifetime x22 login engraved into their birth certificates. True story.
    I DONT HAVE TEAMVIEWER AND IM NOT GOING TO GIVE ANY 24/7 ONLINE SUPPORT VIA STEAM, XFIRE OR OTHER IM PROGRAMS SO DONT BOTHER ASKING. THANKS.

    Comment


      #3
      Glad I was able to inspire you to write your own VMT class. You left out your FunctionAddress function in the class though
      Last edited by Kalvin; 09-25-2008, 02:52 AM.

      Comment


        #4
        Originally posted by Kalvin View Post
        Glad I was able to inspire you to write your own VMT class. You left out your FunctionAddress function in the class though
        nope...it's right after ReHook:wheelchair:
        I 0x90 you!

        Comment


          #5
          :smiley_880::smiley_880:
          :smiley_880::smiley_880:
          :smiley_880::smiley_880:
          :smiley_880::smiley_880:






          gibs coins @
          1KatP9B8KG7mvcoFhdLGua1isG88nYZE8C

          Comment


            #6
            For some reason I cant hook BeginScene with this class with normal detours it works fine:

            Code:
            CVMTHook gBeginSceneHook;
            
            HRESULT WINAPI nBeginScene( LPDIRECT3DDEVICE9 pDevice ) 
            {
            	LogFile->Write( "BEGIN1" );
            
            	gBeginSceneHook.UnHook( );
            	HRESULT hRet = pDevice->BeginScene( );
            	gBeginSceneHook.ReHook( );
            
            	LogFile->Write( "BEGIN2" );
            
            	return hRet;
            }
            In DllMain:

            Code:
            LogFile->Write( "MAIN1" );
            gBeginSceneHook.Hook( ( DWORD )&nBeginScene, vTable, 41 );
            LogFile->Write( "MAIN2" );
            '

            Address is ok cause it doesnt crash with normal hooks.

            The logfile shows:

            Code:
            [15:55:20] MAIN1
            [15:55:20] MAIN2
            So this means is crashed before the LogFile->Write( "BEGIN1" ); call.

            Comment


              #7
              Re: [C++] Simple VMTHook Class

              if this is concerning hl2 engine, its most likely the shaderapix9.dll causing the problem...may sound weird but i had the same problems, somehow my functions got unhooked by game(shaderapix9.dll) if i not rehook everyframe or detour the function by static jump/call patch. so best would be attach olly dbg and have a look where any why it gets unhooked.

              E: its long time ago, just remembered, i think even the vtable wrapp in shaderapix9 changed due to my vtable hook LOL
              I 0x90 you!

              Comment


                #8
                Re: [C++] Simple VMTHook Class

                No its for COD series. But it wasn't the good pointer, it whas from d3d9.dll but it had to be the device from the game exe. Btw. But if I hook more then 1 function for some reason the first hooked functions unhooks after one time calling it. With 1 hook the function works fine ( BeginScene, EndScene ). But I already use something mutch better now. I will maybe look into it when I have sometime.

                PS: This forum is only for Source Engine related games?

                Comment


                  #9
                  Re: [C++] Simple VMTHook Class

                  Originally posted by SystemFiles View Post
                  PS: This forum is only for Source Engine related games?

                  No ! party005

                  Contact:





                  !8m:67%;<51>^5T0-7Nb2cIt-C|229/q]Ps67812
                  HW: v3n0m4, Mattdog, Xeder
                  yO.-(3_=4%Z*Y;<)gsqH_!"5"{_B?34dok&@_91;


                  Comment


                    #10
                    Re: [C++] Simple VMTHook Class

                    Originally posted by SystemFiles View Post
                    For some reason I cant hook BeginScene with this class with normal detours it works fine:

                    Code:
                    CVMTHook gBeginSceneHook;
                    
                    HRESULT WINAPI nBeginScene( LPDIRECT3DDEVICE9 pDevice ) 
                    {
                    	LogFile->Write( "BEGIN1" );
                    
                    	gBeginSceneHook.UnHook( );
                    	HRESULT hRet = pDevice->BeginScene( );
                    	gBeginSceneHook.ReHook( );
                    
                    	LogFile->Write( "BEGIN2" );
                    
                    	return hRet;
                    }
                    In DllMain:

                    Code:
                    LogFile->Write( "MAIN1" );
                    gBeginSceneHook.Hook( ( DWORD )&nBeginScene, vTable, 41 );
                    LogFile->Write( "MAIN2" );
                    '

                    Address is ok cause it doesnt crash with normal hooks.

                    The logfile shows:

                    Code:
                    [15:55:20] MAIN1
                    [15:55:20] MAIN2
                    So this means is crashed before the LogFile->Write( "BEGIN1" ); call.
                    Try it like this: Typedef function so you have address to the original funcito, then call BeginSceneHook.Hook once somewhere.

                    Code:
                    // remember to globalize "pBeginScene" incase you dont have it inside a class.
                    typedef HRESULT (__stdcall* Scene_t)( LPDIRECT3DDEVICE9 );
                    Scene_t pBeginScene;
                    UINT beginScene = BeginSceneHook.Hook( (UINT_PTR)&Hooked_BeginScene, (PUINT_PTR)m_pDevice, 41 );
                    	pBeginScene = (Scene_t)beginScene;
                    
                    HRESULT WINAPI nBeginScene( LPDIRECT3DDEVICE9 pDevice ) 
                    {
                    	LogFile->Write( "BEGIN1" );
                    
                    	gBeginSceneHook.UnHook( );
                    	HRESULT hRet = pBeginScene( );
                    	gBeginSceneHook.ReHook( );
                    
                    	LogFile->Write( "BEGIN2" );
                    
                    	return hRet;
                    }
                    works fine for me and class i'm using for vmt hooking doesnt really differ that much.
                    lolmaoman: Germans are born with a lifetime x22 login engraved into their birth certificates. True story.
                    I DONT HAVE TEAMVIEWER AND IM NOT GOING TO GIVE ANY 24/7 ONLINE SUPPORT VIA STEAM, XFIRE OR OTHER IM PROGRAMS SO DONT BOTHER ASKING. THANKS.

                    Comment


                      #11
                      Re: [C++] Simple VMTHook Class

                      thx !

                      Comment


                        #12
                        Re: [C++] Simple VMTHook Class

                        Originally posted by znnk59 View Post
                        thx !
                        what about you stop posting useless shit?

                        Comment

                        Working...
                        X