首页
社区
课程
招聘
[求助](delphi)为什么这种情况下会建立无数的窗口?
2008-2-26 15:01 4517

[求助](delphi)为什么这种情况下会建立无数的窗口?

2008-2-26 15:01
4517
本人新手,学习delphi通过远程钩子向目标进程注入dll,dll中封装了一个窗体,注入目标程序成功后,在目标程序上按下END键呼出这个窗体,我这个程序写的非常简单,仅仅是在dll文件里封装了一个窗体,主程序把dll文件注入目标进程后通过热键END把窗口呼出就算成功。

目前我遇到的问题:在注入所有的不涉及到3D渲染方面程序,呼出都成功了;但是,一旦涉及到程序本身会带3D的——我试验了很多3D游戏,魔兽3,CS1.6等等,一按热键只见呼出窗口象发疯一样不断的生成,这是为什么?为什么会这样,难道3D渲染下建立窗口有什么特别之处?

源代码

dll的代码:
工程文件代码
==========
library   hook32;

uses
SysUtils,
Forms,
Classes,
myDLl   in   'myDLl.pas'   {Form1};

{$R   *.res}

exports
HookOn,HookOff;

begin
{Application.Initialize;
Application.Run;   }
end.
==========

dll主文件代码
==========
unit   mydll;

interface

uses
Windows,   Messages,   SysUtils,   Variants,   Classes,   Graphics,   Controls,   Forms,
Dialogs,   StdCtrls,   ExtCtrls   ,   ComCtrls;

type
TForm1   =   class(TForm)
Button1:   TButton;
private
{   Private   declarations   }
public
{   Public   declarations   }
end;

var
Form1:   TForm1;
function   HookProc(nCode:Integer;WParam:   WPARAM;LParam:LPARAM):LRESULT;stdcall;
function   HookOn(lpHwnd:HWND;lpType:Longint):Longint;stdcall;export;
function   HookOff:Boolean;stdcall;export;

implementation

var
hHk:   HHOOK=0;   //钩子的句柄
hThread:   Cardinal;   //长整形数据,窗口线程的标示符
hmod:   Pointer;   //进程标示符的指针

{$R   *.dfm}
function   HookProc(nCode:Integer;WParam:   WPARAM;LParam:LPARAM):LRESULT;stdcall;
begin
if   (wParam=VK_END)   and   ((LParam   and   $40000000)   <>   0)   then
begin
Form1:=TForm1.Create(Application);
Form1.show;
end;
Result   :=0   //CallNextHookEx(hHk,nCode,WParam,LParam);
end;

function   HookOn(lpHwnd:HWND;lpType:Longint):   Longint;stdcall;   export;
begin
hThread   :=GetWindowThreadProcessId(lpHwnd,hmod);
//注入开始
hHk   :=SetWindowsHookEx(lpType,@HookProc,hInstance,hThread);   //   WH_KEYBOARD
Result   :=hHk
end;

function   HookOff:Boolean;stdcall;   export;
begin
if   hHk <> 0   then
begin
//移除挂钩
UnHookWindowsHookEx(hHk);
hHk   :=0;
Result   :=true;
end
else
Result   :=false;
end;

end.
==========

以下是调用dll的程序的代码
==========
unit   Unit1;

interface

uses
Windows,   Messages,   SysUtils,   Variants,   Classes,   Graphics,   Controls,   Forms,
Dialogs,   StdCtrls,   ExtCtrls,   XPMan;

type
TForm1   =   class(TForm)
Button1:   TButton;
Button2:   TButton;
procedure   FormClose(Sender:   TObject;   var   Action:   TCloseAction);
procedure   Button1Click(Sender:   TObject);
procedure   Button2Click(Sender:   TObject);

private
{   Private   declarations   }
public
{   Public   declarations   }
end;

var
Form1:   TForm1;
function   HookOn(lpHwnd:HWND;lpType:Longint):Longint;stdcall;external   'Hook32.dll'   name   'HookOn';
function   HookOff:Boolean;stdcall;external   'Hook32.dll'   name   'HookOff';
implementation

{$R   *.dfm}

procedure   TForm1.FormClose(Sender:   TObject;   var   Action:   TCloseAction);
begin
hookoff;
end;

procedure   TForm1.Button1Click(Sender:   TObject);
var

h1:HWND;
begin

h1:=FindWindow(NIL,'counter-strike');//这是窗口的句柄,要自己找到后,填写入。
if   h1=0   then   showmessage('没找到进程!');
if   h1> 0   then   showmessage('找到进程!');
sleep(200);   
HookOn(h1,WH_KEYBOARD);

end;

procedure   TForm1.Button2Click(Sender:   TObject);
begin
HookOff;
end;

end.
==========

[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课

收藏
点赞0
打赏
分享
最新回复 (4)
雪    币: 208
活跃值: (40)
能力值: ( LV3,RANK:20 )
在线值:
发帖
回帖
粉丝
justlovemm 2008-2-27 12:43
2
0
仔细看了一下代码,没有发现会引起你说的现象的问题。不过你怎么确认是在不停的生成窗口,而不是窗口在不停的闪烁呢?
雪    币: 200
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
abcxiawei 2008-2-27 17:13
3
0
回楼上,正常情况下,比如我注入到普通的程序,按下end键,蹦出一个窗口,同时,任务栏上会多出一个程序来。如果我在3D游戏下注入,按下end键,蹦出的窗口不停的闪烁,然后任务栏上这个窗口代表的程序不断增殖,就像IE中病毒不断弹窗口那样,任务栏上一下就排满了
雪    币: 208
活跃值: (40)
能力值: ( LV3,RANK:20 )
在线值:
发帖
回帖
粉丝
justlovemm 2008-2-28 11:54
4
0
在Hook函数里加个SetKeyBoradState试试。
雪    币: 884
活跃值: (4095)
能力值: ( LV3,RANK:20 )
在线值:
发帖
回帖
粉丝
kagayaki 2008-2-28 12:09
5
0
function   HookProc(nCode:Integer;WParam:   WPARAM;LParam:LPARAM):LRESULT;stdcall;
begin
if   (wParam=VK_END)   and   ((LParam   and   $40000000)   <>   0)   then
begin
  if Form1 =nil then
  begin
  Form1:=TForm1.Create(Application);
  Form1.show;
  end
  else......
end;
Result   :=0   //CallNextHookEx(hHk,nCode,WParam,LParam);

  if nCode < 0 then   //必须将消息传递到消息链的下一个接收单元
    Result := CallNextHookEx(hHk, // hHook值是SetWindowsHookEx()的传回值
                             nCode, wParam, lParam);   //nCode、wParam、lParam则是Hook函数中的三个参数
end;
游客
登录 | 注册 方可回帖
返回