Главная » Delphi » Win API » GlobalHook

GlobalHook

17:11
That's cool for you... but I still don't have it working... :o(
I have stripped down your code, Jim, and put some of mine in it. Below is
what's left. What's wrong? The SetWindowsHookEx is being called but it
returns 0! I give it exactly the same arguments as you do in your code!

Thanks, Jeroen

PS sorry for the long posting, but since it's non working code, I thought
not to post it in attachments.

// -------------------------------------------------------------------------
- - - -
 
unit GlobalHook;
 
interface
 
uses
Windows, HookMessages;
 
procedure SetGlobalHook(HookType: THookType; AppHandle: THandle); stdcall;
procedure UnsetGlobalHook(HookType: THookType); stdcall;
 
implementation
 
uses SysUtils, Dialogs;
 
// -------------------------------------------------------------------------
- - - -
 
procedure RetrieveMemMap(var MemMap: THandle; var Data: Pointer);
var
ErrStr: PChar;
begin
// This procedure is called for every call to a hook procedure. It does a
// request for a memory mapped file with the name MapName. If it doesn't get
// the memory, it sets both parameters to zero.
 
MemMap := OpenFileMapping(FILE_MAP_ALL_ACCESS, False, MemMapName);
if MemMap <> 0 then
begin
Data := MapViewOfFile(MemMap, FILE_MAP_ALL_ACCESS, 0, 0, 0);
if not Assigned(Data) then CloseHandle(MemMap);
end
else
begin
Data := nil;
ErrStr := StrAlloc(200);
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, nil, GetLastError, 0, ErrStr,
200, nil);
ShowMessage(ErrStr);
StrDispose(ErrStr);
end;
end;
 
procedure ReleaseMemMap(var MemMap: THandle; var Data: Pointer);
begin
if (MemMap <> 0) and Assigned(Data) then
begin
UnmapViewOfFile(Data);
CloseHandle(MemMap);
end;
end;
 
// -------------------------------------------------------------------------
- - - -
 
function WHProcCbt(Code: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT;
stdcall;
var
MemMap: THandle;
Shared: PShared;
begin
Result := 0;
RetrieveMemMap(MemMap, Pointer(Shared));
if MemMap = 0 then Exit;
 
// Post message to application
case Code of
HCBT_ACTIVATE: PostMessage(Shared.AppHandle, WH_CBT_ACTIVATE,
wParam, lParam);
HCBT_CLICKSKIPPED: PostMessage(Shared.AppHandle, WH_CBT_CLICKSKIPPED,
wParam, lParam);
HCBT_CREATEWND: PostMessage(Shared.AppHandle, WH_CBT_CREATEWND,
wParam, lParam);
HCBT_DESTROYWND: PostMessage(Shared.AppHandle, WH_CBT_DESTROYWND,
wParam, lParam);
HCBT_KEYSKIPPED: PostMessage(Shared.AppHandle, WH_CBT_KEYSKIPPED,
wParam, lParam);
HCBT_MINMAX: PostMessage(Shared.AppHandle, WH_CBT_MINMAX, wParam,
lParam);
HCBT_MOVESIZE: PostMessage(Shared.AppHandle, WH_CBT_MOVESIZE,
wParam, lParam);
HCBT_QS: PostMessage(Shared.AppHandle, WH_CBT_QS, wParam,
lParam);
HCBT_SETFOCUS: PostMessage(Shared.AppHandle, WH_CBT_SETFOCUS,
wParam, lParam);
HCBT_SYSCOMMAND: PostMessage(Shared.AppHandle, WH_CBT_SYSCOMMAND,
wParam, lParam);
end;
 
Result := CallNextHookEx(Shared.CbtHookHandle, Code, wParam, lParam);
 
ReleaseMemMap(MemMap, Pointer(Shared));
end;
 
function WHProcMouse(Code: Integer; wParam: WPARAM; lParam: LPARAM):
LRESULT; stdcall;
var
MemMap: THandle;
Shared: PShared;
begin
Result := 0;
RetrieveMemMap(MemMap, Pointer(Shared));
if MemMap = 0 then Exit;
 
// Post message to application
PostMessage(Shared.AppHandle, WH_MOUSE, wParam, lParam);
Result := CallNextHookEx(Shared.MouseHookHandle, Code, wParam, lParam);
 
ReleaseMemMap(MemMap, Pointer(Shared));
end;
 
// -------------------------------------------------------------------------
- - - -
 
procedure SetGlobalHook(HookType: THookType; AppHandle: THandle); stdcall;
var
MemMap: THandle;
Shared: PShared;
Tmp: Integer;
begin
RetrieveMemMap(MemMap, Pointer(Shared));
if MemMap = 0 then Exit;
Shared.AppHandle := AppHandle;
 
case HookType of
WH_CBT: Shared^.CbtHookHandle := SetWindowsHookEx(HookType,
WHProcCbt, hInstance, 0);
WH_MOUSE: Shared^.MouseHookHandle := SetWindowsHookEx(HookType,
WHProcMouse, hInstance, 0);
end;
 
Tmp := Shared^.CbtHookHandle;
ShowMessage(IntToStr(Tmp));
 
ReleaseMemMap(MemMap, Pointer(Shared));
end;
 
procedure UnsetGlobalHook(HookType: THookType); stdcall;
var
MemMap: THandle;
Shared: PShared;
begin
RetrieveMemMap(MemMap, Pointer(Shared));
if MemMap = 0 then Exit;
 
case HookType of
WH_CBT: UnhookWindowsHookEx(Shared.CbtHookHandle);
WH_MOUSE: UnhookWindowsHookEx(Shared.MouseHookHandle);
end;
 
ReleaseMemMap(MemMap, Pointer(Shared));
end;
 
// -------------------------------------------------------------------------
- - - -
 
initialization
DisableThreadLibraryCalls(hInstance);
 
end.