library Hook;
uses
Windows,
Messages,
sysutils,
graphics;
type
THookRec =
record
Hook: HHOOK;
Wnd: HWND;
oldProc: Integer;
end;
var
map: DWord;
buf: ^THookRec;
startwnd:HWND;
function HookProc(nCode: Integer; wp: wParam; lp: lParam): LongInt;
stdcall;
var
ACanvas: TCanvas;
begin
try
if (nCode >= HC_ACTION)
then
if (PMSG(lP).message=WM_NCPAINT)
and (pmsg(lp).hwnd=buf^.Wnd)
then
begin
ACanvas := TCanvas.Create;
try
ACanvas.Handle := GetWindowDC(startwnd);
with ACanvas
do
begin
Brush.Color := clBlack;
Font.
Name := '
Times New Roman';
Font.Size := 8;
Font.Color := clred;
Font.Style := [fsItalic, fsBold];
TextOut(GetSystemMetrics(SM_CYMENU) + GetSystemMetrics(SM_CXBORDER),
Round((GetSystemMetrics(SM_CYCAPTION) - Abs(Font.Height)) / 2) + 1, '
Test');
end;
finally
ReleaseDC(startwnd, ACanvas.Handle);
ACanvas.Free;
end;
end;
Result := CallNextHookEx(buf^.Hook, nCode, wp, lp);
except
Result := 0;
MessageBox(0, '
Fehler in HookProc', '
error', MB_OK);
end;
end;
// sets up hook
function SetHook: Boolean;
stdcall;
export;
begin
try
Result := false;
if (
not assigned(buf))
then
begin
map := CreateFileMapping(DWord(-1),
nil, PAGE_READWRITE, 0, SizeOf(THookRec), '
HookRecMemBlock');
buf := MapViewOfFile(map, FILE_MAP_ALL_ACCESS, 0, 0, 0);
startwnd:=FindWindow(
nil, '
Name irgendeiner Anwendung');
buf^.Wnd:=startwnd;
buf^.Hook := SetWindowsHookEx(WH_GETMESSAGE, @HookProc, hInstance, GetWindowThreadProcessId(startwnd,
nil));
Result := true;
end;
except
Result := false;
MessageBox(0, '
Fehler in SetHook', '
error', MB_OK);
end;
end;
// DLL entry point
procedure DllEntry(dwReason: DWord);
begin
Case dwReason
of
DLL_PROCESS_ATTACH:
begin
if (
not assigned(buf))
then
begin
map := OpenFileMapping(FILE_MAP_ALL_ACCESS, false, '
HookRecMemBlock');
buf := MapViewOfFile(map, FILE_MAP_ALL_ACCESS, 0, 0, 0);
CloseHandle(map);
map := 0;
end;
end;
DLL_PROCESS_DETACH:
begin
UnmapViewOfFile(buf);
buf :=
nil;
end;
end;
end;
exports
SetHook;
// main
begin
DisableThreadLibraryCalls(hInstance);
DllProc := @DLLEntry;
DllEntry(DLL_PROCESS_ATTACH);
end.