Getting/Setting text from another program.

How can I get/set text to/from another program's edit box?

E.G I type something into my browser's address bar then my program recieves it.

Or I type something into my program and and it puts that into the address bar.
closed account (GhqjLyTq)
Get a handle to the edit box, then use GetWindowText to get the text from it, or SetWindowText to set the text.
Example? ;D
From MSDN entry for GetWindowText

Copies the text of the specified window's title bar (if it has one) into a buffer. If the specified window is a control, the text of the control is copied. However, GetWindowText cannot retrieve the text of a control in another application.

Note the last sentence.

But the same entry does go on to remark

To retrieve the text of a control in another process, send a WM_GETTEXT message directly instead of calling GetWindowText.

Andy

P.S. If your interested in the why, see: "The secret life of GetWindowText"
http://blogs.msdn.com/b/oldnewthing/archive/2003/08/21/54675.aspx
Last edited on
Yes, you must send the WM_GETTEXT message. As I recall, you need to allocate the buffer in the external application using VirtualAllocEx(), then use ReadProcessMemory() to import the result into your own address space.
@webJose - I think the system handles the memory marshalling for you in the case of WM_GETTEXT and other standard messages.

From MSDN entry for SendMessage (ok, the logic is reversed, but...)

The system only does marshalling for system messages (those in the range 0 to (WM_USER-1)). To send other messages (those >= WM_USER) to another process, you must do custom marshalling.


Could I have an example? All this is confusing
This little applet finds the first notepad instance it can and dumps some of the text it's displaying.

Andy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// DumpNotepadText.cpp

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <iostream>
using namespace std;

void DumpNotepadText()
{
	cout << "[DumpNotepadText]" << endl;

	cout << "- find Notepad window" << endl;
	HWND hwndNotepad = FindWindow("Notepad", NULL);

	if(NULL != hwndNotepad)
	{
		cout << "- find Edit control window" << endl;
		HWND hwndEdit = FindWindowEx(hwndNotepad, NULL, "EDIT", NULL);

		if(NULL != hwndEdit)
		{
			cout << "- get text length" << endl;
			int textLen = (int)SendMessage(hwndEdit, WM_GETTEXTLENGTH, 0, 0);

			if(0 < textLen)
			{
				cout << "- get text (up to 1024 chars, inc term null)" << endl;
				const int bufferSize = 1024;
				char textBuffer[bufferSize] = "";
				SendMessage(hwndEdit, WM_GETTEXT, (WPARAM)bufferSize, (LPARAM)textBuffer);

				cout << "[begin text]" << endl;
				cout << textBuffer     << endl;
				cout << "[end text]"   << endl;
			}
			else
			{
				cerr << "? No text there! :-(" << endl;
			}
		}
		else
		{
			cerr << "? Huh? Can't find Edit control!?!" << endl;
		}
	}
	else
	{
		cerr << "? No notepads open! :-(" << endl;
	}
}

int main()
{
	DumpNotepadText();

	return 0;
}

Last edited on
closed account (DSLq5Di1)
I see, a fan of yoda syntax Andy, you are. Hmmmmmm. ~Yoda voice~
Hmmm, yes. When padowan I was, taught I was the yoda way.
closed account (Gz64jE8b)
What if there's more than one edit box?
Then you'll have to be more cunning with the searching.

Either search by name, or search for the n'th edit box (FindWindowEx's 2nd param is hwndChildAfter -- the search begins with the next child window in the Z order.)
Last edited on
closed account (Gz64jE8b)
Thanks, Andy. :)
Topic archived. No new replies allowed.