I am attempting to make a simple botting program for a game. I want this to be functional even when the game is out of focus or minimized. Because of this, I cannot use SendInput() as it simulates global events. I figured out that, to make this work, I must use the PostMessage() function. I made a test program that simulates input in Notepad:

#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 the clicking of "a" in notepad even if notepad is out of focus or minimized. I similarly got mouse events to work as well.

When I try this same thing for my game, however, I am unable to Post the click commands. After investigation, I found that the original handle is obtained but permission is denied when I call FindWindowEx(), and no handle is returned.

Is there a way to obtain 'edit' access to another process if it blocks this function?

有帮助吗?

解决方案

Of course Windows never let you do that, what if you try to steal password of the user?? you are not allowed to receive input while you don't have the focus!!

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

But beside that: while it usually work the line

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

foes not make any sense, windows have to version of most of functions that get at least one string argument: one for ANSI that its name terminated with A and one for wide(UTF-16) characters that terminated with W. MSVC have an extra layer over this design called TCHAR and by using define map all such functions to eith ANSI or Wide, so if you are using that version of API it is not wise to directly use CStringW that generate wide strings. And since Windows API work with char* and wchar_t* why you convert your string literal to CString and then pass it to the function? you should use one of this:

// 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") );

其他提示

Are you sure the game you are trying to send text to owns a Windows Edit control which can be enumerated from the children of the client? It may very well be that the Edit control you are referring to is an entity of the Client's graphics container. If that's the case you might have to send WM_KEYDOWN and WM_KEYUP messages to the client window itself to achieve the desired functionality.

Hope that helps.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top