我的hook死机,不知道怎么回事。(100分)

  • 主题发起人 主题发起人 Jeefxie
  • 开始时间 开始时间
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>&nbsp; WinTypes,<br>&nbsp; WinProcs,<br>&nbsp; Messages,<br>&nbsp; dialogs,<br>&nbsp; SYSUTILS;<br><br>var<br>&nbsp; HookCount: integer;<br>&nbsp; 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>&nbsp; &nbsp; hn :integer;<br>begin<br>&nbsp; if Code &gt;= 0 then begin //这里以下一写上代码就会死机。<br>&nbsp; &nbsp; hn :=findwindow(0,'explorer');<br>&nbsp; &nbsp; if hn &lt;&gt;0 then begin<br>&nbsp; &nbsp; &nbsp; getmessage(myMsg,hn,0,0); <br>&nbsp; &nbsp; &nbsp; IF myMsg.message = WM_WINDOWDESTORY THEN <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SHOWMESSAGE('WINDOWDESTORY');<br>&nbsp; &nbsp; end; &nbsp;// 这里以上<br>&nbsp; &nbsp; MessageBeep(1);<br>&nbsp; &nbsp; Result := CallNextHookEx(HookHandle, Code, Msg, 0);<br>&nbsp; end else<br>&nbsp; &nbsp; Result := CallNextHookEx(HookHandle, Code, Msg, 0);<br>end;<br><br>function InstallHook(SystemHook: boolean; TaskHandle: THandle) : boolean; export;<br>&nbsp; { This is really silly, but that's the way it goes. &nbsp;The only way to get the &nbsp;}<br>&nbsp; { module handle, *not* instance, is from the filename. &nbsp;The Microsoft example }<br>&nbsp; { just hard-codes the DLL filename. &nbsp;I think this is a little bit better. &nbsp; &nbsp; }<br>&nbsp; function GetModuleHandleFromInstance: THandle;<br>&nbsp; var<br>&nbsp; &nbsp; s: array[0..512] of char;<br>&nbsp; begin<br>&nbsp; &nbsp; { Find the DLL filename from the instance value. }<br>&nbsp; &nbsp; GetModuleFileName(hInstance, s, sizeof(s)-1);<br>&nbsp; &nbsp; { Find the handle from the filename. }<br>&nbsp; &nbsp; Result := GetModuleHandle(s);<br>&nbsp; end;<br>begin<br>&nbsp; { Technically, this procedure could do nothing but call SetWindowsHookEx(), &nbsp;}<br>&nbsp; { but it is probably better to be sure about things, and not set the hook &nbsp; &nbsp;}<br>&nbsp; { more than once. &nbsp;You definitely don't want your callback being called more }<br>&nbsp; { than once per message, do you? &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br>&nbsp; Result := TRUE;<br>&nbsp; if HookCount = 0 then begin<br>&nbsp; &nbsp; if SystemHook then<br>MouseHookCallBack, HInstance, 0)<br>&nbsp; &nbsp; &nbsp; HookHandle := SetWindowsHookEx(WH_SHELL, MouseHookCallBack, HInstance, 0)<br>&nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; { See the Microsoft KnowledgeBase, PSS ID Number: Q92659, for a discussion of }<br>&nbsp; &nbsp; &nbsp; { the Windows bug that requires GetModuleHandle() to be used. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br>&nbsp; &nbsp; &nbsp; HookHandle := SetWindowsHookEx(WH_SHELL, MouseHookCallBack,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;GetModuleHandleFromInstance, TaskHandle);<br>&nbsp; &nbsp; if HookHandle &lt;&gt; 0 then<br>&nbsp; &nbsp; &nbsp; inc(HookCount)<br>&nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; Result := FALSE;<br>&nbsp; end else<br>&nbsp; &nbsp; inc(HookCount);<br>end;<br><br>{ Call RemoveHook to remove the system hook. }<br>function RemoveHook: boolean; export;<br>begin<br>&nbsp; { See if our reference count is down to 0, and if so then unhook. }<br>&nbsp; Result := FALSE;<br>&nbsp; if HookCount &lt; 1 then exit;<br>&nbsp; Result := TRUE;<br>&nbsp; dec(HookCount);<br>&nbsp; if HookCount = 0 then<br>&nbsp; &nbsp; Result := UnhookWindowsHookEx(HookHandle);<br>end;<br><br>{ Have we hooked into the system? }<br>function IsHookSet: boolean; export;<br>begin<br>&nbsp; Result := (HookCount &gt; 0) and (HookHandle &lt;&gt; 0);<br>end;<br><br>exports<br>&nbsp; InstallHook,<br>&nbsp; RemoveHook,<br>&nbsp; IsHookSet,<br>&nbsp; MouseHookCallBack;<br><br>{ Initialize DLL data. }<br>begin<br>&nbsp; HookCount := 0;<br>&nbsp; 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. &nbsp;Use InstallSystemHook or InstallTaskHook. }<br>function InstallHook(SystemHook: boolean; TaskHandle: THandle): boolean;<br><br>implementation<br><br>uses WinProcs;<br><br>const<br>&nbsp; HOOK_DLL = 'HOOKDLL.DLL';<br><br>function InstallHook(SystemHook: boolean; TaskHandle: THandle): boolean; external HOOK_DLL;<br>function RemoveHook: boolean; &nbsp;external HOOK_DLL;<br>function IsHookSet: boolean; &nbsp; external HOOK_DLL;<br><br>function InstallSystemHook: boolean;<br>begin<br>&nbsp; InstallHook(TRUE, 0);<br>end;<br><br>function InstallTaskHook: boolean;<br>begin<br>&nbsp; InstallHook(FALSE,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {$IFDEF WIN32}<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; GetCurrentThreadID<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {$ELSE}<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; GetCurrentTask<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {$ENDIF}<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );<br>end;<br>
 
MouseHookCallBack中应该这样写:<br><br>if Code=HSHELL_WINDOWDESTROYED then ...<br><br>另外在MouseHookCallBack中最好不要SHOWMESSAGE弹出对话框。
 
我要是想返回一个值给我的的程序,用什么方式最好?
 
接受答案了.
 
后退
顶部