I would like to “read” information from a window not related to my program. If I have a process ID and a window handle:
Process Proc = Process.GetProcessById(ProcID);
IntPtr hdl = Proc.MainWindowHandle;
And I have information from spy++ telling me that the control-ID of the element I’m interested in is 00000003EA, how can I access it with C#?
Thanks for your help!
Edit_____________________________________
In case anyone is interested, this is how I got it working:
Process p = Process.GetProcessById(ProcID);
IntPtr hdl = p.MainWindowHandle;
byte[] buffer = new byte[1024]; //Assume that 1024 bytes are enough! Better would be to get the text length..
UTF8Encoding enc = new UTF8Encoding();
uint Test = GetDlgItemText((int)hdl, Convert.ToInt32("0x000003EA", 16), buffer, 1024);
string TextFromOtherWindow = enc.GetString(buffer);
[DllImport("user32.dll")]
public static extern uint GetDlgItemText(
int hDlg, //A handle to the dialog box that contains the control.
int nIDDlgItem, //The identifier of the control whose title or text is to be retrieved.
byte[] lpString, //The buffer to receive the title or text.
int nMaxCount //The maximum length, in characters, of the string to be copied to the
//buffer pointed to by lpString. If the length of the string, including
//the null character, exceeds the limit, the string is truncated.
);
byte[] buffer
is the buffer where the text from the other window is written back to. I assumed that the text is no more than 1024 bytes long, but it would be better to get the actual size…
As far as the encoding goes, a different one might be better suited for your needs.
The Handle in Hex needs to be converted to an integer: Convert.ToInt32("0x000003EA", 16)
GetDlgItemText was best suited (I think) for my requirement of getting the static text as opposed to “SendMessage” and “WM_GETTEXT”.
Thanks to all who helped point me in the right direction!
Source for GetDlgItemText: MSDN
Edit_________________________________
Hmmm. I spoke too soon... The element ID is changed each time the program is started. I have opened a new question at Persistent Element Identification.