我有一個應用程序,並希望監視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