在调用dll的应用程序中却能够监控:
//---------------------------dll 单元---------------------
unit hookproc;interface
uses windows;
const
_KeyPressMask = $80000000 ;
var
hNextHookProc: HHook;
pwd:string;
function keyboardhook(icode:integer;wparam:wparam;
lparam:lparam):lresult;stdcall;export;
function setkeyhook:bool;export;//加载钩子
function endkeyhook(var pwd1:string):bool;stdcall;export;//卸载钩子
implementation
function keyboardhook(icode:integer;wparam:wparam;
lparam:lparam):lresult;stdcall;export;
var
i,j:integer;
c:char;
begin
if icode<0 then
begin
result:=callnexthookex(hnexthookproc,icode,wparam,lparam);
exit;
end;
if((lParam and _KeyPressMask)=0) then
begin
j:=getkeystate($14); //返回Caps Lock键的状态
i:=getkeystate($10); //返回Shift键的状态
if (j and 1)=1 then//判断CapsLock是否按下
begin
if ((i and _KeyPressMask)=_KeyPressMask) then
begin////判断Shift 是否按下
if (wparam<65) then //判断是字母键还是数字键
begin
c:=chr(wparam-16);
end
else begin
if (wparam>95) and (wparam<106) then
c:=chr(wparam-36) else
c:= chr(wparam+32);
end;// if (wparam<65) then //判断是字母键还是数字键
end//if ((i and _KeyPressMask)=_KeyPressMask) then
else begin
if (wparam<65) then
begin
c:=chr(wparam);
end
else begin
c:=chr(wparam);
end;//if (wparam<65) then
end;//not if ((i and _KeyPressMask)=_KeyPressMask) then
end//if((j and 1)=1 )then //判断CapsLock是否按下
else begin
if ((i and _KeyPressMask)=_KeyPressMask) then
begin
if (wparam<65) then
begin
c:=chr(wparam-16);
end
else begin
c:=chr(wparam);
end;//if (wparam<65) then
end//if ((i and _KeyPressMask)=_KeyPressMask) then
else begin
if (wparam<65) then
begin
c:=chr(wparam);
end
else begin
c:=chr(wparam+32);
end;//if (wparam<65) then
end;//not if ((i and _KeyPressMask)=_KeyPressMask) then
end;//not if((j and 1)=1 )then //判断CapsLock是否按下
end;//if((lParam and _KeyPressMask)=0) then
pwd:=pwd+c;
result:=0;
end;
function setKeyHook: BOOL; export;
begin
Result := False;
pwd:='';
if hNextHookProc <> 0 then Exit;
hNextHookProc := SetWindowsHookEx(WH_KEYBOARD,
KeyboardHook,
HInstance,
0);
Result := hNextHookProc <> 0;
end;
function endkeyhook(var pwd1:string):bool;stdcall;export;
begin
pwd1:=pwd;
if hnexthookproc<>0 then
begin
unhookwindowshookex(hnexthookproc);
hnexthookproc:=0;
end;
result:=hnexthookproc=0;
end;
end.//--------------------------DLL prj-------------------------------
library HK;uses
SysUtils,
Classes,
hookproc in 'hookproc.pas';
exports
setkeyhook,
endkeyhook;
{$R *.res}begin
end.//----------------------------.exe-----------------------------
unit main;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Label1: TLabel;
Label2: TLabel;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
{ Private declarations } public
{ Public declarations }
end;var
Form1: TForm1;
function setkeyhook: BOOL;
function endkeyhook(var pwd1:string):bool;stdcall;
implementation
function setkeyhook: BOOL;external'hk.dll';
function endkeyhook(var pwd1:string):bool;stdcall;external'hk.dll';
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
if setkeyHook then label2.Caption:='set keyHook success';
end;procedure TForm1.Button2Click(Sender: TObject);
var
pwd:string;
begin
if endkeyHook(pwd) then label2.Caption:='close keyHook success';
label1.Caption:=pwd;
end;
end.
//---------------------------dll 单元---------------------
unit hookproc;interface
uses windows;
const
_KeyPressMask = $80000000 ;
var
hNextHookProc: HHook;
pwd:string;
function keyboardhook(icode:integer;wparam:wparam;
lparam:lparam):lresult;stdcall;export;
function setkeyhook:bool;export;//加载钩子
function endkeyhook(var pwd1:string):bool;stdcall;export;//卸载钩子
implementation
function keyboardhook(icode:integer;wparam:wparam;
lparam:lparam):lresult;stdcall;export;
var
i,j:integer;
c:char;
begin
if icode<0 then
begin
result:=callnexthookex(hnexthookproc,icode,wparam,lparam);
exit;
end;
if((lParam and _KeyPressMask)=0) then
begin
j:=getkeystate($14); //返回Caps Lock键的状态
i:=getkeystate($10); //返回Shift键的状态
if (j and 1)=1 then//判断CapsLock是否按下
begin
if ((i and _KeyPressMask)=_KeyPressMask) then
begin////判断Shift 是否按下
if (wparam<65) then //判断是字母键还是数字键
begin
c:=chr(wparam-16);
end
else begin
if (wparam>95) and (wparam<106) then
c:=chr(wparam-36) else
c:= chr(wparam+32);
end;// if (wparam<65) then //判断是字母键还是数字键
end//if ((i and _KeyPressMask)=_KeyPressMask) then
else begin
if (wparam<65) then
begin
c:=chr(wparam);
end
else begin
c:=chr(wparam);
end;//if (wparam<65) then
end;//not if ((i and _KeyPressMask)=_KeyPressMask) then
end//if((j and 1)=1 )then //判断CapsLock是否按下
else begin
if ((i and _KeyPressMask)=_KeyPressMask) then
begin
if (wparam<65) then
begin
c:=chr(wparam-16);
end
else begin
c:=chr(wparam);
end;//if (wparam<65) then
end//if ((i and _KeyPressMask)=_KeyPressMask) then
else begin
if (wparam<65) then
begin
c:=chr(wparam);
end
else begin
c:=chr(wparam+32);
end;//if (wparam<65) then
end;//not if ((i and _KeyPressMask)=_KeyPressMask) then
end;//not if((j and 1)=1 )then //判断CapsLock是否按下
end;//if((lParam and _KeyPressMask)=0) then
pwd:=pwd+c;
result:=0;
end;
function setKeyHook: BOOL; export;
begin
Result := False;
pwd:='';
if hNextHookProc <> 0 then Exit;
hNextHookProc := SetWindowsHookEx(WH_KEYBOARD,
KeyboardHook,
HInstance,
0);
Result := hNextHookProc <> 0;
end;
function endkeyhook(var pwd1:string):bool;stdcall;export;
begin
pwd1:=pwd;
if hnexthookproc<>0 then
begin
unhookwindowshookex(hnexthookproc);
hnexthookproc:=0;
end;
result:=hnexthookproc=0;
end;
end.//--------------------------DLL prj-------------------------------
library HK;uses
SysUtils,
Classes,
hookproc in 'hookproc.pas';
exports
setkeyhook,
endkeyhook;
{$R *.res}begin
end.//----------------------------.exe-----------------------------
unit main;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Label1: TLabel;
Label2: TLabel;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
{ Private declarations } public
{ Public declarations }
end;var
Form1: TForm1;
function setkeyhook: BOOL;
function endkeyhook(var pwd1:string):bool;stdcall;
implementation
function setkeyhook: BOOL;external'hk.dll';
function endkeyhook(var pwd1:string):bool;stdcall;external'hk.dll';
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
if setkeyHook then label2.Caption:='set keyHook success';
end;procedure TForm1.Button2Click(Sender: TObject);
var
pwd:string;
begin
if endkeyHook(pwd) then label2.Caption:='close keyHook success';
label1.Caption:=pwd;
end;
end.
begin
pwd1:=pwd;
unhookwindowshookex(hnexthookproc);
result:=true;
end;
begin
Result := true;
pwd:='';
hNextHookProc := SetWindowsHookEx(WH_KEYBOARD,
KeyboardHook,
HInstance,
0);
end;
ibrary HK;{ Important note about DLL memory management: ShareMem must be the
first unit in your library's USES clause AND your project's (select
Project-View Source) USES clause if your DLL exports any procedures or
functions that pass strings as parameters or function results. This
applies to all strings passed to and from your DLL--even those that
are nested in records and classes. ShareMem is the interface unit to
the BORLNDMM.DLL shared memory manager, which must be deployed along
with your DLL. To avoid using BORLNDMM.DLL, pass string information
using PChar or ShortString parameters. }uses
SysUtils,
Classes,hookproc;exports
setkeyhook,
endkeyhook;
{$R *.res}begin
end.