首页
社区
课程
招聘
[求助]DX8的游戏在DX9环境应该如何HOOK?
发表于: 2008-9-19 07:15 11373

[求助]DX8的游戏在DX9环境应该如何HOOK?

2008-9-19 07:15
11373
我电脑环境是:WIN2003+DIRECTX9.0C
主要目的是:在一个DX8的游戏画面左上角显示时间。当然也有其他的停止。例如FPS什么的

下面的代码是 用madcodehook 写的。能正常在基于DX9的游戏下 显示字符串。

 
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Direct3D9, shellapi,DXTypes,madcodehook,
  D3DX9;

var

 
  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;
     Procedure InstallHook();
//------------------------------------------------------------------------------
implementation



  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

  MenuFont: D3DX9.ID3DXFont;
  rec:      PRect;
begin
  //Your Drawing Code Here. This Function Gets Called Everytime The Surface Needs Refreshing

  D3DXCreateFontA(Self, 30, 0, FW_BOLD, 1, False, DEFAULT_CHARSET,
    OUT_DEFAULT_PRECIS, ANTIALIASED_QUALITY, DEFAULT_PITCH or
    FF_DONTCARE, 'Arial', MenuFont);

  GetMem(rec, 16);
  SetRect(rec^, 20, 100, Screen.Width, screen.Height);

  MenuFont.DrawTextA(nil, 'Standard DirectX Hook By Ultimation',
    -1, rec, DT_LEFT, D3DCOLOR_ARGB(255, 255, 255, 255));

  Result := PresentNext(self, SourceRect, DestRect, DestWindowOverride, DirtyRegion);
  freemem(rec,16);
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);
 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;
 Procedure InstallHook();
  begin
    hookapi('d3d9.dll','Direct3DCreate9',@Direct3DCreate9CallBack,@Direct3DCreate9Next);
  end;
end.


现在这个游戏是DX8的(看调用了DXD8.DLL).
于是我把下面的代码改成了DX8的。
注入后 游戏提示:找不到 d3dx81ab.dll.
很奇怪~~~~

 library hook;

{$IMAGEBASE $59800000}




uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Direct3D8, shellapi,DXTypes,madcodehook,
  D3DX8;

var

 
  PresentNext: function(const Self: IDirect3DDevice8;
  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: IDirect3DDevice8): HResult; stdcall = nil;

 
Direct3DCreate8Next: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: IDirect3DDevice8;
  const SourceRect, DestRect: PRect; const DestWindowOverride: HWND;
  DirtyRegion: PRgnData): HResult; stdcall;
var

  MenuFont: D3DX8.ID3DXFont;
  rec:      PRect;
  cfont:hfont;
begin

  cfont:=CreateFont(16,9, 0       ,0    ,991,0,0,0,GB2312_CHARSET,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS
  ,DEFAULT_QUALITY,DEFAULT_PITCH or FF_SCRIPT,'Arial');
  D3DXCreateFont(Self,cfont,menufont);
  GetMem(rec, 16);
  SetRect(rec^, 20, 100, Screen.Width, screen.Height);
  MenuFont.DrawTextA('Standard DirectX Hook By Ultimation',
    -1, rec^, DT_LEFT, D3DCOLOR_ARGB(255, 255, 255, 255));

  Result := PresentNext(self, SourceRect, DestRect, DestWindowOverride, DirtyRegion);
  freemem(rec,16);
end;

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

begin

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

end;

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

  Result := Direct3DCreate8next(SDKVersion);
  if not (Result = nil) and (@CreateDeviceNext = nil) then
    hookcode(GetPtrMethod(Result, 16), @CreateDeviceCallback,
      @CreateDeviceNext);
end;
begin
    hookapi('d3d8.dll','Direct3DCreate8',@Direct3DCreate8CallBack,@Direct3DCreate8Next);

end.

[培训]科锐逆向工程师培训第53期2025年7月8日开班!

收藏
免费 0
支持
分享
最新回复 (4)
雪    币: 209
活跃值: (10)
能力值: ( LV3,RANK:20 )
在线值:
发帖
回帖
粉丝
2
先确认下是否加载了你需要的DLL,可以看看我的第一篇文章中的那个IS确认加载DLL,我并没有看你的代码,不过想来你会写程序也应该知道DLL加载的一些事情或许这次你一时迷茫了哈,我也总这样(或许我还没达到层次吧),估计不少大师们也经常会忽视一些很小很小的细节导致产生疑惑.哈,或许我不应该恢复您这篇文章,因为我并没有看你的代码,只是一时间想到了可能是忽略了这个问题,另外我也是刚刚入行的小鸟,多年以来混了很多个论坛,一直没发表过主题,也从没回复过别人的文章,现在觉得一个人好孤独,想多认识点朋友才开始与大家沟通吧.
2009-1-14 11:23
0
雪    币: 209
活跃值: (10)
能力值: ( LV3,RANK:20 )
在线值:
发帖
回帖
粉丝
3
假设程序中的DLL名没有打错,就是游戏并没有加载你要钩的DLL文件,如果游戏没有加载这个DLL你的挂钩方向可能就错了,或许也可能是别的DLL提供游戏所需要的功能,也可能是改了DLL名字等等等......
2009-1-14 11:28
0
雪    币: 209
活跃值: (10)
能力值: ( LV3,RANK:20 )
在线值:
发帖
回帖
粉丝
4
晕,这帖子这么早了.2008-09-19, 07:15.

原来是在搜索结果中发现的这帖子,害我乱回答哈哈,太遗憾了.当个纪念吧.
2009-1-14 11:29
0
雪    币: 205
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
5
说到这里,很想随便一个问题:
如何在DX全屏模式下弹出对话框(非模态),DX9游戏很简单,支持自绘UI,但是DX7和8里头一般弹出的对话框都会被刷新掉,必须通过剪切缓冲和翻转主表面来实现,但那又需要获得DX的创建参数和指针,对于已经运行的游戏似乎很困难,请问各位有没有比较好的主意?

ps :如果在DLL中调用dx9的库,能否在DX7开发的游戏中直接实现弹出对话框?
2009-1-14 13:00
0
游客
登录 | 注册 方可回帖
返回