我有一个应用程序,并希望监视MSWord按键(本地钩),但我不知道如何找到使用的pid!波纹管代码运行良好,具有全局钩子(pid
= 0
)和(pid
= GetCurrentThreadId
)。但是doesn't与GetWindowThreadProcessId
工作:本地钩不工作
HWND hWindow = FindWindowEx(NULL,NULL,String("Notepad").w_str(),NULL);
if (!hWindow) {
ShowMessage("hWindow fail");
return;
}
unsigned long pid;
GetWindowThreadProcessId(hWindow ,&pid);
//pid = GetCurrentThreadId();
if (!hWindow) {
ShowMessage("pid fail");
return;
}
String s = "HookDLL.dll";
DllHandle=LoadLibrary(s.w_str());
HOOKFCT_2 InstHook=reinterpret_cast<HOOKFCT_2> (GetProcAddress(DllHandle,"InstallHook"));
if(!InstHook(pid, (void *)(callIt)))
{
Label1->Caption="Unable to install mouse hook!";
}
else Label1->Caption="Mouse hook installed!";
我会非常,非常对这个问题的任何光gratefuLl ...
注意:
祝钩仅MSWord。
上述代码的工作,只是试图钩住另一应用程序时failling(即:不使用
pid
=0
或pid
=GetCurrentThreadId
),导致= “无法安装鼠标钩子!”。我已经试过
FindWindow
,FindWindowEx
,GetForegroundWindow
,GetActiveWindow
;由于不是这个作品,我相信问题是GetWindowThreadProcessId
。
'GetCurrentThreadId'只返回一个线程ID,而不是进程ID。您应该使用'GetWindowThreadProcessId'来检索拥有指定窗口的进程ID。请注意,该窗口必须属于* MSWord *应用程序。还检查该钩子DLL文档的任何限制。如果没有关于钩子DLL使用什么方法的详细信息,就不可能猜出可能导致问题的原因。 – Jay