2012-08-29 35 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