【问题标题】:Local Hook not working本地挂钩不起作用
【发布时间】:2012-08-29 01:34:09
【问题描述】:

我有一个应用程序并希望监视 MSWord 按键(本地挂钩),但我不知道如何找到要使用的 pid!下面的代码适用于全局钩子 (pid = 0) 和 (pid = GetCurrentThreadId)。但不适用于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!";

我会非常非常感谢任何关于这个问题的...

注意:

  1. 我希望只对 MSWord 有一个钩子。

  2. 1234563安装鼠标钩!”.
  3. 我已经尝试过FindWindowFindWindowExGetForegroundWindowGetActiveWindow;由于这不起作用,我相信问题出在GetWindowThreadProcessId

【问题讨论】:

  • GetCurrentThreadId 只返回线程 ID,不返回进程 ID。您应该使用GetWindowThreadProcessId 来检索拥有指定窗口的进程ID。请注意,该窗口必须属于 MSWord 应用程序。还要检查该钩子 DLL 文档是否有任何限制。如果没有钩子 DLL 使用什么方法的详细信息,就无法猜测可能导致问题的原因。

标签: winapi hook setwindowshookex findwindow


【解决方案1】:

SetWindowsHookEx 需要线程 ID,而不是进程 ID。改为传递线程 ID:

DWORD threadID = GetWindowThreadProcessId(hWindow, 0);

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

【讨论】:

  • 好的,我的错误我在想 GetWindowThreadProcessId 将 threadId 返回到第二个参数(由 ref 传递)。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-02
  • 2019-02-28
  • 1970-01-01
  • 2015-11-22
  • 1970-01-01
相关资源
最近更新 更多