奇怪的HOOK问题???关于WH_CBT,会者简单!(200分)

  • 主题发起人 主题发起人 X-Killer
  • 开始时间 开始时间
X

X-Killer

Unregistered / Unconfirmed
GUEST, unregistred user!
我做了一个WH_CBT钩子,但却遇到一个奇怪的问题,安装其它钩子WH_MOUSE、WH_Shell都正常(正常安装、正常卸载,工作也正常),用iceSword可以查看,但WH_CBT出问题了,安装正常,安装后可以在IceSword中看到,创建窗口、释放窗口等消息也拦截得到,但奇怪的是运行WinAMP播放器,再关闭WinAmp后,WH_CBT被莫名其妙地卸载了,我试了N次了,我的程序中根本没有卸载,用同样的方法测试其它钩子却不会。真是见鬼了?!另外IceSword自身的CBT钩子也不会被WinAmp卸载,还有其它一些软件也不会,这说明是我的程序有问题,那问题到底在哪呢?我搞了半天弄不清楚,所在请哪位兄台教教我,如果有完整的、不会被winamp卸载的CBT的例子也可以发给我,谢谢大家了!<br> 另外,我的Winamp版本是 5.2.1
 
以下是我HOOK DLL单元的代码<br>unit untWinHook;<br>interface<br>uses<br> &nbsp;Windows, Messages;<br>type<br> &nbsp;PMyDLLVar = ^TMyDLLVar;<br> &nbsp;TMyDLLVar = record<br> &nbsp; &nbsp;hTrayHook, hShellHook, hCBTHook,<br> &nbsp; &nbsp;hKeyHook, hMouseHook : HHOOK; &nbsp;//HOOK句柄<br> &nbsp; &nbsp;Msg: TMessage;<br> &nbsp;end;<br><br>type<br> THookType=(htSHELL,htKEYBOARD,htMOUSE,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;htCBT,htGETMESSAGE,htCALLWNDPROC,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;htMSGFILTER,htSYSMSGFILTER);<br><br> &nbsp; &nbsp;//安装钩子<br> &nbsp; &nbsp;procedure InstallHook(SpyWindow : HWND; HookType:THookType); stdcall;<br> &nbsp; &nbsp;//卸载钩子<br> &nbsp; &nbsp;Procedure UnInstallHook(HookType:THookType); &nbsp;stdcall;<br>var<br> SHELL_EVENT : UINT;<br> CBT_EVENT &nbsp;: UINT;<br> KEY_EVENT &nbsp; : UINT;<br> MOUSE_EVENT : UINT;<br><br>implementation<br><br>function ShellHookProc(nCode: Integer; wParam: WPARAM;lParam:LPARAM): LRESULT; stdcall;<br>begin<br> Result := CallNextHookEx(DLLData^.hShellHook, nCode, wParam, lParam);<br>end;<br><br>function CBTHookProc(nCode: Integer; wParam : WPARAM; lParam : LPARAM): LRESULT; stdcall;<br>begin<br> Result := CallNextHookEx(DLLData^.hCBTHook, nCode, wParam, lParam);<br>end;<br><br>function KeyHookProc( nCode: Integer; &nbsp;wParam: WPARAM; lParam:LPARAM &nbsp; <br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ): LRESULT; stdcall;<br>begin<br> Result := CallNextHookEx(DLLData^.hKeyHook, nCode, wParam, lParam);<br>end;<br><br>function MouseHookProc( nCode:Integer; wParam:WPARAM;<br> &nbsp; &nbsp; &nbsp; &nbsp; lParam:LPARAM):LRESULT; stdcall;<br>begin<br> Result := CallNextHookEx(DLLData^.hMouseHook, nCode, wParam, lParam);<br>end;<br><br>//安装钩子<br>procedure InstallHook(SpyWindow : HWND; HookType : THookType); stdcall;<br>begin<br> With &nbsp;DLLData^ do<br> &nbsp;Case &nbsp;HookType of<br> &nbsp; htSHELL : begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (hShellHook = 0) then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hShellHook := SetWindowsHookEx(WH_SHELL, @ShellHookProc, HInstance , 0);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br> &nbsp; htCBT &nbsp;: begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (hCBTHook = 0) then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hCBTHook :=SetWindowsHookEx(WH_CBT, @CBTHookProc, HInstance , 0);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp;htKEYBOARD :<br> &nbsp; &nbsp; &nbsp; &nbsp; begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (hKeyHook=0) then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;hKeyHook:=SetWindowsHookEx(WH_KEYBOARD, @KeyHookProc, HInstance , 0);<br> &nbsp; &nbsp; &nbsp; &nbsp; end;<br> &nbsp;htMouse :<br> &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; if (hMouseHook=0) then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hMouseHook:=SetWindowsHookEx(WH_MOUSE, @MouseHookProc,hInstance,0);<br> &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; end;<br>end;<br><br>Procedure UnInstallHook(HookType:THookType); &nbsp;stdcall;<br>begin<br> with DLLData^ do<br> &nbsp;case HookType of<br> &nbsp; htSHELL: begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (hShellHook&lt;&gt;0) then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; UnhookWindowsHookEx(hShellHook);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hShellHook:=0;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br> &nbsp; htCBT &nbsp;: begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (hCBTHook&lt;&gt;0) then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; UnhookWindowsHookEx(hCBTHook);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hCBTHook:=0;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br> htKEYBOARD: begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (hKeyHook&lt;&gt;0) then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;UnhookWindowsHookEx(hKeyHook);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;hKeyHook:=0;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br> &nbsp;htMouse : begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (hMouseHook &lt;&gt;0) then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;UnHookWindowsHookEx(hMouseHook);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;hMouseHook:=0;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp;end;<br>end;<br>end.
 
楼主,搭车问下,以下代码为何无法编译呢<br>library Project2;<br><br>uses<br> &nbsp;SysUtils, windows,<br> &nbsp;Classes;<br>type<br> THookType=(htSHELL,htKEYBOARD,htMOUSE,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;htCBT,htGETMESSAGE,htCALLWNDPROC,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;htMSGFILTER,htSYSMSGFILTER);<br><br>procedure installhook(SpyWindow : HWND; HookType:THookType);<br>{$R *.res}<br>implementation<br>function KeyHookProc( nCode: Integer; &nbsp;wParam: WPARAM; lParam:LPARAM &nbsp; <br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ): LRESULT; stdcall;<br>begin<br> Result := CallNextHookEx(DLLData^.hKeyHook, nCode, wParam, lParam);<br>end;<br><br>begin<br>end.
 
library 不能加 implementation
 
没人吗?
 
先打个记号,有空再看了.
 
谢谢!再来些人帮忙呀!!!!!
 
谁写个WH_CBT钩子的例子给我呀?<br>各位大哥,怎么没人帮我呀?
 
说实话<br>俺不清楚CBT钩子是干吗用的啊
 
program Project1;<br><br>uses<br> &nbsp;Forms,<br> &nbsp;Unit1 in 'Unit1.pas' {Form1};<br><br>{$R *.RES}<br><br>begin<br> &nbsp;Application.Initialize;<br> &nbsp;Application.ShowMainForm := False;<br> &nbsp;Application.CreateForm(TForm1, Form1);<br> &nbsp;Application.Run;<br>end.
 
我是指托盘图标,一旦用Shell_NotifyIcon()建立图标,会立即显示在任务栏托盘区,但一些软件却可以建立隐藏的托盘图标。。。这应该不是简单的问题。
 
不建立托盘图标不就行了?你看到哪个程序是建立托盘图标而又隐藏的?既然是隐藏的,你怎么知道它已经建立?
 
建立图标的目的就是方便操作,如果不想让人看到就不画不就成了,画蛇添足?
 
支持楼上和楼上的楼上 &nbsp;两位
 
画蛇添足,说得真好。。。。问这一问题总是有它的理由的。<br>查MSDN找不到相关资料,看来又不会有人知道。。。。
 
那个东西是Explorer根据使用情况决定是否隐藏的。<br>在任务栏点击右键可以编辑,具体内容是在注册表中的。对于这种东西,我想是不会归档的。具体内容你可以自己研究!
 
比如任务管理器一打开便会建立13个托盘图标,其中12个是隐藏的<br>楼主,请问那12个图标是什么啊,我只看见一个呢?<br>你的问题不懂啊,关注。
 
多人接受答案了。
 
后退
顶部