Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Multimedia (https://www.delphipraxis.net/16-multimedia/)
-   -   Delphi DirectX 8 Hook (https://www.delphipraxis.net/139065-directx-8-hook.html)

napsterxx 22. Aug 2009 18:13


DirectX 8 Hook
 
Peace alle zusammen. Ich probiere mich gerade erneut an einem DirectX 8 hook.
Zunächst eine Frage, welche ich glaube mir selbst beantworten zu können. Ich glaube, dass wenn ein spiel DirectX 8 benötigt, ich aber z.B. DirectX 10 installiert habe, logischerweise eigentlich DirectX 10 funktionen hooken muss oder?

Falls dies nicht der Fall sein sollte, warum funktioniert diese Unit, welche ich geschrieben, und in meine DLL eingebunden habe, nicht?
Delphi-Quellcode:
unit d3d8hook;

interface
uses Windows, uallHook;

function HookDirect3D8 : boolean;

implementation

var
newSetRenderState8: function(self: pointer; stateType, state: dword): dword; stdcall;

newCreateDevice8 : function(self, adapter, devType, wnd, flags, params, device: pointer) : dword; stdcall;

newDirect3DCreate8 : function(sdkVersion: dword) : dword; stdcall;

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

function SetRenderState8Callback(self: pointer; stateType, state: dword) : dword; stdcall;
begin
end;

function CreateDevice8Callback(self, adapter, devType, wnd, flags, params, device: pointer) : dword; stdcall;
begin
  result := newCreateDevice8(self, adapter, devType, wnd, flags, params, device);

  if result = 0 then
    if @newSetRenderState8 = nil then
      HookCode(GetInterfaceMethod(device^, 50), @SetRenderState8Callback, @newSetRenderState8);
end;

function Direct3DCreate8Callback(sdkVersion: dword) : dword; stdcall;
begin
  result := newDirect3DCreate8(sdkVersion);

  MessageBox(0, 'DEVICE CREATE 8', 'Hook', 0);

  if result <> 0 then
    if @newCreateDevice8 = nil then
      HookCode(GetInterfaceMethod(result, 15), @CreateDevice8Callback, @newCreateDevice8);
end;

function HookDirect3D8 : boolean;
begin
  HookApiIAT('d3d8.dll', 'Direct3DCreate8', @Direct3DCreate8Callback,@newDirect3DCreate8);
end;

end.
Bislang starte ich das Spiel, und injiziere danach die DLL - das funktioniert, allerdings bekomme ich die Message nicht angezeigt. Liegt das nun daran, dass der Prozess schon gestartet und die funktion Direct3DCreate8 schon ausgeführt wurde?

Liebe Grüße

napsterxx 23. Aug 2009 10:23

Re: DirectX 8 Hook
 
So habe wieder bissi weiter gesucht, und nun komme ich soweit, das mein Hook erfolgreich gesetzt wird - laut MessageBox
BlueHookLib.pas
Delphi-Quellcode:
library d3dxhook;

{ Important note about DLL memory management: ShareMem must be the
  first unit in your library's USES clause AND your project's (select
  Project-View Source) USES clause if your DLL exports any procedures or
  functions that pass strings as parameters or function results. This
  applies to all strings passed to and from your DLL--even those that
  are nested in records and classes. ShareMem is the interface unit to
  the BORLNDMM.DLL shared memory manager, which must be deployed along
  with your DLL. To avoid using BORLNDMM.DLL, pass string information
  using PChar or ShortString parameters. }

uses
  Windows,
  SysUtils,
  BlueHookLib,
  BlueFileLog,
  Direct3D8,
  d3dx8;

{$R *.res}

type
  Direct3DCreate8 = function( D3D_SDK_VERSION: cardinal ): IDirect3D8; stdcall;

var
  oldDirect3DCreate8 : Direct3DCreate8;
  newDirect3DCreate8 : Direct3DCreate8;

  pIDirect3D8      : IDirect3D8;

function callbackDirect3DCreate8( D3D_SDK_VERSION: cardinal ): IDirect3D8; stdcall;
begin
  Result := oldDirect3DCreate8(D3D_SDK_VERSION);
  MessageBox(0, 'callbackDirect3DCreate8', 'Information', MB_ICONINFORMATION);
end;

procedure DllMain(dwReason: DWord);
var
  gHook : CDeepHook;              
  gLog : CDeepLog;
begin
  case dwReason of
    DLL_PROCESS_ATTACH:
    begin
      gHook := CDeepHook.Create(gLog);
      oldDirect3DCreate8 := GetProcAddress(GetModuleHandle('d3d8.dll'), 'Direct3DCreate8');
      if not(gHook.HookJMP( @oldDirect3DCreate8, @callbackDirect3DCreate8, @newDirect3DCreate8)) then
        MessageBox(0, 'Direct3DCreate8 hooking failed', 'Information', MB_ICONINFORMATION);
    end;
  end;
end;

begin
  DllProc := @DllMain;
  DllMain(DLL_PROCESS_ATTACH);
end.
Also ich bekomme eine Message: "Direct3DCreate8 hooked", jedoch keine "callbackDirect3DCreate8". Woran könnte das liegen?

Zacherl 23. Aug 2009 12:14

Re: DirectX 8 Hook
 
Sicher, dass das DX8 und nicht DX9 verwendet wird? Funktioniert die Hook Unit korrekt?

napsterxx 23. Aug 2009 12:30

Re: DirectX 8 Hook
 
Ja sicher. Habe es mit der uAllCollection auch schon probiert.
Hook erfolgreich aber die Callbackfunktion wird nie aufgerufen.

Zacherl 23. Aug 2009 13:04

Re: DirectX 8 Hook
 
Vermutlich injizierst du die DLL nachdem die API schon aufgerufen wurde. Der normale Weg wäre, dass du den Zielprozess supendiert startest und dann direkt deine DLL injizierst.

turboPASCAL 23. Aug 2009 13:34

Re: DirectX 8 Hook
 
Du kannst dir auch das mal zu geühte führen (www.gamedev.net)

napsterxx 23. Aug 2009 13:53

Re: DirectX 8 Hook
 
Also das ganze ist so.
Ich injiziere eine DLL in die Anwendung die das Spiel startet. In der ersten DLL hooke ich CreateProcessW. Wenn dies Aufgerufen wird, ändere ich den Flag zu SUSPEND, injiziere die DLL in das Spiel und danach lasse ich den Thread weiter laufen.

Das ganze sieht so aus.
Delphi-Quellcode:
function CallbackCreateProcess( lpApplicationName: PWideChar; lpCommandLine: PWideChar;
                                lpProcessAttributes, lpThreadAttributes: PSecurityAttributes;
                                bInheritHandles: BOOL; dwCreationFlags: DWORD; lpEnvironment: Pointer;
                                lpCurrentDirectory: PWideChar; const lpStartupInfo: TStartupInfoW;
                                var lpProcessInformation: TProcessInformation): BOOL; stdcall;
begin
  if (Pos('vertices',ExtractFileName(lpCommandLine)) > 0) then
  begin
    Result := newCreateProcess(lpApplicationName, lpCommandLine,
                                lpProcessAttributes, lpThreadAttributes,
                                bInheritHandles, CREATE_SUSPENDED,
                                lpEnvironment, lpCurrentDirectory,
                                lpStartupInfo,
                                lpProcessInformation);

    uAllHook.InjectLibrary(lpProcessInformation.dwProcessId, PChar(ExtractFilePath(GetModulePath) + 'd3dxhook.dll'));

    ResumeThread(lpProcessInformation.dwThreadId);
  end else
    Result := newCreateProcess(lpApplicationName, lpCommandLine,
                                lpProcessAttributes, lpThreadAttributes,
                                bInheritHandles, dwCreationFlags,
                                lpEnvironment, lpCurrentDirectory,
                                lpStartupInfo,
                                lpProcessInformation);
end;
Meine d3dxhook.dll sieht so aus:
Delphi-Quellcode:
 library d3dxhook;

{ Important note about DLL memory management: ShareMem must be the
  first unit in your library's USES clause AND your project's (select
  Project-View Source) USES clause if your DLL exports any procedures or
  functions that pass strings as parameters or function results. This
  applies to all strings passed to and from your DLL--even those that
  are nested in records and classes. ShareMem is the interface unit to
  the BORLNDMM.DLL shared memory manager, which must be deployed along
  with your DLL. To avoid using BORLNDMM.DLL, pass string information
  using PChar or ShortString parameters. }

uses
  Windows,
  SysUtils,
  Direct3D8,
  d3dx8,
  uAllHook;

{$R *.res}

type
  Direct3DCreate8 = function( D3D_SDK_VERSION: cardinal ): IDirect3D8; stdcall;

var
  oldDirect3DCreate8 : Direct3DCreate8;
  newDirect3DCreate8 : Direct3DCreate8;

  pIDirect3D8      : IDirect3D8;

function callbackDirect3DCreate8( D3D_SDK_VERSION: cardinal ): IDirect3D8; stdcall;
begin
  Result := oldDirect3DCreate8(D3D_SDK_VERSION);
  MessageBox(0, 'callbackDirect3DCreate8', 'Information', MB_ICONINFORMATION);
end;

procedure DllMain(dwReason: DWord);
begin
  case dwReason of
    DLL_PROCESS_ATTACH:
    begin
      oldDirect3DCreate8 := GetProcAddress(GetModuleHandle('d3d8.dll'), 'Direct3DCreate8');
      if not(HookCode( @oldDirect3DCreate8, @callbackDirect3DCreate8, @newDirect3DCreate8)) then
        MessageBox(0, 'Direct3DCreate8 hooking failed', 'Information', MB_ICONINFORMATION);
    end;
  end;
end;

begin
  DllProc := @DllMain;
  DllMain(DLL_PROCESS_ATTACH);
end.
Direct3DCreate8 hooking failed bekomme ich als Message, danach startet das Game. Wenn ich nur das Spiele starte, und nicht das Programm nehme was dieses eigentlich starten soll wird der Hook gesetzt. Also ist wohl irgendwas bei dem erstellen des Prozesses schief gelaufen - aber was?

Es muss aber auch ohne dieses Suspend funktionieren. Xfire, und die BNet banlist, welche glaube ich von einem aus diesem Board entwickelt wurde, schaffen es beide, nachdem das Spiel gestartet ist, sich darein zu hooken.

Den Thread habe ich mir schon zu Gemühte geführt aber C++ Kentnisse gehen gegen null.

Zacherl 23. Aug 2009 15:45

Re: DirectX 8 Hook
 
Code:
oldDirect3DCreate8 := GetProcAddress(GetModuleHandle('d3d8.dll'), 'Direct3DCreate8');
      if not(HookCode( @oldDirect3DCreate8, @callbackDirect3DCreate8, @newDirect3DCreate8)) then
        MessageBox(0, 'Direct3DCreate8 hooking failed', 'Information', MB_ICONINFORMATION);
mache mal zu

Code:
oldDirect3DCreate8 := GetProcAddress(LoadLibrary('d3d8.dll'), 'Direct3DCreate8');
      if not(HookCode( @oldDirect3DCreate8, @callbackDirect3DCreate8, @newDirect3DCreate8)) then
        MessageBox(0, 'Direct3DCreate8 hooking failed', 'Information', MB_ICONINFORMATION);
und

Code:
function callbackDirect3DCreate8( D3D_SDK_VERSION: cardinal ): IDirect3D8; stdcall;
begin
  Result := oldDirect3DCreate8(D3D_SDK_VERSION);
  MessageBox(0, 'callbackDirect3DCreate8', 'Information', MB_ICONINFORMATION);
end;
wird zu

Code:
function callbackDirect3DCreate8( D3D_SDK_VERSION: cardinal ): IDirect3D8; stdcall;
begin
  Result := newDirect3DCreate8(D3D_SDK_VERSION);
  MessageBox(0, 'callbackDirect3DCreate8', 'Information', MB_ICONINFORMATION);
end;


Alle Zeitangaben in WEZ +1. Es ist jetzt 21:54 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz