2

I have a hotkey window application in C# and I want all the text from the focused window of other application on pressing hotkey like notepad, browser, command window(cmd), Turbo c++, Pascal etc.

So Is it possible?

If any one have idea please help me with code example.

I have attach screen shot. I want to read text from this window. On pressing hotkey I want to read text "This is my test text".

enter image description here

Ashish
  • 123
  • 2
  • 7
  • 1
    There's no universal solution, you'll need to handle each application on case-by-case basis. – Athari Feb 20 '14 at 11:25
  • This might be of some help to you. At least it will give you some direction http://stackoverflow.com/questions/235972/copy-and-modify-selected-text-in-different-application – samar Feb 20 '14 at 11:27
  • 1
    In addition, you may have to recursively drill down into the child windows to collect all of the text (result may be messy). – Jeff Feb 20 '14 at 11:30
  • Handle(IntPtr) I have already available I want to read text based on that handle. – Ashish Feb 20 '14 at 12:02
  • Take to the mind that windowless controls exists. Also text may be drawed. Also app may use subclassing or hooking. – Xearinox Feb 20 '14 at 12:27

3 Answers3

1

There is a GetWindowText() in user32 API, but if you need to get text from a control in another process, GetWindowText() won't work.

You have to use SendMessage() with WM_GETTEXT instead:

const UInt32 WM_GETTEXT = 0x000D;
const UInt32 WM_GETTEXTLENGTH = 0x000E;

[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, StringBuilder lParam);


static string GetWindowTextRaw(IntPtr hwnd)
{
    // Allocate string length 
    int length = (int)SendMessage(hwnd, WM_GETTEXTLENGTH, IntPtr.Zero, IntPtr.Zero);
    StringBuilder sb = new StringBuilder(length + 1);
    // Get window text
    SendMessage(hwnd, WM_GETTEXT, (IntPtr)sb.Capacity, sb);
    return sb.ToString();
}
thepirat000
  • 12,362
  • 4
  • 46
  • 72
  • 2
    Thanks for answer. I have already tried this one. It will work fine for notepad, browsers and many applications but it won't work in command prompt(cmd), Turbo c++, pascal and any dos based screen. – Ashish Feb 20 '14 at 13:45
  • I don't think there is a generic way to do that. By the way why do you need to do that? – thepirat000 Feb 20 '14 at 14:21
  • I want go get that text on pressing Hotkey. – Ashish Feb 21 '14 at 06:10
  • Why don't you use the clipboard hotkeys CTRL+A, CTRL+C, and then paste it with CTRL+V ? – thepirat000 Feb 21 '14 at 12:15
  • I have already used it but it will not work in Turbo C++, Command Prompt(cmd) and all dos based screen. – Ashish Feb 21 '14 at 12:46
1

Application that call themselves "Screen Reader" (for visually impaired people) do that kind of things, sort of.

They use the old MSSA (Microsoft Active Accessibility) APIs and/or the new UIAutomation APIs.

With the two APIs, if you have a "Main Window" HWND, you can then browse the tree of the componants making the app. You can then retrieve properties, such as "Text" or "Name" and so on.

If the application doesn't support Accessive technologies, you fall back on case by case solutions, which means eventually awful hacks (as APIs hooking) or more regular methods (as DLL injection and use of the JNI Invocation API in the JAVA case).

manuell
  • 7,528
  • 5
  • 31
  • 58
  • @Ashish code example of what? Did you used Google or searched on Stackoverflow? – manuell Feb 21 '14 at 13:13
  • I have already searched in Google and Stackoverflow But I'm not able to find then after I add this in stackoverflow. – Ashish Feb 21 '14 at 13:23
  • google gives many results when you search for UIAutomation sample. What query do you use, with no result??? – manuell Feb 21 '14 at 13:32
  • I also got many results those are only interact with notepad and window based applications but not interact with Turbo c++ and command prompt(cmd). – Ashish Feb 21 '14 at 13:39
  • My problem is that I want to fetch text from the dos base window like Turbo c++ and command prompt(cmd). – Ashish Feb 21 '14 at 13:41
  • If you are searching for an universal solution which will work for every type of application, just give up, there is not such thing. That's the first comment to your question. – manuell Feb 21 '14 at 14:09
-2

Its is not directly possible through C#,

Still microsoft provides with WMI services which can utilized to get at max information on the machine and processes. Kindly check MSDN

You can download WMI tool from here and possible check Win32 classes and methods, you may find useful information for your requirement

Keshavdas M
  • 674
  • 2
  • 7
  • 25