Einzelnen Beitrag anzeigen

GOOFY009

Registriert seit: 24. Okt 2009
20 Beiträge
 
#3

Re: D3D9 Hook bzw. als "proxy.dll"

  Alt 25. Okt 2009, 16:49
Hi!

Danke für den Hinweis. Der Fehler hatte sich wohl durchs viele "Herumprobieren" eingeschlichen.
Hier der nun funktionierende Code:

Delphi-Quellcode:
library d3d9;

uses
  Windows,
  SysUtils,
  Classes,
  DirectCreate in 'DirectCreate.pas';

{$R *.res}

exports

Direct3DCreate9;

procedure DLLEntryPoint(reason : Integer);
begin
  case reason of
    DLL_PROCESS_DETACH :
      begin
        ExitInstance;
      end;
    DLL_PROCESS_ATTACH :
      begin
        InitInstance;
      end;
    DLL_THREAD_DETACH : exit;
    DLL_THREAD_ATTACH : exit;
  end;
end;

begin
  DLLProc := @DLLEntryPoint;
  DLLEntryPoint(DLL_PROCESS_ATTACH);
end.
Delphi-Quellcode:
unit DirectCreate;

interface

uses
  Windows,
  SysUtils,
  Classes,
  ShareMem,
  Direct3D9,
  d3dx9;

var
  gl_hOriginalDll : THandle;

  Direct3DCreate9original: function(SDKVersion: cardinal): Pointer; stdcall;
  function Direct3DCreate9(SDKVersion: cardinal): Pointer; stdcall;

  procedure LoadOriginalDLL;
  procedure InitInstance;
  procedure ExitInstance;

implementation

function Direct3DCreate9(SDKVersion: LongWord): Pointer;
begin
  if gl_hOriginalDll = 0 then LoadOriginalDLL;
  @Direct3DCreate9original := GetProcAddress(gl_hOriginalDll, 'Direct3DCreate9');;
  result := Direct3DCreate9original(SDKVersion);
end;

procedure LoadOriginalDLL;
var
  SystemPath: array[0..MAX_PATH] of char;
  SysDir : String;
begin
  GetSystemDirectory(SystemPath, SizeOf(SystemPath));
  SysDir := SystemPath+'\d3d9.dll';
  gl_hOriginalDll := LoadLibrary(PChar(SysDir));
end;

procedure InitInstance;
begin
  gl_hOriginalDll := 0;
end;

procedure ExitInstance;
begin
  if gl_hOriginalDll <> 0 then begin
    FreeLibrary(gl_hOriginalDll);
    gl_hOriginalDll := 0;
  end;
end;

end.
Das funktioniert nun soweit. Jetzt müsste ich noch heraus bekommen, wie man das IDirect3D9 überschreibt um Zugriff auf die einzellnen Funktionen zu bekommen. Das Aufrufende Programm bekomm ja einen Pointer(xxxxx) of IDirect3D9 zurück ?!

Der C++ Aufruf sieht so aus :
Code:
   // Create my IDirect3D8 object and store pointer to original object there.
   // note: the object will delete itself once Ref count is zero (similar to COM objects)
   gl_pmyIDirect3D9 = new myIDirect3D9(pIDirect3D9_orig);
   
   // Return pointer to hooking Object instead of "real one"
   return (gl_pmyIDirect3D9);
Un die dazu gehörige Klasse (nur ein Teil sonst wird es zu lang):
Code:
// myIDirect3D9.cpp

#include "StdAfx.h"

myIDirect3D9::myIDirect3D9(IDirect3D9 *pOriginal)
{
    m_pIDirect3D9 = pOriginal;
}

myIDirect3D9::~myIDirect3D9(void)
{
}

HRESULT __stdcall myIDirect3D9::QueryInterface(REFIID riid, void** ppvObj)
{
    *ppvObj = NULL;

   // call this to increase AddRef at original object
   // and to check if such an interface is there

   HRESULT hRes = m_pIDirect3D9->QueryInterface(riid, ppvObj);

   if (hRes == NOERROR) // if OK, send our "fake" address
   {
      *ppvObj = this;
   }
   
   return hRes;
}

ULONG   __stdcall myIDirect3D9::AddRef(void)
{
    return(m_pIDirect3D9->AddRef());
}

ULONG   __stdcall myIDirect3D9::Release(void)
{
    extern myIDirect3D9* gl_pmyIDirect3D9;

   // call original routine
   ULONG count = m_pIDirect3D9->Release();
   
    // in case no further Ref is there, the Original Object has deleted itself
   // so do we here
   if (count == 0)
   {
      gl_pmyIDirect3D9 = NULL;
         delete(this);
   }

   return(count);
}

HRESULT __stdcall myIDirect3D9::RegisterSoftwareDevice(void* pInitializeFunction)
{
    return(m_pIDirect3D9->RegisterSoftwareDevice(pInitializeFunction));
}

UINT __stdcall myIDirect3D9::GetAdapterCount(void)
{
    return(m_pIDirect3D9->GetAdapterCount());
}
Wie bereits erwähnt... bin für jede Hilfe / Anregung offen...

CU
GOOFY
  Mit Zitat antworten Zitat