Hooking directdraw

c++ / delphi package - dll injection and api hooking
Post Reply
chh
Posts: 3
Joined: Fri Dec 09, 2011 4:09 pm

Hooking directdraw

Post by chh »

Hi everyone,

i have used this code from example:

Code: Select all

var
     //Original Code By  madshi www.madshi.net. Credits To madshi For His Example Hooking Module
     //Code Edited By Ultimation. To Include Drawing Text Example
  MyDevice:  IDirect3DDevice9;
  PresentNext: function(const Self: IDirect3DDevice9;
  const SourceRect, DestRect: PRect;
  const DestWindowOverride: HWND;
  DirtyRegion:
  PRgnData): HResult; stdcall = nil;

  CreateDeviceNext: function(const Self: pointer;
  Adapter: longword;
  DeviceType: TD3DDevType;
  hFocusWindow: HWND;
  BehaviorFlags: DWord;
  pPresentationParameters: PD3DPresentParameters;
  out ppReturnedDeviceInterface:
  IDirect3DDevice9): HResult; stdcall = nil;

  Direct3DCreate9Next: function(SDKVersion: cardinal): Pointer; stdcall;

  //------------------------------------------------------------------------------

  function GetInterfaceMethod(intf: IUnknown; methodIndex: dword): pointer;
  begin
    Result := pointer(pointer(dword(pointer(intf)^) + methodIndex * 4)^);
  end;

  function GetPtrMethod(ptr: pointer; methodIndex: dword): pointer;
  begin
    Result := pointer(pointer(dword(ptr^) + methodIndex * 4)^);
  end;

  //------------------------------------------------------------------------------ 
 function PresentCallback(const Self: IDirect3DDevice9;
  const SourceRect, DestRect: PRect;
  const DestWindowOverride: HWND;
    DirtyRegion: PRgnData): HResult; stdcall;
  var
    Font: D3DX9.ID3DXFont;
    rec:  PRect;
  begin
      //Your Drawing Code Here. This Function Gets Called Everytime The Surface Needs Refreshing
      D3DXCreateFontA(Self, 15, 0, FW_BOLD, 1, False, DEFAULT_CHARSET,
        OUT_DEFAULT_PRECIS, ANTIALIASED_QUALITY, DEFAULT_PITCH or FF_DONTCARE, 'Arial', Font);

      GetMem(rec, 16);
      SetRect(rec^, 200, 0, 900, 900);

      Font.DrawTextA(nil, 'DirectX Drawing Text Example',
        Length('DirectX Drawing Text Example'), rec, DT_LEFT, $FFFFFFFF);

    Result := PresentNext(self, SourceRect, DestRect, DestWindowOverride, DirtyRegion);

  end;

  function CreateDeviceCallback(const Self: pointer;
    Adapter: longword;
    DeviceType: TD3DDevType;
    hFocusWindow: HWND;
    BehaviorFlags: DWord;
    pPresentationParameters: PD3DPresentParameters;
    out ppReturnedDeviceInterface: IDirect3DDevice9)
  : HResult; stdcall;

  begin

    Result   := CreateDeviceNext(self, Adapter, DeviceType, hFocusWindow,
      BehaviorFlags, pPresentationParameters, ppReturnedDeviceInterface);
    MyDevice := ppReturnedDeviceInterface;
    if (Result = 0) and (@PresentNext = nil) then
      HookCode(GetInterfaceMethod(ppReturnedDeviceInterface, 17),
        @PresentCallback, @PresentNext);

  end;

  function Direct3DCreate9Callback(SDKVersion: cardinal): Pointer; stdcall;
  begin

    Result := Direct3DCreate9Next(SDKVersion);

    if not (Result = nil) and (@CreateDeviceNext = nil) then
      HookCode(GetPtrMethod(Result, 16), @CreateDeviceCallback, @CreateDeviceNext);
  end;

begin
  HookAPI('d3d9.dll', 'Direct3DCreate9', @Direct3DCreate9Callback,     //d3d9.dll
    @Direct3DCreate9Next);
end.
compiled it with no errors and injected the ddl into cod4 (DirectX9), but nothing happens :SSS
the game doesn't crash but theres also no rec drawn ingame
im really new to this. give me a hint please :)
madshi
Site Admin
Posts: 10764
Joined: Sun Mar 21, 2004 5:25 pm

Re: Hooking directdraw

Post by madshi »

First thing to check is whether basic things like:

- does HookAPI(Direct3DCreate9) return true or false?
- does Direct3DCreate9Callback get called at all?

Also make sure you inject the dll *first*, before starting the game, because if you don't, the game will already have called Direct3DCreate9, so your hook will be installed too late.

I hope this is not for a cheat? I don't want madCodeHook to be used for any kind of software the original game developers would dislike.
chh
Posts: 3
Joined: Fri Dec 09, 2011 4:09 pm

Re: Hooking directdraw

Post by chh »

Hey Madshi! :)
Also make sure you inject the dll *first*, before starting the game, because if you don't, the game will already have called Direct3DCreate9, so your hook will be installed too late.
I think this is the point. i've injected the dll with a seperate injector, while the game is already running ...
so my hook won't be installed.

How can i fix this? how can i inject the dll without a seperate programm?

PS: thats not for a cheat, just messing around with d3d and maybe getting a clock ingame :)
madshi
Site Admin
Posts: 10764
Joined: Sun Mar 21, 2004 5:25 pm

Re: Hooking directdraw

Post by madshi »

The standard madCodeHook injector injects your hook dll user wide, which means that it will automatically be injected into newly created processes when they start. So just perform the injection first, then start the game afterwards. Done.
chh
Posts: 3
Joined: Fri Dec 09, 2011 4:09 pm

Re: Hooking directdraw

Post by chh »

Hi Madshi :)

Code: Select all

The standard madCodeHook injector [...]
Sorry, but i don't understand that. Which madCodeHook injector? Do you mean an extra unit, in which all the injection is done?
Could you explain it in more detail please?
Sry for dumb question :)
madshi
Site Admin
Posts: 10764
Joined: Sun Mar 21, 2004 5:25 pm

Re: Hooking directdraw

Post by madshi »

There's an exe part of all the madCodeHook (system wide) demos which is responsible for the injection of the demo hook dlls. This injection executable file usually injects the hook dll system/user wide, so that the hook dll gets automatically injected into newly created processes.
Post Reply