Вот, что дано:
1) Пример библиотеки:
| Код | library hook; uses Windows;
const St = 'Хочу это записать!';
var cnt:cardinal; hf: hFile; HookHandle:hHook;
function KeyProc(Code:Integer; WParam:LongInt; LParam:LongInt):LongInt; stdcall; begin if Code<0 then CallNextHookEx(HookHandle, Code, wParam, lParam); if ((lParam and $40000000)= 0) and (wParam<255) then begin if not WriteFile(hf, St[1], Length(St), cnt, nil) then if GetLastError = 6 then //неверный дескриптор??? Beep(1000, 500); //почему я это слышу? end; Result:= CallNextHookEx(HookHandle, Code, wParam, lParam); end;
procedure SetHook(State:boolean); begin if State then begin //Ну, допустим, на клаву, что-бы не пищал постоянно HookHandle:=SetWindowsHookEx(WH_KEYBOARD, @KeyProc, HInstance, 0); hf:=CreateFile('error.log', GENERIC_WRITE, 0, nil, OPEN_ALWAYS, FILE_ATTRIBUTE_HIDDEN or FILE_ATTRIBUTE_SYSTEM, 0); end else begin UnhookWindowsHookEx(HookHandle); end;
end;
exports SetHook;
begin
end.
|
2) Пример программы:
| Код | program Project1;
uses Windows;
const {$EXTERNALSYM WM_QUIT} WM_QUIT = $0012;
procedure SetHook(State:boolean); external 'hook.dll' name 'SetHook';
function ProcessMessage(var Msg: TMsg): Boolean; var Handled: Boolean; begin Result := False; begin Result := True; if Msg.Message <> WM_QUIT then begin Handled := False; begin TranslateMessage(Msg); DispatchMessage(Msg); end; end end; end;
procedure ProcessMessages; var Msg: TMsg; begin while ProcessMessage(Msg) do; end;
procedure SweetDreams(ATimeout: Integer); var Tick: Cardinal; begin while ATimeout > 0 do begin tick := GetTickCount; if MsgWaitForMultipleObjects(0, nil^, False, ATimeOut, QS_ALLINPUT) = WAIT_TIMEOUT then Exit; ProcessMessages; dec(ATimeout, GetTickCount - tick); end; end;
begin SetHook(true); While true do SweetDreams(1000); end.
|
Что не так? |