Sign in to follow this  
prux

Direct3D freezes from DLL

Recommended Posts

hey all,

in this example, I need to hook an app (who could have thought that? :crazy:) with the attached code I need a window and Direct3D, since it must render to a standallone window (and must D3D).
The attaching into it is OK. But when I try to get rid of it, D3D freezes.

[quote]{$IMAGEBASE $57000000}
{$APPTYPE CONSOLE}
{$R+,D+,Q+,S+,I+}
program Application;
uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, MadCodeHook;

var
si : TStartupInfo;
pi : TProcessInformation;

begin
FillChar (si, sizeof(si), 0);
CreateProcessEx (nil, 'hookme.exe', nil, nil, false, 0, nil, nil, si, pi, 'hook.dll');
end.[/quote]

[quote]{$R+,D+,Q+,S+,I+}
library Hook;

uses
Windows,
Dialogs,
Sysutils,
Forms,
Direct3D9;

var
myForm : TForm = nil;
VideoCardInfo : TD3DDISPLAYMODE;
D3DInit : TD3DPRESENT_PARAMETERS;
mode : TD3DDisplayMode;
Direct3D : IDirect3D9;
D3DDevice : IDirect3DDevice9;
D3DBBuffer : IDirect3DSurface9;

procedure DllMain(dwReason: DWord);
begin
if (dwReason = DLL_PROCESS_ATTACH) then
begin
MyForm:= TForm.Create (nil);
MyForm.Width:= 640;
MyForm.Height:= 480;
MyForm.Position:= poScreenCenter;
MyForm.Show;

Direct3D:= Direct3DCreate9 (D3D_SDK_VERSION);
Direct3D.GetAdapterDisplayMode (D3DADAPTER_DEFAULT, mode);
FillChar (VideoCardInfo, SizeOf(VideoCardInfo), 0);
FillChar (D3DInit, SizeOf(D3DInit), 0);
WITH D3DInit DO
BEGIN
Windowed:= TRUE;
BackBufferWidth:= 640;
BackBufferHeight:= 480;
BackBufferCount:= 1;
BackBufferFormat:= Mode.Format;
SwapEffect:= D3DSWAPEFFECT_DISCARD;
hDeviceWindow:= MyForm.Handle;
END;
Direct3D.CreateDevice (D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, MyForm.Handle, D3DCREATE_SOFTWARE_VERTEXPROCESSING, @D3DInit, D3DDevice);
D3DDevice.GetBackBuffer (0, 0, D3DBACKBUFFER_TYPE_MONO, D3DBBuffer);
end;

if (dwReason = DLL_PROCESS_DETACH) then
begin
D3DBBuffer:= nil; messagebeep (1000); sleep (1000);
D3DDevice:= nil; [b][i][u]<-- FREEZE HERE[/u][/i][/b] messagebeep (1000); sleep (1000);
Direct3D:= nil; messagebeep (1000); sleep (1000);
if Assigned(MyForm) then MyForm.Free;
MyForm:= nil;
end;
end;

begin
DllProc := TDLLProc(@DllMain);
DllMain (DLL_PROCESS_ATTACH);
end.[/quote]

So it freezes at DLL_PROCESS_DETACH part, the hooked application remains in the memory. Does anyone know why? Very simple and short program and I cant figure it out

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this