1

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;
Community
  • 1
  • 1
atlr
  • 23
  • 4
  • 1
    Show the definition of `threadID` and the definition and setup of `currentWindowGuiThreadInfo`. – Andrew Dec 31 '12 at 10:02

1 Answers1

1

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.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • Thanks for the replies! @David: the otherWindow is actually defined before hand. I am passing it in. And this the way I have initialized currentWindowGuiThreadInfo.cbSize: – atlr Dec 31 '12 at 21:43
  • Well, I found all the errors in the code you posted. You say you set cbSize, but you didn't post the code. – David Heffernan Dec 31 '12 at 21:49
  • I am sorry about the comment! This is my first post on StackOverflow. So, while editing the post with my answer I kept on getting "Comments may only be edited for 5 minutes". This the way I have initialized currentWindowGuiThreadInfo.cbSize: GUITHREADINFO currentWindowGuiThreadInfo; currentWindowGuiThreadInfo.cbSize = sizeof(GUITHREADINFO); While debugging I can see the cbsize to be 48. The threadID being returned is the proper thread ID (I checked it using Process Explorer). – atlr Dec 31 '12 at 22:20
  • I answered the question that you asked. It's not fair to expect me to decode that comment with code that's not in the question. How can you reasonably expect me to debug code that I cannot see? Look at the code in the question, and then my answer, and then your code. – David Heffernan Dec 31 '12 at 22:36
  • I have edited my post which shows clearly what I've done! Please take a look. – atlr Dec 31 '12 at 22:49
  • You don't assign to otherWindow. You also don't check errors correctly. Check the return value before calling GetLastError. I'm disappointed that you posted fake code. – David Heffernan Dec 31 '12 at 22:51
  • I am sorry again. But the code I posted was not fake. I forgot to change the name of a variable (I renamed anotherTempWindow to otherWindow for clarity but forgot to change it in one place). The return value for GetGUIThreadInfo(threadID, &currentWindowGuiThreadInfo) is 1 but the handles are null. – atlr Dec 31 '12 at 23:13
  • Another thing, In my original post I say that GetGUIThreadInfo returns 87 which again was a mistake on my part. It was returning an error earlier. But I fixed that, now although it doesn't return an error it is not setting the proper values. – atlr Dec 31 '12 at 23:15
  • It's fake if your code is different from the code in the question. Do you disagree that I answered the question that you originally asked? Look at it from my perspective. – David Heffernan Dec 31 '12 at 23:23
  • I completely understand that I messed up while posting my question and the subsequent stuff. And I am really thankful to you for taking the time out to answer my questions and yes, you did answer the question I asked originally. I apologize for the inconvenience but please help me. I am stuck. – atlr Dec 31 '12 at 23:33
  • I'm not sure how we can rescue this question. You need to post a 10 function that demonstrates the problem. It must compile. Proper error checking is essential. – David Heffernan Dec 31 '12 at 23:49
  • I got it working. Actually, I went through an earlier post of yours where you suggest using WM_SETFOCUS before calling GetWindowThreadProcessId. That worked for me. Thanks! – atlr Jan 01 '13 at 23:39