Home > Back-end >  Hook WH_KEYBOARD_ll failure problem
Hook WH_KEYBOARD_ll failure problem

Time:12-03

Hook WH_KEYBOARD_ll run on most of the computer is good, but in some computer hook in uncertain time, hook fails,
Please provide some slightly, thank you


The unit uhook;

Interface

USES the
Winapi. Windows, Winapi Messages, System. SysUtils, System. Variants, System. Classes, Vcl. Graphics,
The Vcl. Controls, Vcl. Forms, Vcl. Dialogs, Vcl. StdCtrls, Vcl. Buttons;

Type
TForm1=class (TForm)
Memo1: TMemo;
BtnHook: TBitBtn;
BtnUnhook: TBitBtn;
Procedure FormCreate (Sender: TObject);
Procedure FormDestroy (Sender: TObject);
Procedure btnHookClick (Sender: TObject);
Procedure btnUnhookClick (Sender: TObject);
Private
{Private declarations}
Public
{Public declarations}
end;

Var
Form1: TForm1;

HHkKeyboard: hhook;
IKeyboardTypeCount: Integer;

The function Setkeyhook: Boolean;
The function Endkeyhook: Boolean;
The function WriteLog (const sContent: string) : Boolean;

Implementation

{$R *. DFM}


The function KeyboardHookProc (iCode: Integer; WParam: wParam; LParam: lParam) : LRESULT; Stdcall;
The begin
WriteLog (' into the hook procedure);
If iCode<0 then//in accordance with the SDK documentation
The begin
Result:=CallNextHookEx (hHkKeyboard, iCode wParam, lParam);
Exit;
end;

If wParam=WM_KEYDOWN then//equipment action
The begin
IKeyboardTypeCount:=iKeyboardTypeCount + 1;
WriteLog (' keyboard number: + inttostr (iKeyboardTypeCount))
end;

Result:=CallNextHookEx (hHkKeyboard, iCode wparam, lparam);
WriteLog (' exit hook program + UIntToStr GetLastError ());

end;

The function Setkeyhook: Boolean;
The begin
If hHkKeyboard=0 then
The begin
HHkKeyboard:=SetwindowsHookEx (WH_KEYBOARD_ll, @ KeyboardHookProc HInstance, 0).//load hook
WriteLog (' hhook + IntToStr (hHkKeyboard));
end;
Result:=hHkKeyboard & lt;> 0;
end;

The function Endkeyhook: Boolean;
The begin
If hHkKeyboard & lt;> 0 then
The begin
Unhookwindowshookex (hHkKeyboard);//uninstall hooks
HHkKeyboard:=0;
end;
The result: hHkKeyboard==0;
end;


The function WriteLog (const sContent: string) : Boolean;
Var
SPath sLogFile: string;
SDate sTime, sText: string;
LogFile: TextFile;
The begin
Result:=True;
SDate=DateToStr (Now);
STime:=TimeToStr (Now);

SPath:=GetHomePath + '\ DORA';
If not directoryExists (sPath) then
ForceDirectories (sPath);
If sPath [Length (sPath)] <> The '\' then sPath: sPath +='\';

SPath:=sPath + 'Log \';
If not DirectoryExists (sPath) then
The begin
If not ForceDirectories (sPath) then
The begin
Result:=False;
Exit;
end;
end;
SLogFile:=sPath + 'log_Comm_test. TXT';
AssignFile (logFile, sLogFile);
If not FileExists (sLogFile) then
Rewrite (logFile)
The else
Append (logFile);

Try
SText:='& lt; Time: '+ sDate +' '+ sTime + operator:' + 'a' + '& gt; ';
SText:=sText + sContent;
Form1. Memo1. Lines. The Add (sText);
Writeln (logFile, sText);
The finally
CloseFile (logFile);
end;
end;

Procedure TForm1. BtnHookClick (Sender: TObject);
The begin
If Setkeyhook then
The begin
WriteLog (' hook: success);
End
The else begin
WriteLog (' hook: failure '+ inttostr GetLastError ());
end;
end;

Procedure TForm1. BtnUnhookClick (Sender: TObject);
The begin
Endkeyhook;
end;

Procedure TForm1. FormCreate (Sender: TObject);
The begin
IKeyboardTypeCount:=0;
HHkKeyboard:=0;
If Setkeyhook then
The begin
WriteLog (' hook: success);
End
The else begin
WriteLog (' hook: failure '+ inttostr GetLastError ());
end;
end;

Procedure TForm1. FormDestroy (Sender: TObject);
The begin
Endkeyhook;
end;

end.

CodePudding user response:

Please provide some solution

CodePudding user response:

Written in a DLL,,,

CodePudding user response:

I tried some DLL, hook will still be off, I don't know why,

CodePudding user response:

This is I before the keyboard hook, is relatively stable, look at your code, don't write files in the hook, hook elapsed time less as far as possible, have write file operations, to add to your thread queue to operate, so it's good that
 
The function KeyBoardProc (nCode: Integer; WParam: WParam; LParam: LParam) : LRESULT; Stdcall;
The begin
If (nCode=HC_ACTION) then
The begin
If ((WParam=WM_KEYUP) or (WParam=WM_KEYDOWN) or (WParam=WM_SYSKEYDOWN) or (WParam=WM_SYSKEYUP)) then
The begin
//OutputDebugStringA (PAnsiChar (AnsiString (' ProcKeyBoard '+' Msg_id='+ IntToHex (WParam, 8))));
//OutputDebugStringA (PAnsiChar (AnsiString (' ProcKeyBoard '+' FKeyboardCallHandle='+ IntToHex (FKeyboardCallHandle, 8))));
end;

Case WParam of
WM_KEYUP:
The begin
If ReportMemoryLeaksOnShutdown then
The begin
OutputDebugStringA (PAnsiChar (AnsiString (' WM_KEYUP key='+ CRH (PKeyBoardHookStruct (LParam). VkCode))));
end;
end;
WM_KEYDOWN:
The begin
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related