1。DLL文件代码:
unit Global;
interface
uses windows,Local;
//define the structure of the SetHookHandle function in hookdll.dll
type TSetHookHandle = procedure(HookHandle: HHook); stdcall;
var LibLoaded: boolean; //true if hookdll.dll is already loaded
LibHandle: HInst; //dll handle
HookProcAdd: pointer; //memory address of hook procedure in windows
GHookInstalled: boolean;
SetHookHandle: TSetHookHandle;
function LoadHookProc: boolean;
function SetupGlobalHook: boolean;
implementation
{
LoadHookProc
------------
This function loads the hook procedure from the dll created in hookdll.dll
and obtains a handle for the dll and the address of the procedure in the
dll. The procedure will be called 'GlobalKeyBoardHook'
This procedure also loads the SetHookHandle procedure in hookdll.dll. As
explained in the dll code, this procedure is simply used to inform the dll
of the handle for the current hook, which is needed to call CallNextHookEx
and also to initialise the keyarray (see the dll code).
}
function LoadHookProc: boolean;
begin
//attempt to load the dll containing our hook proc
LibHandle:=LoadLibrary('hookdll.dll');
if LibHandle=0 then begin //if loading fails, exit and return false
LoadHookProc:=false;
exit;
end;
//once the dll is loaded, get the address in the dll of our hook proc
HookProcAdd:=GetProcAddress(LibHandle,'GlobalKeyBoardHook');
@SetHookHandle:=GetProcAddress(LibHandle,'SetHookHandle');
if (HookProcAdd=nil)or(@SetHookHandle=nil) then begin //if loading fails, unload library, exit and return false
FreeLibrary(LibHandle);
LoadHookProc:=false;
exit;
end;
LoadHookProc:=true;
end;
{
SetupGlobalHook
---------------
This function installs a global hook. To the install a global hook, we first have
to load the hook procedure that we have written from hookdll.dll, using the LoadHookProc
above. If succesful use the setwindowshookex function specifying the hook type as WH_KEYBOARD,
the address of the hook procedure is that loaded from hookdll.dll, the hMod is the handle
of the loaded hookdll.dll and the threadid is set to 0 to indicate a global hook.
}
function SetupGlobalHook: boolean;
begin
SetupGlobalHook:=false;
if LibLoaded=false then LibLoaded:=LoadHookProc; //if hookdll isnt loaded, load it
if LibLoaded=false then exit; //if dll loading fails, exit
CurrentHook:=setwindowshookex(WH_KEYBOARD,HookProcAdd,LibHandle,0); //install hook
SetHookHandle(CurrentHook);
if CurrentHook<>0 then SetupGlobalHook:=true; //return true if it worked
end;
end.
2。运行的另一个程序,监控键盘,当通过键盘键入的字超过20个时,存入log.txt文件。
unit MainFormUnit;
procedure TMainForm.Button1Click(Sender: TObject);
begin
if GHookInstalled=true then exit; //if a global hook is installed, exit routine
//if a local hook not installed, then attempt to install one, else attempt to remove one
if HookInstalled=false then HookInstalled:=SetupLocalHook else HookInstalled:=not(RemoveLocalHook);
end;
procedure TMainForm.FormCreate(Sender: TObject);
begin
HookInstalled:=false;
GHookInstalled:=false;
LibLoaded:=false;
end;
procedure TMainForm.FormDestroy(Sender: TObject);
begin
if HookInstalled=true then RemoveLocalHook;
end;
procedure TMainForm.Button2Click(Sender: TObject);
begin
if HookInstalled=true then exit; //if a local hook is installed, exit routine
//if a local hook not installed, then attempt to install one, else attempt to remove one
//note that removelocalhook can still be used no matter whether the hook is global or local
if GHookInstalled=false then GHookInstalled:=SetupGlobalHook else GHookInstalled:=not(RemoveLocalHook);
end;