-1

The problem is that I already have a working program that sends a click to an inactive window that is not on TOPLEVEL, but it does not work with all windows. And I cannot understand what is the reason for this behavior. When I filter messages with spy ++, they are exactly the same as with a real mouse click, but in the end the game does not respond.

Initially, I coded it in python, but in the end it didn't work out for me and I decided to try C ++, despite the fact that I have no experience. Here's what I managed to put together in C ++.

#include <stdio.h>
#include <cstdlib>
#include <windows.h>
#include <winuser.h>
#include <conio.h>

LPCTSTR WindowName = L"Raid: Shadow Legends";
HWND hMU = FindWindow(NULL, WindowName);
int main() {
    if (hMU)
    {
        int x = 16; //selected values ​​based on the readings of spy ++
        int y = 266; //
        WINDOWPOS wp = { hMU, NULL, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE };
        
        SendMessage(hMU, WM_SETCURSOR, (WPARAM)hMU, MAKELPARAM(HTCLIENT, WM_MOUSEMOVE));
        SendMessage(hMU, WM_MOUSEACTIVATE, (WPARAM)hMU, MAKELPARAM(HTCLIENT, WM_LBUTTONDOWN));
        SendMessage(hMU, WM_WINDOWPOSCHANGING, 0, (LPARAM)& wp);
        SendMessage(hMU, WM_NCPAINT, 1, 0);
        SendMessage(hMU, WM_WINDOWPOSCHANGED, 0, (LPARAM)& wp);
        SendMessage(hMU, WM_ACTIVATEAPP, 1, 0);
        SendMessage(hMU, WM_NCACTIVATE, 1, 0);
        SendMessage(hMU, WM_ACTIVATE, WA_CLICKACTIVE, 0);
        SendMessage(hMU, WM_SETFOCUS, 0, 0);
        SendMessage(hMU, WM_SETCURSOR, (WPARAM)hMU, MAKELPARAM(HTCLIENT, WM_LBUTTONDOWN));
        PostMessage(hMU, WM_LBUTTONDOWN, MK_LBUTTON, MAKELPARAM(x, y));
        PostMessage(hMU, WM_LBUTTONUP, MK_LBUTTON, MAKELPARAM(x, y));
        SendMessage(hMU, WM_CAPTURECHANGED, 0, 0);
    }
}

The code is probably redundant or even bad, but I don't pay attention to it. I followed the path of exact copying of messages on mouse click. Sorry for this. I will accept any help. Also a Python solution would be nice.

D3-one 6
  • 45
  • 5
  • Perhaps the game employs anti-cheat measures. – Sam Varshavchik Jul 21 '20 at 21:44
  • @Sam Varshavchik It is possible. Interestingly, this same game completely accepts my code with simulated keystrokes, but it does not work with simulated mouse clicks. – D3-one 6 Jul 21 '20 at 21:52
  • There is more to simulating input than just sending window messages (and FYI, some of those messages need to be *posted* with `PostMessage()`, not *sent* with `SendMessage()`). The correct way to simulate input is with `SendInput()`, but that won't work if the coordinates of the simulated input are covered up by another window. Does the game's window react to UIAutomation instead? – Remy Lebeau Jul 21 '20 at 22:19

1 Answers1

0

I recommend you to use SendInput to send the corresponding message to the game window.

You can refer to:How to simulate mouse click in a Directx game.

You can position the focus to the corresponding window by using AttachThreadInput, SetForegroundWindow SetActiveWindow and SetFocus.

Then send the specified message through SendInput.

Zeus
  • 3,703
  • 3
  • 7
  • 20
  • 1
    [AttachThreadInput is like taking two threads and pooling their money into a joint bank account, where both parties need to be present in order to withdraw any money](https://devblogs.microsoft.com/oldnewthing/20130619-00/?p=4043). [I warned you: The dangers of attaching input queues](https://devblogs.microsoft.com/oldnewthing/20080801-00/?p=21393). [Sharing an input queue takes what used to be asynchronous and makes it synchronous, like focus changes](https://devblogs.microsoft.com/oldnewthing/20130607-00/?p=4143). – IInspectable Jul 22 '20 at 05:41
  • yes, but I need the window to remain in the same position, that is, so that it does not appear in front – D3-one 6 Jul 22 '20 at 15:36
  • @D3-one6 I think the games uses DirectInput so you can't use SendMessage to send mouse events or keyboard events to it.So you can refer to https://stackoverflow.com/questions/24735029/directx-game-hook – Zeus Jul 23 '20 at 02:30