J
Jeefxie
Unregistered / Unconfirmed
GUEST, unregistred user!
这是我用一个MOUSE HOOK 改的,改回MOUSE HOOK 就会很正常。但SHELL就不<br>行.<br>能够捕获HSELL,但在下面加入代码后程序就会死机。<br>我是想捕获windowcreate和windowdestroy的消息,并通知我程序。<br>hookdll.dpr 如下:<br>library HookDLL;<br><br>uses<br> WinTypes,<br> WinProcs,<br> Messages,<br> dialogs,<br> SYSUTILS;<br><br>var<br> HookCount: integer;<br> HookHandle: HHook;<br><br>{ This is where you do your special processing. }<br>{$IFDEF WIN32}<br>function MouseHookCallBack(Code: integer; Msg: WPARAM; MouseHook: LPARAM): LRESULT; stdcall;<br>{$ELSE}<br><br><br>function MouseHookCallBack(Code: integer; Msg: word; MouseHook: longint): longint; export;<br>{$ENDIF}<br>var myMsg :tmsg;<br> hn :integer;<br>begin<br> if Code >= 0 then begin //这里以下一写上代码就会死机。<br> hn :=findwindow(0,'explorer');<br> if hn <>0 then begin<br> getmessage(myMsg,hn,0,0); <br> IF myMsg.message = WM_WINDOWDESTORY THEN <br> SHOWMESSAGE('WINDOWDESTORY');<br> end; // 这里以上<br> MessageBeep(1);<br> Result := CallNextHookEx(HookHandle, Code, Msg, 0);<br> end else<br> Result := CallNextHookEx(HookHandle, Code, Msg, 0);<br>end;<br><br>function InstallHook(SystemHook: boolean; TaskHandle: THandle) : boolean; export;<br> { This is really silly, but that's the way it goes. The only way to get the }<br> { module handle, *not* instance, is from the filename. The Microsoft example }<br> { just hard-codes the DLL filename. I think this is a little bit better. }<br> function GetModuleHandleFromInstance: THandle;<br> var<br> s: array[0..512] of char;<br> begin<br> { Find the DLL filename from the instance value. }<br> GetModuleFileName(hInstance, s, sizeof(s)-1);<br> { Find the handle from the filename. }<br> Result := GetModuleHandle(s);<br> end;<br>begin<br> { Technically, this procedure could do nothing but call SetWindowsHookEx(), }<br> { but it is probably better to be sure about things, and not set the hook }<br> { more than once. You definitely don't want your callback being called more }<br> { than once per message, do you? }<br> Result := TRUE;<br> if HookCount = 0 then begin<br> if SystemHook then<br>MouseHookCallBack, HInstance, 0)<br> HookHandle := SetWindowsHookEx(WH_SHELL, MouseHookCallBack, HInstance, 0)<br> else<br> { See the Microsoft KnowledgeBase, PSS ID Number: Q92659, for a discussion of }<br> { the Windows bug that requires GetModuleHandle() to be used. }<br> HookHandle := SetWindowsHookEx(WH_SHELL, MouseHookCallBack,<br> GetModuleHandleFromInstance, TaskHandle);<br> if HookHandle <> 0 then<br> inc(HookCount)<br> else<br> Result := FALSE;<br> end else<br> inc(HookCount);<br>end;<br><br>{ Call RemoveHook to remove the system hook. }<br>function RemoveHook: boolean; export;<br>begin<br> { See if our reference count is down to 0, and if so then unhook. }<br> Result := FALSE;<br> if HookCount < 1 then exit;<br> Result := TRUE;<br> dec(HookCount);<br> if HookCount = 0 then<br> Result := UnhookWindowsHookEx(HookHandle);<br>end;<br><br>{ Have we hooked into the system? }<br>function IsHookSet: boolean; export;<br>begin<br> Result := (HookCount > 0) and (HookHandle <> 0);<br>end;<br><br>exports<br> InstallHook,<br> RemoveHook,<br> IsHookSet,<br> MouseHookCallBack;<br><br>{ Initialize DLL data. }<br>begin<br> HookCount := 0;<br> HookHandle := 0;<br>end.<br><br>******************************<br>hookunit 如下:<br><br>unit Hookunit;<br><br>interface<br><br>uses WinTypes;<br><br>function InstallSystemHook: boolean;<br>function InstallTaskHook: boolean;<br>function RemoveHook: boolean;<br>function IsHookSet: boolean;<br>{ Do not use InstallHook directly. Use InstallSystemHook or InstallTaskHook. }<br>function InstallHook(SystemHook: boolean; TaskHandle: THandle): boolean;<br><br>implementation<br><br>uses WinProcs;<br><br>const<br> HOOK_DLL = 'HOOKDLL.DLL';<br><br>function InstallHook(SystemHook: boolean; TaskHandle: THandle): boolean; external HOOK_DLL;<br>function RemoveHook: boolean; external HOOK_DLL;<br>function IsHookSet: boolean; external HOOK_DLL;<br><br>function InstallSystemHook: boolean;<br>begin<br> InstallHook(TRUE, 0);<br>end;<br><br>function InstallTaskHook: boolean;<br>begin<br> InstallHook(FALSE,<br> {$IFDEF WIN32}<br> GetCurrentThreadID<br> {$ELSE}<br> GetCurrentTask<br> {$ENDIF}<br> );<br>end;<br>