谁来帮我看看我Hook窗口创建的代码有什么错误?能Hook成功,只是会导致资源管理器错误(100分)

T

testman

Unregistered / Unconfirmed
GUEST, unregistred user!
以下贴出全部代码,仅仅是监视窗口创建并写出log文件。Dll加载,卸载,挂钩,解钩都能正常打出log,并且成对儿出现。 <br>该释放的都释放了。 <br>实在不解,为什么还是会导致资源管理器(explorer.exe)和任务管理器(taskmgr.exe)出错。 <br>hook过程中,没有任何错误。只是在把主程序关掉后,才可能出现错误。各位测试的时候注意一下,谢谢。 <br>初次发贴,有不合适之处还请多多谅解。<br>按照如下方式操作,就会出现错误:<br>先打开任务管理器(别关掉),执行主程序,挂钩,打开几个有窗口的程序(别关掉),退出主程序。 <br>任务栏鼠标右键-&gt;任务管理器,出错。 <br><br>我这里只要这么操作,就一定出错。<br><br>library makeErr;<br>uses &nbsp;<br>&nbsp; Messages, &nbsp; <br>&nbsp; Classes, &nbsp; <br>&nbsp; SysUtils, &nbsp; <br>&nbsp; Windows, &nbsp; <br>&nbsp; Dialogs, &nbsp; <br>&nbsp; ComServ, &nbsp; <br>&nbsp; theMain in 'theMain.pas'; &nbsp; <br>exports &nbsp;<br>&nbsp; DllGetClassObject, &nbsp; <br>&nbsp; DllCanUnloadNow, &nbsp; <br>&nbsp; DllRegisterServer, &nbsp; <br>&nbsp; DllUnregisterServer; &nbsp; <br>{$R *.RES} &nbsp;<br>const &nbsp;<br>&nbsp; HookMemFileName = 'DllHookMemFile.DTA'; &nbsp; <br>var PShare: PShareMem; &nbsp; <br>&nbsp; MapHandle: THandle; &nbsp; <br>function CallWndProc(nCode: Integer; WParam: WPARAM; LParam: LPARAM): LRESULT; stdcall; &nbsp; <br>var winStruct: TCWPStruct; &nbsp; <br>begin &nbsp;<br>&nbsp; winStruct := PCWPSTRUCT(LParam)^; &nbsp; <br>&nbsp; if nCode &gt;= 0 then &nbsp;<br>&nbsp; begin &nbsp;<br>&nbsp; &nbsp; if winStruct.message = WM_SHOWWINDOW then &nbsp;<br>&nbsp; &nbsp; begin &nbsp;<br>&nbsp; &nbsp; &nbsp; SaveInfo('发现窗口创建:::' + inttostr(winStruct.hwnd)); &nbsp; <br>&nbsp; &nbsp; end; &nbsp; <br>&nbsp; end; &nbsp; <br>&nbsp; Result := CallNextHookEx(0, nCode, WParam, LParam); &nbsp; <br>end; &nbsp; <br>procedure StartHook; stdcall; &nbsp; <br>begin &nbsp;<br>&nbsp; if PShare^.HookHandle = 0 then &nbsp;<br>&nbsp; begin &nbsp;<br>&nbsp; &nbsp; PShare^.HookHandle := SetWindowsHookEx(WH_CALLWNDPROC, PShare^.HookProc, hinstance, 0); &nbsp; <br>&nbsp; &nbsp; SaveInfo('钩子启动完毕,钩子句柄:::' + inttostr(PShare^.HookHandle)); &nbsp; <br>&nbsp; end; &nbsp; <br>end; &nbsp; <br>procedure StopHook; stdcall; &nbsp; <br>var unhookResult: Boolean; &nbsp; <br>begin &nbsp;<br>&nbsp; if PShare^.HookHandle &lt;&gt; 0 then &nbsp;<br>&nbsp; begin &nbsp;<br>&nbsp; &nbsp; unhookResult := UnhookWindowsHookEx(PShare^.HookHandle); &nbsp; <br>&nbsp; &nbsp; if unhookResult then &nbsp;<br>&nbsp; &nbsp; begin &nbsp;<br>&nbsp; &nbsp; &nbsp; PShare^.HookHandle := 0; &nbsp; <br>&nbsp; &nbsp; &nbsp; SaveInfo('钩子卸载完毕'); &nbsp; <br>&nbsp; &nbsp; end &nbsp;<br>&nbsp; &nbsp; else &nbsp;<br>&nbsp; &nbsp; &nbsp; SaveInfo('钩子卸载失败'); &nbsp; <br>&nbsp; end &nbsp;<br>&nbsp; else &nbsp;<br>&nbsp; &nbsp; SaveInfo('PShare^.HookHandle为空:::' + inttostr(PShare^.HookHandle)); &nbsp; <br>end; &nbsp; <br>procedure DllEntry(dwReason: DWORD); &nbsp; <br>begin &nbsp;<br>&nbsp; case dwReason of &nbsp;<br>&nbsp; &nbsp; DLL_PROCESS_ATTACH: &nbsp; <br>&nbsp; &nbsp; &nbsp; begin &nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; saveinfo('run in DLL_PROCESS_ATTACH'); &nbsp; <br>&nbsp; &nbsp; &nbsp; end; &nbsp; <br>&nbsp; &nbsp; DLL_PROCESS_DETACH: &nbsp; <br>&nbsp; &nbsp; &nbsp; begin &nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; saveinfo('dll 卸载,MapHandle:::' + inttostr(MapHandle)); &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; UnmapViewOfFile(PShare); &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; closehandle(MapHandle); &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; SaveInfo('共享内存关闭'); &nbsp; <br>&nbsp; &nbsp; &nbsp; end; &nbsp; <br>&nbsp; end; &nbsp; <br>end; &nbsp; <br>exports StartHook, StopHook; &nbsp; <br>begin &nbsp;<br>&nbsp; DisableThreadLibraryCalls(HInstance); &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; MapHandle := OpenFileMapping(FILE_MAP_ALL_ACCESS, False, pchar(HookMemFileName)); &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; if MapHandle = 0 then &nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; begin &nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MapHandle := CreateFileMapping($FFFFFFFF, nil, PAGE_READWRITE, 0, SizeOf(TShareMem), pchar(HookMemFileName)); &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; end; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; PShare := MapViewOfFile(MapHandle, FILE_MAP_ALL_ACCESS, 0, 0, 0); &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; PShare^.HookProc := @CallWndProc; &nbsp; <br>&nbsp; DllProc := @DllEntry; &nbsp; <br>end. &nbsp;<br><br>theMain.pas:<br><br>unit theMain; &nbsp; <br>interface &nbsp;<br>uses &nbsp;<br>&nbsp; Windows,SysUtils; &nbsp; <br>type &nbsp;<br>&nbsp; THookProc = function(nCode: integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall; &nbsp; <br>&nbsp; PShareMem = ^TShareMem; &nbsp; <br>&nbsp; TShareMem = packed record &nbsp;<br>&nbsp; &nbsp; Data: String[255]; &nbsp; <br>&nbsp; &nbsp; HookHandle:HHook; &nbsp; <br>&nbsp; &nbsp; ModuleHandle:THandle; &nbsp; <br>&nbsp; &nbsp; HookProc: THookProc; &nbsp; <br>&nbsp; end; &nbsp; <br>procedure SaveInfo(str: string); stdcall; &nbsp; <br>implementation &nbsp;<br>procedure SaveInfo(str: string); stdcall; &nbsp; <br>var &nbsp;<br>&nbsp; f: textfile; &nbsp; <br>begin &nbsp;<br>&nbsp; assignfile(f,'d:/Records.txt'); &nbsp; <br>&nbsp; if FileExists('d:/Records.txt') = false then rewrite(f) &nbsp; <br>&nbsp; else append(f); &nbsp; <br>&nbsp; writeln(f, str); &nbsp; <br>&nbsp; closefile(f); &nbsp; <br>end; &nbsp; <br>initialization &nbsp;<br>finalization &nbsp;<br>end. &nbsp;<br>unit theMain;<br>interface<br>uses<br>&nbsp; Windows,SysUtils;<br>type<br>&nbsp; THookProc = function(nCode: integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;<br>&nbsp; PShareMem = ^TShareMem;<br>&nbsp; TShareMem = packed record<br>&nbsp; &nbsp; Data: String[255];<br>&nbsp; &nbsp; HookHandle:HHook;<br>&nbsp; &nbsp; ModuleHandle:THandle;<br>&nbsp; &nbsp; HookProc: THookProc;<br>&nbsp; end;<br>procedure SaveInfo(str: string); stdcall;<br>implementation<br>procedure SaveInfo(str: string); stdcall;<br>var<br>&nbsp; f: textfile;<br>begin<br>&nbsp; assignfile(f,'d:/Records.txt');<br>&nbsp; if FileExists('d:/Records.txt') = false then rewrite(f)<br>&nbsp; else append(f);<br>&nbsp; writeln(f, str);<br>&nbsp; closefile(f);<br>end;<br>initialization<br>finalization<br>end.<br><br><br>主程序:<br><br>unit MainForm; &nbsp; <br>interface &nbsp;<br>uses &nbsp;<br>&nbsp; Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, &nbsp; <br>&nbsp; Dialogs, StdCtrls; &nbsp; <br>type &nbsp;<br>&nbsp; TForm1 = class(TForm) &nbsp; <br>&nbsp; &nbsp; Button1: TButton; &nbsp; <br>&nbsp; &nbsp; Button2: TButton; &nbsp; <br>&nbsp; &nbsp; procedure Button1Click(Sender: TObject); &nbsp; <br>&nbsp; &nbsp; procedure FormClose(Sender: TObject; var Action: TCloseAction); &nbsp; <br>&nbsp; private &nbsp;<br>&nbsp; &nbsp; { Private declarations } &nbsp;<br>&nbsp; &nbsp; procedure SaveInfo(str: string); &nbsp; <br>&nbsp; public &nbsp;<br>&nbsp; &nbsp; { Public declarations } &nbsp;<br>&nbsp; end; &nbsp; <br>//procedure StartHook; stdcall; external 'makeErr.dll' name 'StartHook'; &nbsp; <br>//procedure StopHook; stdcall; external 'makeErr.dll' name 'StopHook'; &nbsp; <br>var &nbsp;<br>&nbsp; Form1: TForm1; &nbsp; <br>&nbsp; StartHook:procedure; stdcall; &nbsp; <br>&nbsp; StopHook:procedure; stdcall; &nbsp; <br>&nbsp; moduleHandle : THandle; &nbsp; <br>implementation &nbsp;<br>{$R *.dfm} &nbsp;<br>procedure TForm1.Button1Click(Sender: TObject); &nbsp; <br>begin &nbsp;<br>&nbsp; moduleHandle := loadlibrary(pchar('makeErr.dll')); &nbsp; <br>&nbsp; StartHook := GetProcAddress(moduleHandle, 'StartHook'); &nbsp; <br>&nbsp; StopHook := GetProcAddress(moduleHandle, 'StopHook'); &nbsp; <br>&nbsp; if @StartHook&lt;&gt;nil then StartHook; &nbsp; <br>end; &nbsp; <br>procedure TForm1.SaveInfo(str: string); &nbsp; <br>var &nbsp;<br>&nbsp; f: textfile; &nbsp; <br>begin &nbsp;<br>&nbsp; assignfile(f,'d:/Records.txt'); &nbsp; <br>&nbsp; if FileExists('d:/Records.txt') = false then rewrite(f) &nbsp; <br>&nbsp; else append(f); &nbsp; <br>&nbsp; writeln(f, str); &nbsp; <br>&nbsp; closefile(f); &nbsp; <br>end; &nbsp; <br>procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction); &nbsp; <br>begin &nbsp;<br>&nbsp; SaveInfo('开始卸载钩子'); &nbsp; <br>&nbsp; if @StopHook&lt;&gt;nil then &nbsp;<br>&nbsp; &nbsp; StopHook &nbsp; <br>&nbsp; else &nbsp;<br>&nbsp; &nbsp; SaveInfo('未找到卸载函数'); &nbsp; <br>&nbsp; &nbsp; &nbsp; <br>&nbsp; freelibrary(moduleHandle); &nbsp; <br>// &nbsp;closehandle(moduleHandle); &nbsp; <br>&nbsp; SaveInfo('关闭主程序'); &nbsp; <br>end; &nbsp; <br>end.
 
顶部