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.

Community
  • 1
  • 1
Daro
  • 1,990
  • 2
  • 16
  • 22
  • 2
    Maybe this can help: http://stackoverflow.com/questions/352236/reading-from-a-text-field-in-another-applications-window – Patrick Apr 12 '12 at 17:59
  • Can I ask what process ? is it a .net Process ? – Micah Armantrout Apr 12 '12 at 18:03
  • That link is for C++, which could do the trick, but I have no idea what it means :-( It is the Installer from AutoCAD that I'm trying to wrap. – Daro Apr 12 '12 at 18:08
  • 1
    [SendMessage function](http://msdn.microsoft.com/en-us/library/windows/desktop/ms644950(v=vs.85).aspx) and [how to use Sendmessage in C#](http://social.msdn.microsoft.com/forums/en-US/winforms/thread/94e500c8-6d1b-43bf-9c04-9823597525bf) – L.B Apr 12 '12 at 18:18
  • 1
    @Daro: The problem is it's not possible to do in "pure C#", you'll have to use pinvoke (see pinvoke.net) to call some C++ code. – Patrick Apr 12 '12 at 18:38
  • So I need do do something like this? [DllImport("user32.dll")] public static extern int SendMessage( int hWnd, // handle to destination window uint Msg, // message long wParam, // first message parameter long lParam // second message parameter ); What Message do I need to send, and how do I access the specific element? – Daro Apr 12 '12 at 20:00

3 Answers3

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:)

Community
  • 1
  • 1
NSGaga-mostly-inactive
  • 14,052
  • 3
  • 41
  • 51
0

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

Sandeep
  • 7,156
  • 12
  • 45
  • 57
  • 1
    So how would the webservice read the text of the control? The OP stated that he doesn't have control over the source of the other process – Patrick Apr 12 '12 at 18:01
  • No. I have no control over the other program. – Daro Apr 12 '12 at 18:04
0

I would look into something like this Managed Spy ++

This might also help Find Window in C#

Community
  • 1
  • 1
Micah Armantrout
  • 6,781
  • 4
  • 40
  • 66