UnhookWindowsHookEx(HookHandle);
InstHandle :DWord;
MessageID :DWord;
end;
SysUtils,
Windows,
untMouseHook in 'untMouseHook.pas',
untMouseHookConst in 'untMouseHookConst.pas';
StartHook,StopHook;
end.
DLL模块源代码:untMouseHook.pas
unit untMouseHook;
pSharedMem :^TSharedMem; //Pointer for Shared Memory
HookHandle :HHook; //Handle for the hook
function StopHook:BOOL; stdcall;
begin
Result := 0;
if nCode<0 then Result:=CallNextHookEx(HookHandle,nCode,wParam,lParam);
//Rule of API call, which referred to Win32 Hooks topic in MSDN
SendMessage(pSharedMem^.InstHandle,pSharedMem^.MessageID,0,0);
//Sends Message to Instance to which was injected this DLL
end;
begin
Result := False;
if HookHandle<>0 then Exit; //Already Installed the hook
pSharedMem^.InstHandle := Sender;
pSharedMem^.MessageID := MessageID;
HookHandle := SetWindowsHookEx(WH_MOUSE,MouseProc,hInstance,0);
Result := HookHandle <> 0;
end;
begin
if HookHandle <> 0 then
begin
UnhookWindowsHookEx(HookHandle);
HookHandle := 0;
end;
Result := HookHandle = 0;
end;
//Try to open an existing mapping file as MappingFileName specified
if hMappingFile = 0 then //Not exist
hMappingFile := CreateFileMapping($FFFFFFFF,nil,PAGE_READWRITE,0,sizeof(TSharedMem),MappingFileName);
//Here $FFFFFFFF is a invalid file handle, which cause this file being created in Windows page file.
Exception.Create('Unable to create shared memory. Make sure your system have enough memory and page file space.');
//Details of this API call, refer to MapViewOfFile in MSDN
if pSharedMem = nil then //Create a pointer to the mapped file
begin
CloseHandle(hMappingFile);
Exception.Create('Unable to map shared memory. Program halt.');
end;
//Whether HookHandle = 0 is used to judge if this hooked was installed
//In function StartHook, we will later give a value to HookHandle
CloseHandle(hMappingFile);
公用模块源代码:untMouseHookConst.pas
unit untMouseHookConst;
InstHandle :DWord;
MessageID :DWord;
end;
EXE模块源代码:untMain.pas
unit untMain;
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, untMouseHookConst, StdCtrls, ExtCtrls;
TfrmMain = class(TForm)
lblMain: TLabel;
tmrMain: TTimer;
procedure tmrMainTimer(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
procedure WndProc(var Messages:TMessage); override;
end;
DLLFileName = 'dllTOEFLHook.dll';
frmMain :TfrmMain;
hMappingFile :THandle;
pSharedMem :^TSharedMem;
time_counter :integer;
function StopHook:BOOL; stdcall; external DLLFileName;
begin
if not StopHook then
Exception.Create('Unable to uninstall the mouse hook. Abnormal termination.');
end;
begin
pSharedMem := nil;
if not StartHook(frmMain.Handle,MessageID) then //Sends handle and MessageID to DLL
Exception.Create('Unable to install a mouse hook. Program halt.');
time_counter := 0;
end;
begin
inc(time_counter);
lblMain.Caption := IntToStr(time_counter);
end;
begin
if pSharedMem = nil then
begin
hMappingFile := OpenFileMapping(FILE_MAP_WRITE,False,MappingFileName);
if hMappingFile = 0 then Exception.Create('Unable to access shared memory. Program halt.');
pSharedMem := MapViewOfFile(hMappingFile,FILE_MAP_WRITE or FILE_MAP_READ,0,0,0);
if pSharedMem = nil then
begin
CloseHandle(hMappingFile);
Exception.Create('Unable to map to shared memory. Program halt.');
end;
end;
if pSharedMem = nil then exit; //Halt program if unable to create/open/map shared memory.
begin
time_counter := 0;
lblMain.Caption := 'CLICK!';
tmrMain.Interval := 0;
tmrMain.Interval := 1000; //Cause timer to restart timing
end
else Inherited; //Do traditional WndProc without override
end;