library Hook;
uses
Windows,
Messages;
type
TPMsg = ^TMsg;
PMMFData = ^TMMFData;
TMMFData =
record
NextHook: HHOOK;
WinHandle: HWND;
MsgToSend: Integer;
end;
var
HookHandle: Cardinal = 0;
WindowHandle: Cardinal = 0;
FileMapObj: THandle;
FileMapView: ^Integer;
Kill: Boolean;
MMFHandle: THandle;
MMFData: PMMFData;
const
WM_MOUSEHOOKMSG = WM_USER + 2706;
MMFName = '
CSMouseHook';
function CreateMMF(
Name:
string; Size: Integer): THandle;
begin
Result := CreateFileMapping($FFFFFFFF,
nil, PAGE_READWRITE, 0, Size, PChar(
Name));
if Result <> 0
then
begin
if GetLastError = ERROR_ALREADY_EXISTS
then
begin
CloseHandle(Result);
Result := 0;
end;
end;
end;
function OpenMMF(
Name:
string): THandle;
begin
Result := OpenFileMapping(FILE_MAP_ALL_ACCESS, False, PChar(
Name));
// The return value is an open handle to the specified file-mapping object.
end;
function MapMMF(MMFHandle: THandle): Pointer;
begin
Result := MapViewOfFile(MMFHandle, FILE_MAP_ALL_ACCESS, 0, 0, 0);
end;
function UnMapMMF(P: Pointer): Boolean;
begin
Result := UnmapViewOfFile(P);
end;
function CloseMMF(MMFHandle: THandle): Boolean;
begin
Result := CloseHandle(MMFHandle);
end;
function UnMapAndCloseMMF: Boolean;
begin
Result := False;
if UnMapMMF(MMFData)
then
begin
MMFData :=
nil;
if CloseMMF(MMFHandle)
then
begin
MMFHandle := 0;
Result := True;
end;
end;
end;
function MouseHookProc(nCode: Integer; wParam: WPARAM; lParam: LPARAM):
LRESULT;
stdcall;
begin
Result := 0;
MMFHandle := OpenMMF(MMFName);
if MMFHandle <> 0
then
begin
MMFData := MapMMF(MMFHandle);
if MMFData <>
nil then
begin
if (nCode < 0)
or (wParam = PM_NOREMOVE)
then
Result := CallNextHookEx(MMFData.NextHook, nCode, wParam, lParam)
else
begin
Kill := True;
SendMessage (MMFData.WinHandle, WM_MOUSEHOOKMSG, wParam, lParam);
//if Kill then TPMsg(lParam)^.message := WM_NULL;
Result := CallNextHookEx(MMFData.NextHook, nCode, wParam, lParam)
end;
UnMapMMF(MMFData);
end;
CloseMMF(MMFHandle);
end;
end;
function InstallHook(Hwnd: Cardinal): Boolean;
stdcall;
begin
Result := False;
MMFHandle := 0;
MMFData :=
nil;
Result := False;
if (MMFData =
nil)
and (MMFHandle = 0)
then
begin
MMFHandle := CreateMMF(MMFName, SizeOf(TMMFData));
if MMFHandle <> 0
then
begin
MMFData := MapMMF(MMFHandle);
if MMFData <>
nil then
begin
MMFData.WinHandle := hWnd;
// MMFData.MsgToSend := MsgToSend;
MMFData.NextHook := SetWindowsHookEx(WH_MOUSE, @MouseHookProc, HInstance, 0);
if MMFData.NextHook = 0
then
UnMapAndCloseMMF
else
Result := True;
end
else
begin
CloseMMF(MMFHandle);
MMFHandle := 0;
end;
end;
end;
end;
function UninstallHook: Boolean;
stdcall;
begin
//Hook aus der Hookchain entfernen
//Uninstall hook from hook chain
Result := False;
if (MMFData <>
nil)
and (MMFHandle <> 0)
then
if UnHookWindowsHookEx(MMFData.NextHook)
then
Result := UnMapAndCloseMMF;
end;
exports
//Installations- und Deinstallationsroutine exportieren
//Export the installation and deinstallation routine
InstallHook,
UninstallHook;
end.