atlr
atlr

Reputation: 23

GetGuiThreadInfo does not work

I am trying to send mouse inputs to a window by clicking on another window. As per Simulating key press with PostMessage only works in some applications?, I am trying to find the correct handle to send the event to. Here is what I have so far:

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;

Before, finding this thread I was sending a PostMessage to the handle returned by WindowFromPoint and using Spy++ I could see the messages going through (but this approach only worked for certain windows). But now, the GetGuiThreadInfo returns an error code 87 (The parameter is incorrect). I am setting the currentWindowGuiThreadInfo.cbSize to sizeof(GUITHREADINFO). Where am I going wrong? Please help.I am using Visual C++, win32 on Windows 7 64-bit and Visual Studio 2010.

Thanks a lot!

EDIT

I am sorry for not answering clearly. Here is a more complete version of the code:

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;

Upvotes: 1

Views: 1585

Answers (1)

David Heffernan
David Heffernan

Reputation: 613382

You are passing otherWindow to GetWindowThreadProcessId. But otherWindow has not been initialised at that point. I guess you meant to pass hWnd.

I guess the call to GetWindowThreadProcessId returns a thread ID of 0 since the window you passed it does not exist. And that leads to the failure in GetGUIThreadInfo.

The other obvious failure vector is that you fail to set currentWindowGuiThreadInfo.cbSize correctly. You say that you are doing that, but since you didn't show the code, we can only take your word for that.

As an aside, I note that you are not checking for errors in any of your calls to Windows API functions. I count 8 API calls in that code, not one of which has error checking. You really must get into the habit of checking for errors.

Upvotes: 1

Related Questions