Сделал глобальный хук на открытие/закрытие и потерю/получение фокуса -
Код:
Library HookDLL;
Uses
Windows, Messages, SysUtils, forms;
Const
GlobMapID = 'Global Hook {917C91AA-88D5-4134-BB91-15161728594D}';
Type
PShareInf = ^TShareInf;
TShareInf = Record
AppWndHandle: HWND;
OldHookHandle: HHOOK;
hm:THandle;
End;
Var
MapHandle: THandle = 0;
ShareInf: PShareInf = nil;
ptr:PByteArray;
f: TextFile;
Procedure DLLEntryPoint(dwReason: DWORD); stdcall;
Begin
Case dwReason Of
DLL_PROCESS_ATTACH:
Begin
MapHandle:=CreateFileMapping(INVALID_HANDLE_VALUE, nil, PAGE_READWRITE, 0, SizeOf(TShareInf), GlobMapID);
ShareInf:=MapViewOfFile(MapHandle, FILE_MAP_ALL_ACCESS, 0, 0, SizeOf(TShareInf));
End;
DLL_PROCESS_DETACH:
Begin
UnMapViewOfFile(ShareInf);
CloseHandle(MapHandle);
End
End;
End;
function CallWndProc(Code: Integer; ParamW: WPARAM; ParamL: LPARAM): LRESULT;stdcall;
begin
if Code in [HC_ACTION, HC_NOREMOVE] then begin
if TCWPStruct(Pointer(ParamL)^).message = WM_CREATE then
writeln(f, 'Открыт файл ' + application.exename);
if TCWPStruct(Pointer(ParamL)^).message = WM_DESTROY then
writeln(f, 'Закрыт файл ' + application.exename);
if TCWPStruct(Pointer(ParamL)^).message = WM_SETFOCUS then
writeln(f, 'Получен фокус ' + application.exename);
if TCWPStruct(Pointer(ParamL)^).message = WM_KILLFOCUS then
writeln(f, 'Потерян фокус ' + application.exename);
end;
Result := CallNextHookEx(ShareInf^.OldHookHandle, Code, ParamW, ParamL)
end;
Function SetKeyboardHook(Wnd: HWND): BOOL; stdcall;
Begin
If ShareInf<>Nil Then
Begin
ShareInf^.AppWndHandle:=Wnd;
ShareInf^.OldHookHandle:=SetWindowsHookEx(WH_CALLWNDPROC, @CallWndProc, HInstance, 0);
Result:=ShareInf^.OldHookHandle<>0;
AssignFile(f, 'c:\bzreport.txt');
Append(f);
End
Else Result:=False
End;
Function RemoveKeyboardHook: BOOL; stdcall;
Begin
Result := UnhookWindowsHookEx(ShareInf^.OldHookHandle);
CloseFile(f);
CloseHandle(ShareInf^.hm);
End;
Exports
SetKeyboardHook, RemoveKeyboardHook;
BEGIN
If DLLProc = Nil Then DLLProc := @DLLEntryPoint;
DLLEntryPoint(DLL_PROCESS_ATTACH);
END.
Код:
Library HookDLL;
Uses
Windows, Messages, SysUtils, forms;
Const
GlobMapID = 'Global Hook {917C91AA-88D5-4134-BB91-15161728594D}';
Type
PShareInf = ^TShareInf;
TShareInf = Record
AppWndHandle: HWND;
OldHookHandle: HHOOK;
hm:THandle;
End;
Var
MapHandle: THandle = 0;
ShareInf: PShareInf = nil;
ptr:PByteArray;
f: TextFile;
Procedure DLLEntryPoint(dwReason: DWORD); stdcall;
Begin
Case dwReason Of
DLL_PROCESS_ATTACH:
Begin
MapHandle:=CreateFileMapping(INVALID_HANDLE_VALUE, nil, PAGE_READWRITE, 0, SizeOf(TShareInf), GlobMapID);
ShareInf:=MapViewOfFile(MapHandle, FILE_MAP_ALL_ACCESS, 0, 0, SizeOf(TShareInf));
End;
DLL_PROCESS_DETACH:
Begin
UnMapViewOfFile(ShareInf);
CloseHandle(MapHandle);
End
End;
End;
function CallWndProc(Code: Integer; ParamW: WPARAM; ParamL: LPARAM): LRESULT;stdcall;
begin
if Code in [HC_ACTION, HC_NOREMOVE] then begin
if TCWPStruct(Pointer(ParamL)^).message = WM_CREATE then
writeln(f, 'Открыт файл ' + application.exename);
if TCWPStruct(Pointer(ParamL)^).message = WM_DESTROY then
writeln(f, 'Закрыт файл ' + application.exename);
if TCWPStruct(Pointer(ParamL)^).message = WM_SETFOCUS then
writeln(f, 'Получен фокус ' + application.exename);
if TCWPStruct(Pointer(ParamL)^).message = WM_KILLFOCUS then
writeln(f, 'Потерян фокус ' + application.exename);
end;
Result := CallNextHookEx(ShareInf^.OldHookHandle, Code, ParamW, ParamL)
end;
Function SetKeyboardHook(Wnd: HWND): BOOL; stdcall;
Begin
If ShareInf<>Nil Then
Begin
ShareInf^.AppWndHandle:=Wnd;
ShareInf^.OldHookHandle:=SetWindowsHookEx(WH_CALLWNDPROC, @CallWndProc, HInstance, 0);
Result:=ShareInf^.OldHookHandle<>0;
AssignFile(f, 'c:\bzreport.txt');
Append(f);
End
Else Result:=False
End;
Function RemoveKeyboardHook: BOOL; stdcall;
Begin
Result := UnhookWindowsHookEx(ShareInf^.OldHookHandle);
CloseFile(f);
CloseHandle(ShareInf^.hm);
End;
Exports
SetKeyboardHook, RemoveKeyboardHook;
BEGIN
If DLLProc = Nil Then DLLProc := @DLLEntryPoint;
DLLEntryPoint(DLL_PROCESS_ATTACH);
END.