【问题标题】:GetGuiThreadInfo does not workGetGuiThreadInfo 不起作用
【发布时间】:2012-12-31 09:57:18
【问题描述】:

我试图通过单击另一个窗口将鼠标输入发送到一个窗口。根据Simulating key press with PostMessage only works in some applications?,我正在尝试找到正确的句柄来发送事件。这是我目前所拥有的:

case WM_LBUTTONDOWN:
  GetCursorPos( reinterpret_cast<POINT *>( &mousePosition ) );  
  hWnd = WindowFromPoint(mousePosition);
  threadID = GetWindowThreadProcessId(otherWindow, &procID);

  GetGUIThreadInfo(threadID, &currentWindowGuiThreadInfo);

  otherWindow = currentWindowGuiThreadInfo.hwndFocus;

  ScreenToClient(hWnd, &mousePosition);
  ClientToScreen(otherWindow, &mousePosition);
  ScreenToClient(otherWindow, &mousePosition);
  dw = MAKELPARAM(mousePosition.x, mousePosition.y);
  PostMessage(otherWindow, WM_LBUTTONDOWN, MK_LBUTTON, dw);             

  break;

在找到这个线程之前,我正在向 WindowFromPoint 返回的句柄发送 PostMessage 并使用 Spy++ 我可以看到消息通过(但这种方法仅适用于某些窗口)。但是现在GetGuiThreadInfo返回错误码87(参数不正确)。我将 currentWindowGuiThreadInfo.cbSize 设置为 sizeof(GUITHREADINFO)。我哪里错了?请帮忙。我在 Windows 7 64 位和 Visual Studio 2010 上使用 Visual C++、win32。

非常感谢!

编辑

很抱歉没有清楚地回答。这是代码的更完整版本:

POINT mousePosition;
DWORD dw, procID, threadID;
HWND hWnd;
GUITHREADINFO currentWindowGuiThreadInfo;
currentWindowGuiThreadInfo.cbSize = sizeof(GUITHREADINFO);
INPUT Input={0};
HWND hw;
int e;
int xPos, yPos;
MSLLHOOKSTRUCT *mouseParameters = (MSLLHOOKSTRUCT*)lParam;
int a = 2;
if (nCode == HC_ACTION) {
    switch(wParam) {

        case WM_LBUTTONDOWN:

          GetCursorPos( reinterpret_cast<POINT *>( &mousePosition ) );
          hWnd = WindowFromPoint(mousePosition);

          threadID = GetWindowThreadProcessId(otherWindow, &procID); //otherWindow exists and I can see the proper threadID


          GetGUIThreadInfo(threadID, &currentWindowGuiThreadInfo); //currentWindowGuiThreadInfo returns null for all the handles. The cbSize is 48. But no error is returned. The return value is 1

          otherWindow= currentWindowGuiThreadInfo.hwndFocus;
          ScreenToClient(hWnd, &mousePosition);
          ClientToScreen(otherWindow, &mousePosition);
          ScreenToClient(otherWindow, &mousePosition);
          dw = MAKELPARAM(mousePosition.x, mousePosition.y);

          PostMessage(otherWindow, WM_LBUTTONDOWN, MK_LBUTTON, dw);
          break;

【问题讨论】:

  • 显示threadID的定义和currentWindowGuiThreadInfo的定义和设置。

标签: winapi visual-c++


【解决方案1】:

您将otherWindow 传递给GetWindowThreadProcessId。但此时otherWindow 尚未初始化。我猜你是想通过hWnd

我猜对GetWindowThreadProcessId 的调用返回的线程ID 为0,因为您传递给它的窗口不存在。这会导致GetGUIThreadInfo 失败。

另一个明显的失败向量是你没有正确设置currentWindowGuiThreadInfo.cbSize。您说您正在这样做,但由于您没有显示代码,我们只能相信您的话。

顺便说一句,我注意到您没有在对 Windows API 函数的任何调用中检查错误。我计算了该代码中的 8 个 API 调用,其中没有一个具有错误检查功能。你真的必须养成检查错误的习惯。

【讨论】:

  • 感谢您的回复! @David: otherWindow 实际上是事先定义的。我正在传递它。这是我初始化 currentWindowGuiThreadInfo.cbSize 的方式:
  • 好吧,我在您发布的代码中发现了所有错误。你说你设置了cbSize,但是你没有贴出代码。
  • 对评论感到抱歉!这是我在 StackOverflow 上的第一篇文章。因此,在用我的答案编辑帖子时,我不断收到“评论只能编辑 5 分钟”。这是我初始化 currentWindowGuiThreadInfo.cbSize: GUITHREADINFO currentWindowGuiThreadInfo; currentWindowGuiThreadInfo.cbSize = sizeof(GUITHREADINFO);在调试时,我可以看到 cbsize 为 48。返回的 threadID 是正确的线程 ID(我使用 Process Explorer 检查了它)。
  • 我回答了您提出的问题。期望我用不存在问题的代码来解码该评论是不公平的。你怎么能合理地期望我调试我看不到的代码?查看问题中的代码,然后是我的答案,然后是您的代码。
  • 我已经编辑了我的帖子,清楚地显示了我所做的!请看一看。
猜你喜欢
  • 1970-01-01
  • 2020-05-10
  • 2010-10-17
  • 2016-03-13
  • 1970-01-01
  • 1970-01-01
  • 2016-09-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多