2012-08-29 37 views
1

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

注意:

  1. 祝鉤僅MSWord

  2. 上述代碼的工作,只是試圖鉤住另一應用程序時failling(即:不使用pid = 0pid = GetCurrentThreadId),導致= 「無法安裝鼠標鉤子!」

  3. 我已經試過FindWindow,FindWindowEx,GetForegroundWindow,GetActiveWindow;由於不是這個作品,我相信問題是GetWindowThreadProcessId

+0

'GetCurrentThreadId'只返回一個線程ID,而不是進程ID。您應該使用'GetWindowThreadProcessId'來檢索擁有指定窗口的進程ID。請注意,該窗口必須屬於* MSWord *應用程序。還檢查該鉤子DLL文檔的任何限制。如果沒有關於鉤子DLL使用什麼方法的詳細信息,就不可能猜出可能導致問題的原因。 – Jay

回答

2

SetWindowsHookEx要求線程ID,不處理ID。傳遞線程ID代替:

DWORD threadID = GetWindowThreadProcessId(hWindow, 0); 

if(!InstHook(threadID, (void *)(callIt))) {...} 
+0

好吧,我的錯誤我在想GetWindowThreadProcessId返回threadId到第二個參數(通過ref)。謝謝 – sgm