Reading from a text field in another application’s window

For reading text content from another application’s text box you will need to get that text box control’s window handle somehow. Depending on how your application UI is designed (if it has a UI that is) there are a couple of different ways that you can use to get this handle. You might use “FindWindow”https://stackoverflow.com/”FindWindowEx” to locate your control or use “WindowFromPoint” if that makes sense. Either way, once you have the handle to the text control you can send a “WM_GETTEXT” message to it to retrieve its contents (assuming it is a standard text box control). Here’s a concocted sample (sans error checks):

HWND hwnd = (HWND)0x00310E3A;
char szBuf[2048];
LONG lResult;

lResult = SendMessage( hwnd, WM_GETTEXT, sizeof( szBuf ) / sizeof( szBuf[0] ), (LPARAM)szBuf );
printf( "Copied %d characters.  Contents: %s\n", lResult, szBuf );

I used “Spy++” to get the handle to a text box window that happened to be lying around.

As for protecting your own text boxes from being inspected like this, you could always sub-class your text box (see “SetWindowLong” with “GWL_WNDPROC” for the “nIndex” parameter) and do some special processing of the “WM_GETTEXT” message to ensure that only requests from the same process are serviced.

Leave a Comment