简体   繁体   中英

obtaining text from textbox in not my window c#

I need to obtain some text which is displayed in text box in some other windows app window.

Anybody can tell me how can i do it usining c#?

It's a long shot, but I recall an app called " HawkEye " that enabled you to delve into a .NET app's control tree as long as the code wasn't obfuscated or something like this. The code appears to have gone open source so you never know what you might find useful/useless in there.

You can't do it natively - you'll have to dig into the Windows API.

Take a look at

You'll want to use Spy++ (or equiv.) to find the class name of the element you're looking for. Then you need to use PInvoke using a combination of the aforementioned functions.

Don't think that's possible, the windows API calls won't let you dig that far into another application. One idea I have is force a screen shot then use OCR on the resulting image, but this ranks high on the hacked solution meter. Is there something you're trying to accomplish underneath, like determine a particular process is done?

You will have to get the window handle to the application hosting the text box and then get a handle to the actual control that you want to get the text out of. Then you can send a WM_GETTEXT message to that control to read the value of the control. You will want to use some window spy application to get the details of the controls hosted in a window like the AutoIt Window Information Tool.

I was able to find an example off of Experts Exchange detailing how perform the above reading the detail of the edit control in notepad: http://www.experts-exchange.com/Programming/Languages/.NET/Visual_CSharp/Q_23748618.html (Scroll to the bottom for the answers).

private const int WM_GETTEXTLENGTH      = 0x000E;
private const int WM_GETTEXT        = 0x000D;

[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

[DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int msg, int wParam, StringBuilder lParam);

[DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int msg, int wParam, int lParam);

IntPtr notepad = FindWindow("notepad", null);
IntPtr editx = FindWindowEx(notepad, IntPtr.Zero, "edit", null);
int length = SendMessage(editx, WM_GETTEXTLENGTH, 0, 0);
StringBuilder text = new StringBuilder(length);
int hr = SendMessage(editx, WM_GETTEXT, length, text);
Console.WriteLine(text);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM