读书人

钩子失败,该如何解决

发布时间: 2012-03-16 16:34:56 作者: rapoo

钩子失败
我建立了一个系统的钩子,用WH_KEYBOARD钩子失败。用其他的钩子类型就不会出错,难到WH_KEYBOARD不支持系统钩子,还是我的代码出错

[解决办法]
代码呢?拿出来看看阿

一下是我写的一个代码,不会出错

library KeyBoard;

uses
SysUtils,
Windows,
ShellApi,
Messages,
WinProcs,
Classes;

var
KBHook:HHook;
IsHooked:boolean;

function KeyBoardProc(Code:integer;wParam:WPARAM;lParam:LPARAM):LRESULT;stdcall;
const
_keypressmask=$80000000;
begin
result:=0;
if Code <0 then
begin
CallNextHookEx(KBHook,Code,wParam,lParam);
end;

if ((lparam and _keypressmask)=0) and (GetAsyncKeyState(VK_CONTROL) <0) and (GetAsyncKeyState(VK_MENU) <0) and (wParam=47) then
begin
ShellExecute(0, 'Open ', 'NotePad.exe ',nil,nil,SW_SHOW);
result:=1;
end;

end;

function StartHook:boolean;stdcall;
begin
result:=false;
if IsHooked then exit;
KBHook:=SetWindowsHookEx(WH_KEYBOARD,@KeyBoardProc,hInstance,0);
if KBHook <> 0 then
begin
Result:=true;
IsHooked:=true
end
else
IsHooked:=false;
end;

function RemoveHook:boolean;stdcall;
begin
result:=false;
if (not IsHooked) and (KBHook <> 0) then exit;
UnHookWindowsHookEx(KBHook);
Result:=true;
IsHooked:=false;
end;

exports
StartHook,RemoveHook;

{$R *.res}

begin
end.

读书人网 >.NET

热点推荐