HOOK进程失败,大家帮看看是何原因?

Think-In-C 2010-01-21 12:05:20

var
Form1: TForm1;
hhk: HHOOK;
function GetMsgProc(nCode:integer;wParam:WPARAM;lParam:LPARAM):LRESULT;stdcall;

implementation

{$R *.dfm}

function GetMsgProc(nCode:integer;wParam:WPARAM;lParam:LPARAM):LRESULT;stdcall;
begin
result := 1;
if nCode<>HC_ACTION then exit;
MessageBox(0,'ok','ok',MB_OK);
result := CallNextHookEx(hhk,nCode,wParam,lParam);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
hwnd,pid:dword;
begin
try
hwnd := FindWindow('Notepad',nil);
if hwnd=0 then exit;
GetWindowThreadProcessId(hwnd,pid);
if pid=0 then exit;
hhk := SetWindowsHookEx(13,@GetMsgProc,HINSTANCE,pid);
//这里pid改为0就没问题,可以成功HOOK,如果设置为找到的pid,就HOOK失败,这个pid和任务管理器的pid是一致的。
//请问为什么HOOK失败呢?
if hhk=0 then exit;
Caption := 'hook on';
except
Caption := '出错';
end;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
if (hhk<>0) and UnHookWindowsHookEx(hhk) then Caption := 'unhook';
end;

...全文
238 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
Think-In-C 2010-02-05
  • 打赏
  • 举报
回复
谢谢朋友们的支持,整忙了半个月,才结这个帖,对不起,朋友们。谢谢。
cnzdgs 2010-01-23
  • 打赏
  • 举报
回复
在反驳别人之前总要先把人家回复的内容看明白吧~
slmax1 2010-01-22
  • 打赏
  • 举报
回复
人家题意要求是HOOK进程而不是全局HOOK

把线程ID改正一下即可
dd_zhouqian 2010-01-21
  • 打赏
  • 举报
回复
竟然还messagebox
等着退出吧
cnzdgs 2010-01-21
  • 打赏
  • 举报
回复
你获得的pid是进程ID,而函数需要给出的是线程ID。不过这不是主要问题。

主要问题是键盘和鼠标钩子只能用全局Hook(SetWindowsHookEx的最后参数给0)。MSDN中SetWindowsHookEx的说明下面的Remark部分有一张表,列出的各种Hook哪些可以Hook线程、哪些只能Hook全局。
hjkto 2010-01-21
  • 打赏
  • 举报
回复
帮顶一下
slmax1 2010-01-21
  • 打赏
  • 举报
回复
Parameters
hWnd
Handle to the window.
lpdwProcessId
Pointer to a 32-bit value that receives the process identifier. If this parameter is not NULL, GetWindowThreadProcessId copies the identifier of the process to the 32-bit value; otherwise, it does not.
Return Values
The return value is the identifier of the thread that created the window.

看看MSDN的吧,返回值才是线程ID
slmax1 2010-01-21
  • 打赏
  • 举报
回复
dwThread:=GetWindowThreadProcessId(hWnd,nil);
slmax1 2010-01-21
  • 打赏
  • 举报
回复
hhk := SetWindowsHookEx(13,@GetMsgProc,HINSTANCE,pid);
//这里需要的是线程ID而不是进程ID

GetWindowThreadProcessId(hwnd,pid);
//你这里得到的是进程ID

Think-In-C 2010-01-21
  • 打赏
  • 举报
回复
如果给少了,我再加了。。。。高手帮帮忙啊。
Think-In-C 2010-01-21
  • 打赏
  • 举报
回复
是不是我的分数给得少了啊。。。

1,183

社区成员

发帖
与我相关
我的任务
社区描述
Delphi Windows SDK/API
社区管理员
  • Windows SDK/API社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧