|
用delphi编制windows95下的钩子函数 |
| windows消息管理机构提供了能使应用程序访问控制消息流μ 'c4所谓的钩子(hook)机制。钩子有多种,分别用于捕获某一特定类型或某一范围的消息。如:键盘消息,鼠标消息等。我们这里仅以键盘钩子的使用为例,讨论在delphi下怎样编写dll程序和怎样在自己的程序中安装使用键盘钩子函数,并讨论了不同程序使用同一dll文件时怎样共享数据。 一、 钩子过滤函数的编写说明 由于钩子过滤函数必须在独立的模块中,也就是说我们必须首先生成一个dll框架,然后再在其中加入钩子函数代码以及其他相关函数代码。我们这里以键盘钩子过滤函数的编写为例来说明。具体步骤如下: 1、先生成一个dll筐2架 2、编写自己的键盘钩子过滤函数 钩子过滤函数必须是回调函数,其函数的 4稳缦拢o function keyhookproc( icode:integer; wparam:wparam; lparam:lparam ) : lresult; stdcall ;export ; 在生成的dll框架中加入自己的键盘钩子处理函数处理键盘消息。 代码如下:… if(icode>=0) then begin result:=0; //初始化返回值 // 在这里加入自己的代码 end else begin result:=callnexthook(holdkeyhook,icode,wparam,lparam); // holdkeyhook是保存的原键盘过滤函数 5刂· end; 3、 安装键盘钩子过滤函数 为安装一个钩子筥fd滤函数应调用setwindowshookex函数(适用于windows3.0的setwindowshook钩子安装函数现在已经废弃不用)。该函数的原形如下: hhook setwindowshookex( int idhook, // 安装的筥b3子类型 hookproc lpfn, // 钩子过滤籂f数地址 hinstance hmod, // 任务句柄 dword dwthreadid // 钩子用于的目的 ); 需要说明的是:蚠a8常应该调用makeprocinstance函数以获取一个输出函数的前导码的入口地址,再将此地址作为setwindowshookex的第二个参数lpfn。但由于delphi提供了"灵巧调用(smart callback)",使得makeprocinstance可以省去,而直接将钩子过滤函数名用作入口地址。 这样当应用程序觃c3getmessage或peekmessage函数从消息队列中读消息或有按键消息(wm_keydown或wm_keyup)要处理时,系统就要调用钩子过滤函数keyhookproc处理键盘消息。 4、 卸载钩子过滤函数。 当钩子函数不再需要时,应调用unhookwindowshookproc卸载安装的钩子以释放系统资源。 完整的程序清单如下ba library keyhook; uses windows; const buffer_size=16*1024; const hook_mem_filename='sample key_hook_mem_file'; const hook_mutex_name ='sample key_hook_mutex_name'; type tshared=record keys : array[0..buffer_size] of char; keycount : integer; end; pshared=^tshared; var memfile,hookmutex : thandle; holdkeyhook : hhook; procsaveexit : pointer; shared : pshared; //键盘钩子过滤函数 function keyhookproc(icode: integer; wparam: wparam ; lparam: lparam):lresult ; stdcall; export; const keypressmask = $80000000; begin if icode < 0 then result := callnexthookex(holdkeyhook, icode, wparam, lparam) else begin if ((lparam and keypressmask)= 0) then // 键按下 begin shared^.keys[shared^.keycount]:=char(wparam and $00ff); inc(shared^.keycount); if shared^.keycount>=buffer_size-1 then shared^.keycount:=0; end; icode:=-1; result := callnexthookex(holdkeyhook, icode, wparam, lparam); end; end; // 设置钩子过滤函数 function enablekeyhook : bool ; export; begin shared^.keycount:=0; //初始化键盘指针 if holdkeyhook=0 then begin holdkeyhook := setwindowshookex(wh_keyboard, keyhookproc, hinstance, 0); end; result := (holdkeyhook <> 0); end; //撤消钩子过滤函数 function disablekeyhook: bool ; export; begin if holdkeyhook<> 0 then begin unhookwindowshookex(holdkeyhook); // 解除 keyboard hook holdkeyhook:= 0; shared^.keycount:=0; end; result := (holdkeyhook = 0); end; //取得键盘缓冲区中击键的个数 function getkeycount :integer ; export; begin result:=shared^.keycount; end; //取得键盘缓冲区的键 function getkey(index:integer) : char ; export; begin result:=shared^.keys[index]; end; //清空键盘缓冲区 procedure clearkeystring ; export; begin shared^.keycount:=0; end; //dll的退出处理过程 procedure keyhookexit; far; begin if holdkeyhook <> 0 then disablekeyhook; unmapviewoffile(shared); // 释放内存映象文件 closehandle(memfile); // 关闭映象文件 exitproc := procsaveexit; end; exports // 定义输出函数 enablekeyhook, disablekeyhook, getkeycount, clearkeystring, getkey; begin // dll 初始化部分 hookmutex:=createmutex(nil,true,hook_mutex_name); // 通过建立内存映象文件以共享内存 memfile:=openfilemapping(file_map_write,false, hook_mem_filename); if memfile=0 then memfile:=createfilemapping($ffffffff,nil,page_readwrite,0, sizeof(tshared) ,hook_mem_filename); shared:=mapviewoffile(memfile,file_map_write,0,0,0); releasemutex(hookmutex); closehandle(hookmutex); procsaveexit := exitproc; // 保存dll的exitproc exitproc := @keyhookexit; // 设置dll新的exitproc end. // 源代码结束 二、 在自己的程序中使用编制好的键盘钩子过滤函数。 钩子函数编制好后,使用起来其实很简单:首先调用setwindowshookex安装自己的钩子过滤函数,同时保存原先的钩子过滤函数地址。这时钩子函数就开始起作用了,它将按照你的要求处理键盘消息。程序运行完毕或不再需要监视键盘消息时,调用unhookwindowshookproc函数卸载所安装的钩子函数,同时恢复原来的钩子过滤函数地址。 下面就是使用在以上编制的钩子函数的例子: unit unit1; interface uses windows, messages, sysutils, classes, graphics, controls, forms, dialogs, stdctrls, extctrls; type tform1 = class(tform) memo1: tmemo; panel1: tpanel; bsethook: tbutton; bcancelhook: tbutton; breadkeys: tbutton; bclearkeys: tbutton; panel2: tpanel; procedure bsethookclick(sender: tobject); procedure bcancelhookclick(sender: tobject); procedure breadkeysclick(sender: tobject); procedure bclearkeysclick(sender: tobject); end; var form1: tform1; implementation {$r *.dfm} function enablekeyhook : bool ; external 'keyhook.dll'; function disablekeyhook : bool ; external 'keyhook.dll'; function getkeycount : integer ; external 'keyhook.dll'; function getkey(idx:integer) : char ; external 'keyhook.dll'; procedure clearkeystring ; external 'keyhook.dll'; procedure tform1.bsethookclick(sender: tobject); // 设置键盘钩 7ó begin enablekeyhook; bsethook.enabled :=false; bcancelhook.enabled:=true; breadkeys.enabled :=true; bclearkeys.enabled :=true; panel2.caption:=' 键盘钩子已经设置'; end; procedure tform1.bcancelhookclick(sender: tobject); // 卸载键盘钩子 begin disablekeyhook; bsethook.enabled :=true; bcancelhook.enabled:=false; breadkeys.enabled :=false; bclearkeys.enabled :=false; panel2.caption:=' 键盘钩子没有设置'; end; procedure tform1.breadkeysclick(sender: tobject); // 取得击键的历史记录 var i:integer; begin memo1.lines.clear; // 在memo1中显示击键历史记录 for i:=0 to getkeycount-1 do memo1.text:=memo1.text+getkey(i); end; procedure tform1.bclearkeysclick(sender: tobject); // 清除击键历史记录 begin memo1.clear; clearkeystring; end; end. // 源代码结束 三、 windows95下dll中实现共享内存 在上面的钩子函数所在的dll文件中,需要使用共享内存,即,所有击键的记录存储在同一个数据段中。为什么要这样做呢?这是因为windows95的dll调用方法与windows3.x的方法不同。每个进(线)程在登录某动态连接库时都会为该动态连接库传入一个新的实例句柄(即dll数据段的句柄)。这使得dll各个实例之间互不干扰,但是这对那些所有dll实例共享一组变量带来一些困难。为了解决这个问题,我们在这儿通过建立内存映射文件的方法来解决。即使用windows的openfilemapping、createfilemapping和 mapviewoffile三个函数来实现。使用方法如下: … memfile是thandle类型,shared是指针类型,hook_mem_filename是一常量串 … memfile:=openfilemapping(file_map_write,false, hook_mem_filename); //打开内存映射文件 if memfile=0 then //打开失败则衉c2建内存映射文件 memfile:=createfilemapping($ffffffff,nil,page_readwrite,0, sizeof(tshared) ,hook_mem_filename); //映射文件到变量 shared:=mapviewoffile(memfile,file_map_write,0,0,0); 到此为止,你已经知道用delphi编制钩子函数有多么容易。最后不得不提醒大家:钩子函数虽然功能比较强,但如果使用不当将会严重影响系统的效率,所以要尽量避免使用系统钩子。非要使用不可时也应该格外小心,应使之尽可能小地影响系统的运行。 [全文完] |
| 文章作者: 王浩然 | |
| 文章来源: 暂无 | |
| 作者邮箱: 暂无 | |
| 作者网站: 暂无 |