Results 1 to 6 of 6
  1. #1
    Anddos's Avatar
    Join Date
    Nov 2009
    Gender
    male
    Posts
    44
    Reputation
    10
    Thanks
    5

    Help with hooking from a dll

    Here's my dll code , the messagebox hook works fine but now i want to get a handle to the d3d device and unsure how todo it using Hook and UnHook

    Code:
    #include "stdafx.h"
    #include <stdio.h>
    #include <d3d9.h>
    #include <d3dx9.h>
    #pragma comment (lib, "d3d9.lib")
    #pragma comment (lib, "d3dx9.lib")
    char FileName[64];
    int MyMessageBoxA(HWND hWnd, LPCSTR lpText, LPCSTR lpCaption, UINT uType);
    int MyCreateDevice9(UINT adapter, D3DDEVTYPE deviceType, HWND focusWindow,DWORD behaviourFlags, D3DPRESENT_PARAMETERS *presentationParameters, IDirect3DDevice9** device);
    DWORD HookFunction(LPCSTR lpModule, LPCSTR lpFuncName, LPVOID lpFunction, unsigned char *lpBackup);
    BOOL UnHookFunction(LPCSTR lpModule, LPCSTR lpFuncName, unsigned char *lpBackup);
    void RenderScene(LPDIRECT3DDEVICE9 d3ddev);
    
    BYTE hook[6];
    LPDIRECT3D9 d3d;
    
    BOOL APIENTRY DllMain( HMODULE hModule,
                           DWORD  ul_reason_for_call,
                           LPVOID lpReserved
    					 )
    {
    	switch (ul_reason_for_call)
    	{
    	case DLL_PROCESS_ATTACH:
    		GetModuleFileName(NULL,FileName,sizeof(FileName));
    		MessageBox(NULL,"Injected",FileName,0);
    		HookFunction("user32.dll", "MessageBoxA", MyMessageBoxA, hook); //this is a sample of runtime hook
    	    MessageBox(0, "HEY", "", MB_OK);
    		HookFunction("d3d9.dll", "CreateDevice", MyCreateDevice9, hook); 
    	    
    		
    	case DLL_THREAD_ATTACH:
    	case DLL_THREAD_DETACH:
    	case DLL_PROCESS_DETACH:
    		break;
    	}
    	return TRUE;
    }
    
    int MyCreateDevice9(UINT adapter, D3DDEVTYPE deviceType, HWND focusWindow,DWORD behaviourFlags, D3DPRESENT_PARAMETERS *presentationParameters, IDirect3DDevice9** device)
    {
    	    d3d = Direct3DCreate9(D3D_SDK_VERSION);
    		UnHookFunction("d3d9.dll", "CreateDevice", hook);
    		//device should be the active device in the game right?
    		int x = d3d->CreateDevice(adapter,deviceType,focusWindow,behaviourFlags, presentationParameters,device);
    		HookFunction("d3d9.dll", "CreateDevice", MyCreateDevice9, hook);
    		return x;
    }
    
    
    int MyMessageBoxA(HWND hWnd, LPCSTR lpText, LPCSTR lpCaption, UINT uType)
    {
            UnHookFunction("user32.dll", "MessageBoxA", hook);
    		char msg[32];
    		sprintf(msg, "HOOKED!!\n\n%s", lpText);
    		int x = MessageBox(hWnd, msg, lpCaption, uType);
    		HookFunction("user32.dll", "MessageBoxA", MyMessageBoxA, hook);
    		return x;
    }
    
    
    DWORD HookFunction(LPCSTR lpModule, LPCSTR lpFuncName, LPVOID lpFunction, unsigned char *lpBackup)
    {
    	DWORD dwAddr = (DWORD)GetProcAddress(GetModuleHandle(lpModule), lpFuncName);
    
            BYTE jmp[6] = { 0xe9,   //jmp
    
                    0x00, 0x00, 0x00, 0x00, //address
    
                    0xc3
    
            };      //retn
    
    		ReadProcessMemory(GetCurrentProcess(), (LPVOID)dwAddr, lpBackup, 6, 0);
    		DWORD dwCalc = ((DWORD)lpFunction - dwAddr - 5);        //((to)-(from)-5)
    		memcpy(&jmp[1], &dwCalc, 4);    //build the jmp
    		WriteProcessMemory(GetCurrentProcess(), (LPVOID)dwAddr, jmp, 6, 0);
    		return dwAddr;
    }
    
    
    BOOL UnHookFunction(LPCSTR lpModule, LPCSTR lpFuncName, unsigned char *lpBackup)
    {
    
            DWORD dwAddr = (DWORD)GetProcAddress(GetModuleHandle(lpModule), lpFuncName);
    		if (WriteProcessMemory(GetCurrentProcess(), (LPVOID)dwAddr, lpBackup, 6, 0))
    			return TRUE;
    
    		return FALSE;
    
    }
    
    void RenderScene(LPDIRECT3DDEVICE9 d3ddev)
    {
    	d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
        d3ddev->Clear(0, NULL, D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
    
        d3ddev->BeginScene();
    
    	d3ddev->EndScene(); 
    	d3ddev->Present(NULL, NULL, NULL, NULL);
    }
    Last edited by Anddos; 12-21-2009 at 05:27 AM.

  2. #2
    Hell_Demon's Avatar
    Join Date
    Mar 2008
    Gender
    male
    Location
    I love causing havoc
    Posts
    3,976
    Reputation
    343
    Thanks
    4,320
    My Mood
    Cheeky
    HMODULE hModule = LoadLibrary("d3d9.dll");
    FARPROC d3dc9 = GetProcAddress(hModule, "Direct3DCreate9");

    That's all i'll give you figure the rest our yourself
    Ah we-a blaze the fyah, make it bun dem!

  3. #3
    Anddos's Avatar
    Join Date
    Nov 2009
    Gender
    male
    Posts
    44
    Reputation
    10
    Thanks
    5
    well i know about that , but i want to use the device that is running the game and not create a new one , thats what hooking runtime means , you get to access the parameters that was used at compile time...
    Last edited by Anddos; 12-21-2009 at 06:14 AM.

  4. #4
    falzarex's Avatar
    Join Date
    Apr 2008
    Gender
    male
    Location
    here
    Posts
    417
    Reputation
    14
    Thanks
    145
    Get the device pointer
    Quote Originally Posted by falzarex aka myself
    GTFO FUCKER U DONT BELONG IN THE INTERNETZ WORLD COZ ITS MINE


    This is an epic fail resume
    Hello VBfags.
    A 'member' of the almighty C++ section will soon join you, he is 13 year old, has the IQ and typing skills of a VBfag, so I thought he would fit in here nicely.

    A few reasons why he should be in this section instead of the C++ section:
    1) He has the IQ of a VBfag.
    2) He has no sense of grammer/spelling at all.
    3) He thinks he is pro(like most of the people in here)
    4) He thinks copy pasting is fun(exactly what you guys do)
    5) He loves it up the ass(he will keep you VBfags nice and warm)

  5. #5
    Anddos's Avatar
    Join Date
    Nov 2009
    Gender
    male
    Posts
    44
    Reputation
    10
    Thanks
    5
    Care to explain how?

  6. #6
    falzarex's Avatar
    Join Date
    Apr 2008
    Gender
    male
    Location
    here
    Posts
    417
    Reputation
    14
    Thanks
    145
    I need to check my code lol haven't touched it in awhile
    oh yeah welcome to mpgh
    Quote Originally Posted by falzarex aka myself
    GTFO FUCKER U DONT BELONG IN THE INTERNETZ WORLD COZ ITS MINE


    This is an epic fail resume
    Hello VBfags.
    A 'member' of the almighty C++ section will soon join you, he is 13 year old, has the IQ and typing skills of a VBfag, so I thought he would fit in here nicely.

    A few reasons why he should be in this section instead of the C++ section:
    1) He has the IQ of a VBfag.
    2) He has no sense of grammer/spelling at all.
    3) He thinks he is pro(like most of the people in here)
    4) He thinks copy pasting is fun(exactly what you guys do)
    5) He loves it up the ass(he will keep you VBfags nice and warm)

Similar Threads

  1. Help with hook up
    By elcamu987 in forum Combat Arms Coding Help & Discussion
    Replies: 0
    Last Post: 06-09-2011, 06:00 AM
  2. [Help Request] QCZM 3.1 combine with weapons from 5.0 - help
    By maciek1o3s in forum Call of Duty Modern Warfare 2 GSC Modding Help/Discussion
    Replies: 13
    Last Post: 06-01-2011, 07:18 AM
  3. Help With Hook
    By Flengo in forum Combat Arms Coding Help & Discussion
    Replies: 0
    Last Post: 03-05-2011, 09:07 AM
  4. [Help] Dealing with pointers from a dll
    By ctpsolo in forum C++/C Programming
    Replies: 11
    Last Post: 01-26-2010, 11:19 PM
  5. Need HELP with hooking FIFA09 :: Willing to pay
    By irfanwcg in forum C++/C Programming
    Replies: 0
    Last Post: 02-14-2009, 05:52 AM