Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
377 views
in Technique[技术] by (71.8m points)

c++ - Using SendMessage to enter text into an edit control belonging to another process

I want to set up text in edit box programatically in other program, which is a game app and using directX, but running in window mode. I'm doing this:

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 not working for me.... but some how once worked. I think I misunderstood something, maybe it must focus on edit control, then set cursor then paste text and then disable focus... I realy don't know much...please save my time and nervs, tell me how to make it working....

I tried this too, and not working:

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

hwnd is correct, but text is not updated... it send to correct handle, but not updated.... I think I need somehow to focus or update, once it updated successful, but I didnt realized what happened...so code worked once, but no more working... why one time it worked?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

There's a tool named Spy++ (from Microsoft) that let's you watch messages that are sent to a window/class. This is great for debugging because it allows you to monitor the messages that are sent to the EDIT box when a key is pressed on your keyboard, so you can find out the exact calls and parameters that are send to the game to simulate this operation.

Use spy++ to open the game process, and once you are in Game's login window you will see that spy++ reports several threads opened in this process, but only one thread is going to have 3 EDIT boxes. That is the thread you are interested at!

Also notice that neither of the EDIT boxes have caption, so the following code will never work:

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

and by the way, always make sure that FindWindowEx() returns something valid, else how would you know that it succeeded finding the edit box?

What you should do instead is:

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

And this will find the first EDIT box. This box corresponds to the username box. The game uses 3 PostMessage() calls to simulate a key press, and not SendMessage() as you were trying:

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

Spy++ will reveal what the other parameters are, don't worry. You will probably spend some time figuring out how the last parameter of the call is built (because it's a mask).

I haven't been able to send keys to the game if it was minimized or without focus. You will have to figure this one out. For testing purposes, use can use SetForegroundWindow(window_hwnd); and some more stuff to focus the window.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...