对指定程序安装键盘钩子(100分)

  • 主题发起人 zhang12345
  • 开始时间
Z

zhang12345

Unregistered / Unconfirmed
GUEST, unregistred user!
以下代码在钩子被触发时总是报错,请各位帮小弟看看错在那里?
function Hookproc(iCode:Integer;wParam:wParam;lParam:lParam):LRESULT;stdcall;
begin
beep;
Result := CallNextHookEx(hHook,iCode,wParam,lParam);
end;

procedure SetMessageTrappingHook; stdcall;
var
TheHandle : HWND;
TheThread : DWORD;
begin
TheHandle := FindWindow(NIL, 'FPE 2001');
if TheHandle <> 0 then begin
TheThread := GetWindowThreadProcessId(TheHandle,NIL);
hHook := SetWindowsHookEx(WH_KEYBOARD,@Hookproc,
HInstance,TheThread);
if hHook = 0 then
ShowMessage('Setting Hook Failed.');
end else
showmessage('Icon Author is not currently running.');
end;

 
从你的程序来看,应该是个全局钩子,
既然是全局钩子,应该做成DLL的形式
SetWindowsHookEx(WH_KEYBOARD,@Hookproc,HInstance,TheThread);
///////// nil
你可以在这一句的后面加以下语句进行跟踪:
showmessage(sysErrorMessage(getLastError));
 
好像是线程之间地址访问越界问题,为什么WH_KEYBOARD_LLY底层钩子,对TheThread无效呢?
触发不了,怪。。。。
 
顶部