Using SendMessage to enter text into another edit control related to another process

I want to adjust the text in the edit box programmatically in another program, which is a game application and uses directX, but works in window mode. I'm doing it:

HWND hWnd = FindWindow(NULL,"Game"); HWND edit = FindWindowEx(hWnd, NULL, "Edit", NULL); SendMessage(edit, WM_CHAR, (TCHAR) 'H', 0); SendMessage(edit, WM_CHAR, (TCHAR) 'E', 0); SendMessage(edit, WM_CHAR, (TCHAR) 'L', 0); SendMessage(edit, WM_CHAR, (TCHAR) 'L', 0); SendMessage(edit, WM_CHAR, (TCHAR) 'O', 0); 

this does not work for me .... but some of them somehow worked. I think I didn’t understand something, maybe he should focus on editing, then position the cursor, then paste the text, and then turn off the focus ... I really don't know much ... please save my time and nerves, tell me how to do it works ....

I tried this too and didn't work:

 SendMessage(hwnd, WM_SETTEXT, NULL, (LPARAM)"text"); 

hwnd is correct, but the text is not updated ... it sends the correct descriptor, but it is not updated .... I think I need to somehow focus or update as soon as it is updated, but I did not understand what happened ... so the code worked once, but no longer worked ... why did it work once?

+2
source share
2 answers

Your question is money! Seriously . Let's say a little bird told me what game you are interested in (starts with S ). I spent several hours on this problem, and I had some success, so I will share it with you.

There is a tool called Spy ++ (from Microsoft) that allows you to view messages sent to a window / class. This is great for debugging, because it allows you to control messages sent to the EDIT field when a key is pressed on the keyboard, so you can find out exactly which calls and parameters that are sent to the game to simulate this operation.

Use spy ++ to open the game process, and as soon as you enter the game login window, you will see that spy ++ reports several threads open in this process, but only one thread will have 3 EDIT blocks. This is the topic you are interested in!

Also note that none of the EDIT fields has a header, so the following code will never work:

 HWND edit = FindWindowEx(hWnd, NULL, "Edit", NULL); 

and by the way, always make sure FindWindowEx() returns something real, otherwise how do you know that it managed to find the edit field?

Instead, you should:

 HWND edit = FindWindowEx(hWnd, NULL, "", NULL); if (!edit) { // report error } 

And that will find the first EDIT block. This square corresponds to the username field. The game uses 3 PostMessage() calls to simulate a keystroke and not SendMessage() , as you tried:

 // "..." means you need to find out the other parameters PostMessage(edit, WM_KEYDOWN, ...); PostMessage(edit, WM_CHAR, ...); PostMessage(edit, WM_KEYUP, ...); 

Spy ++ will show that other options do not worry. You will probably spend some time figuring out how to create the last call parameter (because it's a mask).

I could not send the keys to the game if it was minimized or without focus. You will have to find out. For testing purposes, use SetForegroundWindow(window_hwnd); and a few other things to focus the window.

+4
source

Look for the source code for Autoit. Autoit can send keys / mouse to almost anything. When newer versions of Kaspersky Anti-Virus are installed, it interacts with the SendMessage|SendInput between processes.

Edit: For people who point out that Autoit is a huge piece of code that needs to be executed in order to accomplish this task:

Autoit can send input to background applications that do not want to see input. This is not an easy task. There are games that do not want to see any script input of any form, and they try to prevent this. In addition, in the wild, there are AV solutions that block input to the interprocess input process. In most cases, Autoit works around blocks.

+2
source

All Articles