在许多系统中、出于安定或其它原因、经常要求随时对键盘进行监控、壹个专业的监控步骤必需具备两点、壹是实时;贰是作为领导图标运行实际应用中把应用Hook技巧编写的应用步骤添加到Windows的义务栏的领导区中就能够很好的达到这个目的我在参考了API赞助文档根本上、根在Delphi开发环境中的具体表现分辨对这两局部进行详细阐述
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: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:lresult;stdcall;export;
begin
if icodelt;0 then
begin
result:=callnexthookex;
exit;
end;
assignfile;
append;
if getkeystatelt;0 then
begin
writeln;
write);
end
else
write);
closefile;
result:=0;
end;
function endkeyhook:bool;export;
begin
if nexthookproclt;0 then begin
unhookwindowshookex;
nexthookproc:=0;
messagebeep; end;
result:=hnexthookproc=0;
end;
procedure keyhookexit;far;
begin
if nexthookproclt;0 then endkeyhook;
exitproc:=procsaveexit; end;
end.
通过该函函数将应用步骤的图标添加到领导区中、使其作为图标运行、增加专业特色卖步骤起动后、用鼠标右键点击图标、则弹出壹个菜单、可选择sethook或endhook
unit kb;
interface
uses
Windows、 Messages、 SysUtils、 Classes、
Graphics、 Controls、 Forms、
Dialogs、
StdCtrls、 Menus、shellapi;
const
iconid=1;
MIiconevent=wmuser+1;//界说壹个用户
type
TForm1 = class
PopupMenu1: TPopupMenu;
sethook1: TMenuItem;
endhook1: TMenuItem;
N1: TMenuItem;
About1: TMenuItem;
Close1: TMenuItem;
Gettext1: TMenuItem;
procedure FormCreate;
procedure sethook1Click;
procedure endhook1Click;
procedure FormDestroy;
procedure Close1Click;
private
Private declarations
nid:tnotifyicondata;
normalicon:ticon;
public
Public declarations
procedure icontray;
message miiconevent;
end;
var
Form1: TForm1;
implementation
R .DFM
function setkeyhook:bool;external keyspy.dll;
function endkeyhook:bool;external keyspy.dll;
procedure tform1.icontray;
var
pt:tpoint;
begin
if msg.lparam=wmlbuttondown then
sethook1click;
if msg.LParam=wmrbuttondown then
begin
getcursorpos;
setforegroundwindow;
popupmenu1.popup;
end;
end;
procedure TForm1.FormCreate;
begin
normalicon:=ticon.create;
application.title:=caption;
nid.cbsize:=sizeof;
nid.wnd:=handle;
nid.uid:=iconid;
nid.uflags:=nificon or nifmessage or niftip;
nid.ucallbackmessage:=miiconevent;
nid.hIcon :=normalicon.handle;
strcopy);
nid.uFlags:=nifmessage or nificon or niftip;
shellnotifyicon;
SetWindowLong;
end;
procedure TForm1.sethook1Click;
begin
setkeyhook;
end;
procedure TForm1.endhook1Click;
begin
endkeyhook;
end;
procedure TForm1.FormDestroy;
begin
nid.uFlags :=0;
shellnotifyicon;
end;
procedure TForm1.Close1Click;
begin
application.terminate;
end;
该步骤虽然只用了几个shellai函数、但是它涉及到了在Delphi中对DLL的引用!钩子表现!对领导区的操纵!用户界说的处置!文件的读写等比拟重要的内容、我信任这篇文章能对许多Delphi的初学者有所赞助


