Getting UI text from an external application in C #

Is it possible to get UI text from an external application in C #.

In particular, is there a way to read Unicode text from a shortcut (I assume this is a regular Windows label control) from an external Win32 application written by a third party? The text is visible, but not selected by the mouse in the user interface.

I assume that there is some accessibility API (for example, intended for reading from the screen) which allows it.

Edit: You are currently learning something like the Managed Spy App , but still appreciate any other findings.

+4
source share
3 answers

You can do this if this Unicode text is actually a text box by sending a WM_GETTEXT message.

[DllImport("user32.dll")] public static extern int SendMessage (IntPtr hWnd, int msg, int Param, System.Text.StringBuilder text); System.Text.StringBuilder text = new System.Text.StringBuilder(255) ; // or length from call with GETTEXTLENGTH int RetVal = Win32.SendMessage( hWnd , WM_GETTEXT, text.Capacity, text); 

If it's just painted on canvas, you might be lucky if you know what infrastructure the application uses. If he uses WinForms or Borland VCL, you can use this knowledge to access the text.

+5
source

If you just need a standard Win32 label, then WM_GETTEXT will work just fine, as indicated in other answers.

-

There is an accessibility API - UIAutomation - for standard tags, it also uses WM_GETTEXT behind the scenes. One of the advantages of this is that it can receive text from several other types of controls, including most system controls, and often UI using non-system controls, including WPF, text in IE and Firefox, and others.

 // compile as: // csc file.cs /r:UIAutomationClient.dll /r:UIAutomationTypes.dll /r:WindowsBase.dll using System.Windows.Automation; using System.Windows.Forms; using System; class Test { public static void Main() { // Get element under pointer. You can also get an AutomationElement from a // HWND handle, or by navigating the UI tree. System.Drawing.Point pt = Cursor.Position; AutomationElement el = AutomationElement.FromPoint(new System.Windows.Point(pt.X, pt.Y)); // Prints its name - often the context, but would be corresponding label text for editable controls. Can also get the type of control, location, and other properties. Console.WriteLine( el.Current.Name ); } } 
+5
source

did not see the value for wm_gettext or wm_gettextlength in this article, so just in case ..

 const int WM_GETTEXT = 0x0D; const int WM_GETTEXTLENGTH = 0x0E; 
+2
source

Source: https://habr.com/ru/post/1276384/


All Articles