   Getting info from a control in another app
  Hi all. I'm trying to get the get text from a text box in an app I don't control. Is the possible and if it is whats the best way of doing it.Thanks!   
  Which OS?   ;  windows vista   ;  It's possible, but not pretty. In order to get text from a control, you can set a WM_GETTEXT message, with the LPARAM equal to a pointer to a buffer to copy the text into and the WPARAM the size of the buffer. However, if you're doing this across application boundaries, you'll need to allocate memory in the other process since you can't share addresses due to virtual memory protection. In order to allocate memory in another process you can use VirtualAllocEx(), then after sending the message you can read the memory with ReadProcessMemory(). You should match the VirtualAllocEx() call with a VirtualFreeEx() to free the memory. To get a handle to the process you can use OpenProcess() with the process id or start it with CreateProcess().   ;  First, you need to get the window handle of the control's window handle, then you can send it WM_GETTEXTLENGTH and WM_GETTEXT with SendMessage to get the length of the control's text and then create a suitably sized buffer to obtain the text.Its getting the control's window handle that can be problematic.  If the control is always created in a fixed hierarchy relative to the top-level window for the application, then you can use the EnumWindows to enumerate all top-level windows on the desktop and then drill down into the window hierarchy once you've got the appropriate top-level window.  The title bar text will be the result of WM_GETTEXT for a top-level window, so that's how people usually identify a top-level window.All of this might be more readily done in something like AutoIt instead of C++/C#.  It depends on what kind of solution you need.   ;Quote:Original post by SiCraneIt's possible, but not pretty. In order to get text from a control, you can set a WM_GETTEXT message, with the LPARAM equal to a pointer to a buffer to copy the text into and the WPARAM the size of the buffer. However, if you're doing this across application boundaries, you'll need to allocate memory in the other process since you can't share addresses due to virtual memory protection. In order to allocate memory in another process you can use VirtualAllocEx(), then after sending the message you can read the memory with ReadProcessMemory(). You should match the VirtualAllocEx() call with a VirtualFreeEx() to free the memory. To get a handle to the process you can use OpenProcess() with the process id or start it with CreateProcess().You don't need to allocate memory in the other process.  Just use SendMessage with WM_GETTEXTLENGTH and WM_GETTEXT and the appropriate HWND.  I just tried this:#include "stdafx.h"#include <windows.h>#include <vector>int _tmain(int argc, _TCHAR* argv[]){    HWND window = (HWND) 0xA00C6;    int length = SendMessage(window, WM_GETTEXTLENGTH, 0, 0);    std::vector<TCHAR> buffer(length+1);    LRESULT result = SendMessage(window, WM_GETTEXT, length + 1, LPARAM(&buffer[0]));    _tcprintf(_T("Text is '%s'\n"), &buffer[0]);    return 0;}and it worked fine (0xA00C6 is the window handle as reported by Spy++ for the IE window hosting this reply :-).   ;  Thanks for the help. I got the window handle to the control using RealChildWindowFromPoint().  Would InternalGetWindowText() be an easier way of doing this?   ;Quote:Original post by pbbm2000Thanks for the help. I got the window handle to the control using RealChildWindowFromPoint().  Would InternalGetWindowText() be an easier way of doing this?I generally stay away from anything that has "internal" in the name.Just use WM_GETTEXTLENGTH and WM_GETTEXT as I have shown.  Its the documented way of doing this.   ;  legalize, I tried implementing your solution but the SendMessage(window, WM_GETTEXTLENGTH, 0, 0); returns 0 evey time when there is text.  I know the window handle is valid because I called GetWindowInfo() and the dimensions and position match up.   ;Quote:Original post by pbbm2000legalize, I tried implementing your solution but the SendMessage(window, WM_GETTEXTLENGTH, 0, 0); returns 0 evey time when there is text.  I know the window handle is valid because I called GetWindowInfo() and the dimensions and position match up.Does Spy++ show nested windows?  Remember that you can have a child window whose position and size is the same as the parent and the child window can have the text, but the parent window won't.  You can also have two windows that are siblings and overlapping, you can have any number of situations.  Spy++ will show the window text itself in its display, so validate that you are getting the proper window handle as reported by Spy++.   