Accessing Elements from Other Processes

1

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.

c#
interprocess
asked on Stack Overflow Apr 12, 2012 by Daro • edited May 23, 2017 by Community

3 Answers

1

Your best bet is going with the UI Automation.

Though that's not perfect as many applications do not support that.

Also take a look at this answer of mine, similar question, might help with being able to access / 'attach' to other process threads / queues etc.

EDIT: (I forgot to link up on my other post, just corrected:)

answered on Stack Overflow Apr 12, 2012 by NSGaga-mostly-inactive • edited May 23, 2017 by Community
0

WCF, Webservices are there to make inter-process communication easy.

answered on Stack Overflow Apr 12, 2012 by Sandeep
0

I would look into something like this Managed Spy ++

This might also help Find Window in C#

answered on Stack Overflow Apr 12, 2012 by Micah Armantrout • edited May 23, 2017 by Community

User contributions licensed under CC BY-SA 3.0