做全局键盘钩子时,WARPAM为何始终为0?(50分)

  • 主题发起人 主题发起人 dazzling
  • 开始时间 开始时间
D

dazzling

Unregistered / Unconfirmed
GUEST, unregistred user!
使用的DLL做全局键盘钩子处理程序,里面已经设置HOOK函数了。想将击键结果SHOWMESSAGE出来但是不行。<br>源程序:<br>键盘HOOK函数:<br>function KeybodHookProc(nCode: Integer;WParam: WPARAM;LParam: LPARAM): LRESULT;stdcall;<br>begin<br>&nbsp; showmessage(pchar(WPARAM));//此处即使为intToStr(WPARAM)亦不行<br>&nbsp; result:=0;<br>end;<br><br><br>设置HOOK:<br>SetWindowsHookEx(WH_KEYBOARD,@KeybodHookProc,Hinstance,0);<br><br>盼高手赐教!<br>
 
请看我的写的钩子代码ajgz.rar在http://www.efile.com.cn/?liumazi
 
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1.引言<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 有些软件或网站为了不让别人拷贝信息,都做了处理,譬如:屏蔽了Ctrl+C和鼠标右键复制功能。其实使用键盘钩子和鼠标钩子就可以实现对Ctrl+C和鼠标右键等的屏蔽。本文简单介绍了钩子的概念和运行机制,并详细说明了编程实现方法。<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2.Win32钩子及其运行机制<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Win32钩子实际上是一个处理特定消息的程序段,它可以监视系统或进程中的各种事件消息,截获发往目标窗口的消息并进行处理。当触发某个特定的消息时,该消息到达目的应用程序之前,钩子程序就先得到该消息的控制权并捕获该消息。这时钩子程序可以按特定的要求加工处理(改变)该消息,也可以不作任何处理而继续传递该消息,还可以强制结束消息的传递。对每种类型的钩子由系统来维护一个钩子链,最近安装的钩子放在链的开始,而最先安装的钩子放在最后,也就是后加入的钩子先获得控制权。其中,全局钩子函数必须含在DLL中,而线程专用钩子还可以包含在执行文件中。[1]<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 这样,我们就可以在系统中安装自定义的钩子,监视系统中特定事件的发生,完成特定的功能,比如截获键盘、鼠标的输入、屏幕取词和日志监视等等。Win32中钩子类型很多,按事件分主要有键盘钩子、鼠标钩子和日志钩子等,按使用范围有系统钩子和线程钩子。[2]<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 3.实现方法<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (1)安装钩子函数<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; HHOOK SetWindowsHookEx(<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int idHook, //钩子的类型<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; HOOKPROC lpfn, //安装的钩子函数的地址<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; HINSTANCE hMod, //此函数所在模块的句柄<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DWORD dwThreadId //所监控线程的ID<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; idHook的取值决定Hook要截获消息的类型(如系统消息、键盘消息、鼠标消息或所有消息)和截获方式(如到达目的程序之前还是目的程序处理之后),主要有 <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; :WH_CALLWNDPROC、WH_CALLWNDPROCRET、WH_GETMESSAGE、 <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WH_JOURNALRECORD、WH_KEYBOARD、WH_MOUSE、WH_SHELL。 <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; idHook的详细使用说明请查看联机帮助。由于本程序要实现键盘钩子功能,所以选用WH_KEYBOARD。<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Windows发出的消息如符合钩子函数的条件就调用设置的钩子函数(地址为lpfn),钩子函数由用户定义 ,其接口格式必须为 :<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LRESULT KeyboardHookHandler(iCode: Integer;wParam: WPARAM;lParam: <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LPARAM);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 其中参数wParam,lParam因钩子的类型不同有不同含义。参数iCode&lt;0必须设置返回值Result:=CallNextHookEx();<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SetWindowsHookEx函数多数情况下在DLL中,所以hMod通常为GetModuleHandle(DllfileName:LPCTSTR);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dwThreadId如为0,表示监视所有线程。<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (2)卸载钩子函数<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 安装上钩子函数会降低系统性能,用完后应调用卸载钩子函数UnHookWindowsHookProc以释放系统资源。<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (3)以下程序在Delphi 6.0中编译通过。步骤如下:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ①如果Hook在应用程序中实现,若应用程序不是当前窗口时,该Hook就不起作用;如果Hook在DLL中实现,程序在运行中动态调用它,它能实时对系统进行监控。所以采用在DLL中实现Hook。新建一个导出两个函数的DLL文件,在HookUnit.pas中定义了钩子具体实现过程。代码如下:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; library Hook;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; uses<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; HookUnit in 'HookUnit.pas';<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exports<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; EnableHotKeyHook,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DisableHotKeyHook;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hNextHookProc := 0; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; procSaveExit := ExitProc;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ExitProc := @HotKeyHookExit;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end.<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ②键盘钩子编程实现过程如下:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; unit HookUnit;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; interface<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; uses<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Windows, Messages;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hNextHookProc: HHook;//保存SetWindowsHookEx的返回值<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; procSaveExit: Pointer;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; function KeyboardHookHandler(iCode: Integer;wParam: WPARAM;lParam: <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LPARAM): LRESULT; stdcall; export;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; function EnableHotKeyHook: BOOL; export;//安装钩子<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; function DisableHotKeyHook: BOOL; export;//卸载钩子<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; procedure HotKeyHookExit; far;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; implementation<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; function KeyboardHookHandler(iCode: Integer;wParam: WPARAM;lParam: <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LPARAM): LRESULT; stdcall; export;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; const<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _KeyPressMask=$80000000;//键盘掩码常量<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Result:=0;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; If iCode &lt; 0 Then //根据SDK说明,若iCode小于0,调用CallNextHookEx并返回<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Result:=CallNextHookEx(hNextHookProc, iCode, wParam, lParam);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Exit;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 监测是否按下了Ctrl+X、Ctrl+V、Ctrl+C这三个组合键,若按下则退出,不响应事件<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ((lParam and _KeyPressMask) = 0) and(GetKeyState(vk_control) <br>&lt;0)<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; and ((wParam = Ord('X'))or(wParam = Ord('V'))or(wParam = <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Ord('C')))then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Result:=1;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Exit;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; function EnableHotKeyHook: BOOL; export;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Result := False;//初始化返回值<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if hNextHookProc&lt;&gt;0 then//如果已经注册,直接退出<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Exit; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //挂上键盘钩子,同时传回值必须保留下来,免得Hook呼叫链结断掉<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hNextHookProc:=SetWindowsHookEx(WH_KEYBOARD,KeyboardHookHandler,HInstance,0);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //注册hook<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Result:=hNextHookProc &lt;&gt; 0;//通过返回值确定是否注册成功<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; function DisableHotKeyHook: BOOL; export;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if hNextHookProc&lt;&gt;0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; UnhookWindowshookEx(hNextHookProc);//解除Keyboard Hook<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hNextHookProc:=0;//恢复标志<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Result:=hNextHookProc=0;//返回是否注销成功 <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; procedure HotKeyHookExit;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if hNextHookProc&lt;&gt;0 then DisableHotKeyHook;//如果忘了解除HOOK,自动代理解除动作<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ExitProc:=procSaveExit;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end.<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ③主程序<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; unit HkUnit;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; interface<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; uses<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Dialogs,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; StdCtrls;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Memo1: TMemo;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; procedure FormCreate(Sender: TObject);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; procedure FormClose(Sender: TObject; var Action: TCloseAction);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; private<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { Private declarations }<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { Public declarations }<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Form1: TForm1;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; implementation<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {$R *.DFM}<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; function EnableHotKeyHook: BOOL; external <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'HookDll.DLL';//dll中注册hook的函数,传递参数为回调函数的指针<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; function DisableHotKeyHook: BOOL; external <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'HookDll.DLL';//dll中注销hook的函数<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; procedure TForm1.Memo1ContextPopup(Sender: TObject; MousePos: <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TPoint;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var Handled: Boolean);//屏蔽Memo1鼠标右键弹出的系统菜单<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Handled:=true;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; procedure TForm1.FormCreate(Sender: TObject);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; EnableHotKeyHook;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; procedure TForm1.FormClose(Sender: TObject; var Action: <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TCloseAction);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DisableHotKeyHook;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end.<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 4.总结<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 本文介绍了如何拦截Memo的复制、剪切和粘贴功能。调用Memo事件OnContextPopup(或新建空的PopupMenu,然后指向Memo的PopupMenu)可以屏蔽鼠标右键;使用键盘钩子则可以拦截Ctrl+C、Ctrl+X和Ctrl+V,让键盘失效。最大的缺点是:运行该程序后,将无法再使用这三个组合键,如:Word、记事本等都无使用这三个组合键来进行复制、剪切和粘贴功能,也不能用这三个组合键对文件或文件夹进行操作
 
不可能全部0的,应该: showmessage(pchar(intToStr(WPARAM)));<br>还不行的话请把代码贴出来 我帮你看 &nbsp;。。 &nbsp; &nbsp;:)
 
多人接受答案了。
 
后退
顶部