GetGuiThreadInfo不起作用

时间:2012-12-31 09:57:18

标签: winapi visual-c++

我正在尝试通过单击另一个窗口将鼠标输入发送到窗口。根据{{​​3}},我正在尝试找到发送事件的正确句柄。以下是我到目前为止的情况:

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;

在找到这个线程之前,我发送一个PostMessage到WindowFromPoint返回的句柄并使用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;

1 个答案:

答案 0 :(得分:1)

您正在将otherWindow传递给GetWindowThreadProcessId。但otherWindow尚未在此时初始化。我猜你打算通过hWnd

我想对GetWindowThreadProcessId的调用会返回一个0的线程ID,因为你传递的窗口不存在。这导致GetGUIThreadInfo失败。

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

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