谢谢各位关心!!!!<br>我已成功的用shell hook捕捉了窗口的创建事件,但任然有些问题,现提出来与大家讨论:<br> 在我使用了挂起hook之后,应用程序的创建事件我能捕捉,但,凡是在钩子挂起之后运行的<br>应用程序,无论什么类型,只要进行了minimize 操作,就会隐藏hide起来.且在taskbar上也<br>找不到.只有用alt+tab切换才会出现.........不知什么问题.我把原代码贴上,请大家帮忙<br>研究.<br><br>以下是测试程序的原代码<br>unit testmain;<br><br>interface<br><br>uses<br> Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br> StdCtrls,tlhelp32, AppEvnts;<br><br> <br>type<br> TForm1 = class(TForm)<br> Button1: TButton;<br> ListBox1: TListBox;<br> ApplicationEvents1: TApplicationEvents;<br> procedure Button1Click(Sender: TObject);<br> procedure FormCreate(Sender: TObject);<br> procedure FormClose(Sender: TObject; var Action: TCloseAction);<br> procedure ApplicationEvents1Message(var Msg: tagMSG;<br> var Handled: Boolean);<br> private<br> { Private declarations }<br> <br> public<br> { Public declarations }<br> end;<br><br>var<br> Form1: TForm1;<br> mymsg:dword;<br><br><br><br><br><br>type<br>EDLLLoadError=class(exception);<br> <br>implementation <br>{$R *.DFM} <br>function createhook:bool; external 'mydll.dll' ;<br>function freehook:bool; external 'mydll.dll' ;<br><br>var hookhandle:hhook=0;<br> oldwinproc
ointer;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br>close;<br>end;<br><br>function newproc(windowhandle:hwnd; themessage, paramw,paraml:longint):longint; stdcall;<br>var <br> pid:integer;<br> ps:tprocessentry32;<br> hp:thandle;<br> filename:string;<br> isend:bool;<br>begin<br> result:=0;<br> if themessage=mymsg then<br> begin<br> form1.listbox1.Items.Clear;<br> getwindowthreadprocessid(paramw,@pid);<br> hp:= createtoolhelp32snapshot(TH32CS_SNAPPROCESS,pid);<br> ps.dwsize:=sizeof(ps); <br> isend:=process32first(hp,ps);<br> while isend do <br> begin<br> filename:=ps.szExeFile; <br> form1.listbox1.Items.Add('w'+filename);<br> isend:=process32next(hp,ps);<br> end;<br> result:=0;<br> end<br> else<br> result:=callwindowproc(oldwinproc,form1.handle,themessage,paramw,paraml);<br>end;<br><br><br>procedure TForm1.FormCreate(Sender: TObject);<br><br>var lb:bool; <br>begin<br><br> mymsg:=registerwindowmessage('cbtcreatewndmessage');<br> oldwinproc:=pointer(setwindowlong(form1.handle,gwl_wndproc,longint(@newproc)));<br><br>lb:=createhook; <br>end;<br><br><br>procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);<br>var lb:bool;<br>begin<br>lb:=freehook;<br><br>end;<br><br>procedure TForm1.ApplicationEvents1Message(var Msg: tagMSG;<br> var Handled: Boolean);<br>var <br> pid:integer;<br> ps:tprocessentry32;<br> hp:thandle;<br> filename:string;<br> isend:bool;<br>begin<br> if msg.message=mymsg then<br> begin<br> form1.listbox1.Items.Clear;<br> getwindowthreadprocessid(msg.wParam,@pid);<br> hp:= createtoolhelp32snapshot(TH32CS_SNAPPROCESS,pid);<br> ps.dwsize:=sizeof(ps); <br> isend:=process32first(hp,ps);<br> while isend do <br> begin<br> filename:=ps.szExeFile; <br> form1.listbox1.Items.Add('w'+filename);<br> isend:=process32next(hp,ps);<br> end;<br> showwindow(msg.wparam,SW_normal);<br><br> end;<br> <br><br>end;<br><br>end.<br><br><br>以下是dll中的原代码<br>unit cbt;<br><br>interface<br><br>uses<br>messages,windows;<br><br>function createhook:bool;stdcall;<br>function freehook:bool;stdcall;<br>function cbtcreatewndhook( int: integer; // hook code<br> WPARAM: longint; // depends on hook code<br> LPARAM: longint // depends on hook code<br>  
:longint stdcall;<br><br><br><br> <br>implementation<br><br>var hookhandle:hhook=0;<br><br>function cbtcreatewndhook( int: integer; // hook code<br> WPARAM: longint; // depends on hook code<br> LPARAM: longint // depends on hook code<br>  
:longint ; <br>begin<br>result:=0;<br>if int=Hshell_WINDOWCREATED then<br> begin<br> lparam:=getwindowlong(wparam,GWL_STYLE);<br> postmessage(HWND_BROADCAST,registerwindowmessage('cbtcreatewndmessage'),wparam,lparam);<br> end<br>else<br> result:= callnexthookex(hookhandle,int,wparam,lparam);<br>end;<br><br>function createhook:bool; <br>begin<br>hookhandle:=setwindowshookex(wh_shell,cbtcreatewndhook,hinstance,0);<br>result:=hookhandle<>0;<br>end;<br><br>function freehook:bool;<br>begin<br>result:=unhookwindowshookex(hookhandle);<br>end;<br><br>end.<br><br>以下是dll接口程序的代码<br>library mydll;<br><br>{ Important note about DLL memory management: ShareMem must be the<br> first unit in your library's USES clause AND your project's (select<br> Project-View Source) USES clause if your DLL exports any procedures or<br> functions that pass strings as parameters or function results. This<br> applies to all strings passed to and from your DLL--even those that<br> are nested in records and classes. ShareMem is the interface unit to<br> the BORLNDMM.DLL shared memory manager, which must be deployed along<br> with your DLL. To avoid using BORLNDMM.DLL, pass string information<br> using PChar or ShortString parameters. }<br><br>uses<br> SysUtils,<br> Classes,<br> windows,<br> messages,<br> cbt in 'cbt.pas';<br><br>const<br> cbtgothandle=wm_user+101;<br><br>{$R *.RES}<br>exports<br> createhook,freehook,cbtcreatewndhook;<br><br>begin<br><br>end.