PostMessage () for emulating input in C ++?

I am trying to make a simple botting program for a game. I want it to be functional, even if the game is out of focus or minimized. Because of this, I cannot use SendInput () as it mimics global events. I realized that to do this, I have to use the PostMessage () function. I made a test program that simulates notepad input:

#include <Windows.h> HWND handle = FindWindow(NULL,CStringW("Untitled - Notepad")); HWND edit = FindWindowEx(handle, NULL, CStringW("Edit"), NULL); PostMessage(edit, WM_CHAR, 'a', 0 ); 

This example successfully simulates a β€œa” click in a notebook, even if the notebook is out of focus or minimized. I also had mouse events for work.

When I try to do the same for my game, I cannot publish click commands. After researching, I found that the original descriptor was received, but permission is rejected when FindWindowEx () is called, and no descriptor is returned.

Is there a way to get β€œediting” access to another process if it blocks this function?

+4
source share
2 answers

Of course, Windows never let you do this, what if you are trying to steal a user password? you are not allowed to receive input until you have focus!

But you can do some tricks here: you have to write a hook function for the mouse or keyboard, and this function should be implemented in a DLL (and not on your EXE). SetWindowsHookEx to install it, and then use the IPC mechanism in your DLL to send mouse and / or keyboard messages to your main EXE.

But apart from this: while he usually works with the line

 HWND handle = FindWindow(NULL,CStringW("Untitled - Notepad")); 

make no sense, windows should have a version of most functions that receive at least one string argument: one for ANSI, that its name is terminated with A and one for wide (UTF-16), which is terminated with W MSVC has an additional layer over this design called TCHAR and with define map all such functions can be used with ANSI or Wide, so if you use this version of the API, it is impractical to directly use CStringW , which generate wide strings. And since the Windows API works with char* and wchar_t* , why do you convert a string literal to a CString and then pass it to a function? You must use one of the following values:

 // This also work with CStringA HWND handleA = FindWindowA(NULL, "Untitled - Notepad"); // This also work with CStringW HWND handleW = FindWindowW(NULL, L"Untitled - Notepad"); // This also work with CString HWND handleT = FindWindow(NULL, _T("Untitled - Notepad") ); 
+1
source

Are you sure that the game you are trying to send text to has a Windows Edit control that can be listed from child elements of the client? It is possible that the Edit control you are referencing is an object of the client graphics container. In this case, you may need to send WM_KEYDOWN and WM_KEYUP to the client window to achieve the desired functionality.

Hope this helps.

0
source

Source: https://habr.com/ru/post/1444692/


All Articles