hook DrawIndexedPrimitive

c++ / delphi package - dll injection and api hooking
Post Reply
johndoe
Posts: 9
Joined: Thu Mar 01, 2012 2:09 am

hook DrawIndexedPrimitive

Post by johndoe »

Hi,
I want to hook the DrawIndexedPrimitive API from Direct3D9, but I have no idea how to do this..

Can anyone help-me?

Thanks
madshi
Site Admin
Posts: 10764
Joined: Sun Mar 21, 2004 5:25 pm

Re: hook DrawIndexedPrimitive

Post by madshi »

This demo should show you the basics:

http://madshi.net/HookDirect3D.zip
johndoe
Posts: 9
Joined: Thu Mar 01, 2012 2:09 am

Re: hook DrawIndexedPrimitive

Post by johndoe »

Thanks madshi. :P
But, can you explain me about Direct3D hook?

like this..

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

this parameter are not listed in MSDN..

http://msdn.microsoft.com/en-us/library ... 85%29.aspx

Why?
madshi
Site Admin
Posts: 10764
Joined: Sun Mar 21, 2004 5:25 pm

Re: hook DrawIndexedPrimitive

Post by madshi »

Because it's not a simple function, but a class/interface method. All class/interface methods have a hidden first "Self" or "This" parameter.
johndoe
Posts: 9
Joined: Thu Mar 01, 2012 2:09 am

Re: hook DrawIndexedPrimitive

Post by johndoe »

Hi madshi,

Can you tell me what's wrong with this hook?

Code: Select all

function GenerateTexture(pD3Ddev: IDirect3DDevice9; ppD3Dtex: IDirect3DTexture9; colour32: dword):HRESULT;
var
 colour16: word;
 d3dlr: D3DLOCKED_RECT;
 pDst16: pword;
 xy: integer;
begin
 if failed(pD3Ddev.CreateTexture(8, 8, 1, 0, D3DFMT_A4R4G4B4, D3DPOOL_MANAGED, ppD3Dtex, nil)) then begin
  result := E_FAIL;
  exit;
 end;

 colour16 :=  (word(((colour32 shr 28)and $F) shl 12)
           or word((((colour32 shr 20)and $F) shl 8))
           or word((((colour32 shr 12)and $F) shl 4))
           or word((((colour32 shr 4)and $F) shl 0)));

  ppD3Dtex.LockRect(0, d3dlr, nil, 0);
  pDst16 := PWORD(d3dlr.pBits);
  xy:=0;
  while xy<(8*8) do begin
   Inc(pDst16^);
   pDst16^ := colour16;
   inc(xy);
  end;
  ppD3Dtex.UnlockRect(0);

  Result := S_OK;
end;

function DrawIndexedPrimitiveCallback(pDevice: IDIRECT3DDEVICE9; _Type: TD3DPRIMITIVETYPE; BaseVertexIndex: integer; MinVertexIndex: LongInt; NumVertices: LongInt; startIndex: LongInt; primCount: LongInt): HRESULT; stdcall;
var
 Stream_Data: IDIRECT3DVERTEXBUFFER9;
 Offset: cardinal;
 Stride: cardinal;
begin
  asm
   mov ecx,[ebp+32]
   mov edx,[ebp+28]
   PUSHAD;
  end;

  if generate then begin
     GenerateTexture(pDevice, texGreen , D3DCOLOR_ARGB(255,0,255,0));
     GenerateTexture(pDevice, texRed , D3DCOLOR_ARGB(255,255,0,0));
     GenerateTexture(pDevice, texBlue , D3DCOLOR_ARGB(255,0,0,255));
     GenerateTexture(pDevice, texOrange , D3DCOLOR_ARGB(255,255,165,0));
     GenerateTexture(pDevice, texYellow , D3DCOLOR_ARGB(255,255,255,0));
     GenerateTexture(pDevice, texPink , D3DCOLOR_ARGB(255,255,192,203));
     GenerateTexture(pDevice, texCyan , D3DCOLOR_ARGB(255,0,255,255));
     GenerateTexture(pDevice, texPurple , D3DCOLOR_ARGB(255,160,32,240));
     GenerateTexture(pDevice, texBlack , D3DCOLOR_ARGB(255,0,0,0));
     GenerateTexture(pDevice, texWhite , D3DCOLOR_ARGB(255,255,255,255));
     GenerateTexture(pDevice, texSteelBlue , D3DCOLOR_ARGB(255,33,104,140));
     GenerateTexture(pDevice, texLightSteelBlue, D3DCOLOR_ARGB(255,201,255,255));
     GenerateTexture(pDevice, texLightBlue , D3DCOLOR_ARGB(255,26,140,306));
     GenerateTexture(pDevice, texSalmon , D3DCOLOR_ARGB(255,196,112,112));
     GenerateTexture(pDevice, texBrown , D3DCOLOR_ARGB(255,168,99,20));
     GenerateTexture(pDevice, texTeal , D3DCOLOR_ARGB(255,38,140,140));
     GenerateTexture(pDevice, texLime , D3DCOLOR_ARGB(255,50,205,50));
     GenerateTexture(pDevice, texElectricLime , D3DCOLOR_ARGB(255,204,255,0));
     GenerateTexture(pDevice, texGold , D3DCOLOR_ARGB(255,255, 215, 0));
     GenerateTexture(pDevice, texOrangeRed , D3DCOLOR_ARGB(255,255,69,0));
     GenerateTexture(pDevice, texGreenYellow , D3DCOLOR_ARGB(255,173,255,47));
     GenerateTexture(pDevice, texAquaMarine , D3DCOLOR_ARGB(255,127,255,212));
     GenerateTexture(pDevice, texSkyBlue , D3DCOLOR_ARGB(255,0,191,255));
     GenerateTexture(pDevice, texSlateBlue , D3DCOLOR_ARGB(255,132, 112, 255));
     GenerateTexture(pDevice, texCrimson , D3DCOLOR_ARGB(255,220,20,60));
     GenerateTexture(pDevice, texDarkOliveGreen, D3DCOLOR_ARGB(255,188,238,104 ));
     GenerateTexture(pDevice, texPaleGreen , D3DCOLOR_ARGB(255,154,255, 154));
     GenerateTexture(pDevice, texDarkGoldenRod , D3DCOLOR_ARGB(255,255, 185, 15 ));
     GenerateTexture(pDevice, texFireBrick , D3DCOLOR_ARGB(255,255,48,48));
     GenerateTexture(pDevice, texDarkBlue , D3DCOLOR_ARGB(255,0,0,204));
     GenerateTexture(pDevice, texDarkerBlue , D3DCOLOR_ARGB(255,0,0,153));
     GenerateTexture(pDevice, texDarkYellow , D3DCOLOR_ARGB(255,255,204,0));
     GenerateTexture(pDevice, texLightYellow , D3DCOLOR_ARGB(255,255,255,153));
     Generate := false;
  end;

  if (pDevice.GetStreamSource(0, Stream_Data, Offset, Stride) = D3D_OK) then Stream_Data._Release();

  if Stride=44 then begin
    pDevice.SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
    pDevice.SetTexture(0, texLightBlue);
    DrawIndexedPrimitiveNext(pDevice, _Type, BaseVertexIndex,MinVertexIndex,numVertices,StartIndex,primCount);
    pDevice.SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
  end;

  asm
   popad
  end;

  Result := DrawIndexedPrimitiveNext(pDevice, _Type, BaseVertexIndex,MinVertexIndex,numVertices,StartIndex,primCount);
end;

begin
 HookAPI('d3d9.dll', 'Direct3DCreate9', @Direct3DCreate9Callback, @Direct3DCreate9Next);
end;
thx
madshi
Site Admin
Posts: 10764
Joined: Sun Mar 21, 2004 5:25 pm

Re: hook DrawIndexedPrimitive

Post by madshi »

I don't know. Why don't you tell me what happens? Do you get a blue screen? Does your computer explode? Without a proper problem description I won't be able to help you.
johndoe
Posts: 9
Joined: Thu Mar 01, 2012 2:09 am

Re: hook DrawIndexedPrimitive

Post by johndoe »

simply returns FAILED hook
madshi
Site Admin
Posts: 10764
Joined: Sun Mar 21, 2004 5:25 pm

Re: hook DrawIndexedPrimitive

Post by madshi »

Who returns what where? Look, if you want my help, you *really* need to get the quality of your problem description way up. I can't read your mind.
johndoe
Posts: 9
Joined: Thu Mar 01, 2012 2:09 am

Re: hook DrawIndexedPrimitive

Post by johndoe »

madshi,

I'm using:

Code: Select all

 if not HookAPI('d3d9.dll', 'Direct3DCreate9', @Direct3DCreate9Callback, @Direct3DCreate9Next) then ShowMessage('hook failed');
and return this showmessage.. understand? Isn't hooking the API...
madshi
Site Admin
Posts: 10764
Joined: Sun Mar 21, 2004 5:25 pm

Re: hook DrawIndexedPrimitive

Post by madshi »

Ah, the "ShowMessage('hook failed')" was not in your previous post, so I didn't know where it failed exactly.

What does GetLastError say?

What happens if you recompile the HookDirect3D demo as it is? Does that work?
Post Reply