求购一个CBT类型的HOOK例子(50分)

  • 主题发起人 主题发起人 avant
  • 开始时间 开始时间
A

avant

Unregistered / Unconfirmed
GUEST, unregistred user!
&nbsp; &nbsp;资料所见,多是钱达智keyboard hook的例子.我仿了一个CBT型的HOOK,<br>想监测所有的窗体Active事件(暂设为Beep一声),可是一运行...那位有经验<br>的大虾帮我调一调?或见赐一个相关的例子?分不够我可以再加!小弟的拙作附<br>下:<br>//DLL工程文件用到的单元<br>unit Hkwaproc;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages,SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br>&nbsp; ExtCtrls, StdCtrls;<br><br>var<br>&nbsp; hNextHookProc: HHook;<br>&nbsp; procSaveExit: Pointer;<br><br><br>function WinSkbdHandler(nCode: Integer;wParam: WPARAM;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lParam:LPARAM): LRESULT; stdcall; export;<br>function EnableWAHook: BOOL; export;<br>function DisableWAHook: BOOL; export;<br>procedure WAHookExit; far;<br><br>implementation<br><br>function WinSkbdHandler(nCode: Integer;wParam: WPARAM;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;lParam:LPARAM): LRESULT; stdcall; export;<br>begin<br>&nbsp; Result := 0;<br>&nbsp; if nCode &lt; 0 Then<br>&nbsp; begin<br>&nbsp; &nbsp; Result := CallNextHookEx(hNextHookProc, nCode, wParam, lParam);<br>&nbsp; &nbsp; Exit;<br>&nbsp; end;<br>&nbsp; if nCode = HCBT_ACTIVATE then<br>&nbsp; begin<br>&nbsp; &nbsp; Result:=1;<br>&nbsp; &nbsp; beep;<br>&nbsp; &nbsp; //我待加的操作<br>&nbsp; end;<br>end;<br><br><br>function EnableWAHook: BOOL; export;<br>begin<br>&nbsp; Result := False;<br>&nbsp; if hNextHookProc &lt;&gt; 0 then Exit;<br>&nbsp; hNextHookProc:=SetWindowsHookEx(WH_CBT,WinSkbdHandler,HInstance,0);<br>&nbsp; Result := hNextHookProc &lt;&gt; 0;<br>end;<br><br><br>function DisableWAHook: BOOL; export;<br>begin<br>&nbsp; if hNextHookProc &lt;&gt; 0 then<br>&nbsp; begin<br>&nbsp; &nbsp; UnhookWindowshookEx(hNextHookProc);<br>&nbsp; &nbsp; hNextHookProc := 0;<br>&nbsp; &nbsp; MessageBeep(0);<br>&nbsp; &nbsp; MessageBeep(0);<br>&nbsp; end;<br>&nbsp; Result := hNextHookProc = 0;<br>end;<br><br><br>procedure WAHookExit;<br>begin<br>&nbsp; if hNextHookProc &lt;&gt; 0 then DisableWAHook;<br>&nbsp; ExitProc := procSaveExit;<br>end;<br><br>end.<br><br>//---------DLL工程文件<br>library HkWinActivate;<br><br>uses<br>&nbsp; Hkwaproc in 'Hkwaproc.pas';<br><br>exports<br>&nbsp; EnableWAHook,<br>&nbsp; DisableWAHook;<br><br>begin<br>&nbsp; hNextHookProc := 0;<br>&nbsp; procSaveExit := ExitProc;<br>&nbsp; ExitProc := @WAHookExit;<br>end.<br><br>//----------------测试用例<br>unit wademo;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br>&nbsp; StdCtrls;<br><br>type<br>&nbsp; TForm2 = class(TForm)<br>&nbsp; &nbsp; Button1: TButton;<br>&nbsp; &nbsp; Button2: TButton;<br>&nbsp; &nbsp; procedure Button1Click(Sender: TObject);<br>&nbsp; &nbsp; procedure Button2Click(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br><br>var<br>&nbsp; Form2: TForm2;<br><br>implementation<br><br>{$R *.DFM}<br>function EnableWAHook: BOOL; external 'HKWinACTIVATE.DLL';<br>function DisableWAHook: BOOL; external 'HKWinACTIVATE.DLL';<br><br>procedure TForm2.Button1Click(Sender: TObject);<br>begin<br>&nbsp; if EnableWAHook then<br>&nbsp; &nbsp; ShowMessage('WAHook 启动;');<br>end;<br><br>procedure TForm2.Button2Click(Sender: TObject);<br>begin<br>&nbsp; if DisableWAHook then<br>&nbsp; &nbsp; ShowMessage('WA Hook已解除!');<br>end;<br><br>end.<br>
 
Only listen?<br>Who can answer?Nobody?
 
我总觉得老钱的例子有问题,可是这么多人在传抄。<br>把那个HHOOK放在FileMap里试试。<br>搜索本论坛“CreateFileMapping”。<br>
 
:O*O<br>&nbsp; &nbsp; FileMap?详细点好吗?分就准备给您了,不成敬意.<br>&nbsp; &nbsp; 老钱的例子有问题,但那里有比较完善的例子?比如Inprise官方出的?哪一类都<br>行!
 
在下没有什么官方的例子。<br>之所以说有问题是根据我制作的一个SHELL钩子的经验。(SHELL与CBT很象呀)<br>如果按老钱那样声明HHOOK,就总出错。改成FILEMAP就好了。(仅针对SHELL)<br>但我不能保证你的问题出错是因为这个。<br><br>手头上没有代码,看这个吧。<br>http://202.120.85.61/delphibbs/DispQ.asp?LID=145601
 
后果是不是程序死掉了..<br>
 
o*o:<br>&nbsp; &nbsp; &nbsp;那个讨论我也看了,关系不大,没有CBT类型.各类型还是有一定的差别的.<br>&nbsp; &nbsp; &nbsp;其实我就是想监测所有的窗体Active事件,该用什么类型的?我是乱用的. <br>sunstone:<br>&nbsp; &nbsp; &nbsp;你怎么知...XiXi,有时候也就是个半死,至少可以用Alt+Ctrl+Del结束任务.有<br>时候呢,就嘟嘟叫个不停了.--随机?'&amp;'
 
ShellHook的HSHELL_WINDOWACTIVATED不就是干这个的吗?
 
接受答案了.
 

Similar threads

后退
顶部