uses
Windows,
Messages,
SysUtils;
const
WM_SpecialEvent = WM_User + 1678;
type
THookRec =
record
hMSNHook: HHOOK;
hMSNWnd: HWND;
end;
var
hMap: DWord;
buf: ^THookRec;
function PROC(nCode: Integer; wp: wParam; lp: lParam): LongInt;
stdcall;
var pt : TSmallPoint;
begin
if (nCode >= HC_ACTION)
then
if wp = WM_LButtondown
then begin
pt := PointToSmallPoint(PMouseHookStruct(lp)^.pt);
PostMessage(buf^.hMSNWnd, WM_SpecialEvent, wp, integer(pt));
// z.B. informieren des ursprünglichen Progs darüber, bei welchem X,Y das LButtonDown stattgefunden hat...
{...}
end;
Result := CallNextHookEx(buf^.hMSNHook, nCode, wp, lp);
end;
// sets up hook
function SetHook(ProgHandle, OtherApp : integer): Boolean;
stdcall;
export;
begin
try
Result := false;
if (
not assigned(buf))
then
begin
hMap := CreateFileMapping(DWord(-1),
nil, PAGE_READWRITE, 0, SizeOf(THookRec), '
HookRecMemBlock');
buf := MapViewOfFile(hMap, FILE_MAP_ALL_ACCESS, 0, 0, 0);
buf^.hMSNHook := SetWindowsHookEx(WH_Mouse, @Proc, hInstance, GetWindowThreadProcessId(OtherApp,
nil));
// OtherApp=0 heißt dann: globaler Hook
buf^.hMSNWnd:=HWND(ProgHandle);
Result := true;
end;
except
Result := false;
MessageBox(0, '
error in SetHook', '
error', MB_OK);
end;
end;
// removes hook
function RemoveHook: Boolean;
stdcall;
export;
begin
Result := false;
if (assigned(buf))
then
begin
UnhookWindowsHookEx(buf^.hMSNHook);
buf^.hMSMHook := 0;
UnmapViewOfFile(buf);
buf :=
nil;
CloseHandle(hMap);
hMap := 0;
Result := true;
end;
end;
// DLL entry point
procedure DllEntry(dwReason: DWord);
begin
Case dwReason
of
DLL_PROCESS_ATTACH:
begin
if (
not assigned(buf))
then
begin
hMap := OpenFileMapping(FILE_MAP_ALL_ACCESS, false, '
HookRecMemBlock');
buf := MapViewOfFile(hMap, FILE_MAP_ALL_ACCESS, 0, 0, 0);
CloseHandle(hMap);
hMap := 0;
end;
end;
DLL_PROCESS_DETACH:
begin
if (
not assigned(buf))
then
begin
UnmapViewOfFile(buf);
buf :=
nil;
end;
end;
end;
{ of case }
end;
exports
SetHook,
RemoveHook;
begin // ähnlich dem initialization-Bereich bei Units
DllProc := @DLLEntry;
DllEntry(DLL_PROCESS_ATTACH);
// bewirkt: initiales Memery-Mappen
end.