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, ¤tWindowGuiThreadInfo);
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, ¤tWindowGuiThreadInfo); //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;