library hooks;
uses
Windows,
Messages,
Dialogs;
var
HookHandle: Cardinal = 0;
WindowHandle: Cardinal = 0;
const
keybmsg = '
KeyboardHookMessage_Enemyleft';
{$INCLUDE Include\HookingDLL_Data.pas}
function GetPView: Pointer;
stdcall;
begin
Result := lpView;
end;
function CreateMMF: Boolean;
begin
Result := False;
MapHandle := CreateFileMapping($FFFFFFFF,
nil, PAGE_READWRITE, 0, SizeOf(TDLLData), MMFName);
if MapHandle <> 0
then
begin
if Assigned(lpView)
then
Result := True
else
CloseHandle(MapHandle);
end;
end;
function OpenAndMapMMF: Pointer;
begin
Result :=
nil;
MapHandle := CreateFileMapping($FFFFFFFF,
nil, PAGE_READWRITE, 0, SizeOf(TDLLData), MMFName);
if MapHandle <> 0
then
try
lpView := MapViewOfFile(MapHandle, FILE_MAP_ALL_ACCESS, 0, 0, SizeOf(TDLLData))
finally
CloseHandle(MapHandle);
end;
Result := lpView;
end;
function CloseAndUnmapMMF(view: Pointer): Boolean;
begin
result := UnMapViewOfFile(view);
end;
function KeyboardHookProc(nCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT;
stdcall;
var
lpView: PDLLData;
begin
// it's possible to call CallNextHookEx conditional only.
Result := CallNextHookEx(HookHandle, nCode, wParam, lParam);
case nCode < 0
of
TRUE: exit;
// if code smaller 0 nothing has to be done
FALSE:
begin
lpView := OpenAndMapMMF;
if Assigned(lpView)
then
try
lpView^.nCode := nCode;
lpView^.ProcessID := GetCurrentProcessID;
lpView^.ThreadID := GetCurrentThreadID;
lpView^.TestStr := '
#enemyleft';
// we use HWND_BROADCAST to send the message to any window which handles this WM
SendMessage(HWND_BROADCAST, lpView^.WM_KEYBHOOKMSG, wParam, lParam);
finally
CloseAndUnMapMMF(lpView);
end;
end;
end;
end;
function InstallHook(Hwnd: Cardinal): Boolean;
stdcall;
begin
//lpView^.WM_KEYBHOOKMSG := WM_USER+1;
// WM_KEYBHOOKMSG := RegisterWindowMessage(WM_USER+13);
Result := False;
if HookHandle = 0
then
begin
// install the hook
HookHandle := SetWindowsHookEx(WH_KEYBOARD, @KeyboardHookProc, HInstance, 0);
// save the given window handle
WindowHandle := Hwnd;
Result := TRUE;
end;
end;
procedure FreeMMF;
begin
//UnmapviewofFile(lpView);
//CloseHandle(MapHandle);
end;
function UninstallHook: Boolean;
stdcall;
begin
//
FreeMMF;
// uninstall hook from hook chain
Result := UnhookWindowsHookEx(HookHandle);
HookHandle := 0;
end;
procedure DLLMain;
begin
if CreateMMF
then
begin
// register our private window messages
lpView^.WM_KEYBHOOKMSG := RegisterWindowMessage(keybmsg);
end;
//FreeMMF;
end;
exports
// export the installation and deinstallation routine
InstallHook,
UninstallHook,
GetPView;
begin
end.