BunnyStrider

Hi guys,

I'm currently trying to come up for a solution for the following problem:
Right now the user is reading values off a screen and manually copying them into another window to store in a databse. Because of the closed nature of the program they are using I can't edit the program, and was trying to figure out how to automate this process.

Summary:
I want to read the displayed text values off the window of another program.

anyone know how to do this Or at leaste can anyone point me in the right direction for some learning sources or api classes that might be related

Thanks a bunch,



Re: Visual C# General How do I Retrieve text from other programs?

Mark Betz

The answer depends on whether the app you're scraping is displaying HTML (easiest), text in a console (fairly easy), or text in a graphical window (harder). There are libraries for the first two, but the third is a little more complicated.



Re: Visual C# General How do I Retrieve text from other programs?

BunnyStrider

It is text in a graphical window (harder) !=)

Yeah, I could do html, and maybe even figure out console, but I don't even know where to start for windows. Can you give me some place to start on for this Any kind of direction would help...I just don't even know where to look at this point.

thanks.





Re: Visual C# General How do I Retrieve text from other programs?

Mark Betz

Here's something I found that lines up with what I expected you would have to do for a graphical window: drill down to the correct window handle and then either send it a WM_GETTEXT message or find some other way. WM_GETTEXT will work if it is a standard Windows control.

>>

Use the tool Spy++ (that came with MSVC) for you to learn about window
hierarchies, and in particular the hierarchy relevant to the specific
other application you're going to be working with. You must figure
out, using Spy++, how you're going to "drill down" from the desktop
through successive child windows until you get to the text box.

Once you've figured out how, you'll have to code this "drilling"
yourself, using FindWindow/FindWindowEx/GetWindow.

If it's one of the standard text box controls, you'll be able to send
a WM_GETTEXT (equivalently, GetWindowText()) to it. (I think you can
at least... I think there aren't any cross-process problems with
getting the window text).

If it's a custom control that doesn't support this, then all bets are
off, and there's no general solution.





Re: Visual C# General How do I Retrieve text from other programs?

BunnyStrider

Ok thank you, I'll look into that.

One thing I'm in the dark is about who is holding app windows I tried to do a parent on my application and form but just got nulls. Is there like a main window class process running around that all apps are childs of that I can then access in my app to go up and then get the children Is that like how I would go about doing that And if so, what do I look for to acces that master parent control





Re: Visual C# General How do I Retrieve text from other programs?

Mark Betz

Actually I think I used to know the answer to this :). I believe the windows on the desktop are all children of the desktop window. But in any case FindWindow and FindWindowEx will locate a particular Window by title (the text in the title bar). Once you have that you can further parameterize calls to FindWindow to get down to the control that holds the text. As to which control that is, Spy++ will show you.



Re: Visual C# General How do I Retrieve text from other programs?

JJ Jordan - MSFT

Right, there is no parent of appliation windows, and therefore no real root window. Mark is correct that to search for these, you'll need to use FindWindow and FindWindowEx.

I'll additionally comment that for textboxes, you generally need to use the WM_GETTEXT method to get text out of them. Some will give the text values as a result from GetWindowText, but this is not typical. There are numerous examples that show how, but since I happened to do this not too long ago (not for MS), I'll share:

    [DllImport("user32.dll", EntryPoint="SendMessage")]
    static extern IntPtr SendMessage1(IntPtr hWnd, IntPtr Msg, IntPtr wParam, IntPtr lParam);

    [DllImport("user32.dll", EntryPoint = "SendMessageW", CharSet = CharSet.Unicode, SetLastError = true)]
    static extern IntPtr SendMessage2(IntPtr hWnd, IntPtr Msg, IntPtr wParam, [MarshalAs(UnmanagedType.LPWStr)] StringBuilder lParam);

    public static string GetCaption(IntPtr hwnd)
    {
      IntPtr WM_GETTEXT    = new IntPtr(0x000D);
      IntPtr WM_GETTEXTLENGTH = new IntPtr(0x000E);

      IntPtr size = SendMessage1(hwnd, WM_GETTEXTLENGTH, IntPtr.Zero, IntPtr.Zero);
      StringBuilder sb = new StringBuilder(size.ToInt32() + 1);
      SendMessage2(hwnd, WM_GETTEXT, new IntPtr(sb.Capacity), sb);

      return sb.ToString();
    }

Hope this helps,






Re: Visual C# General How do I Retrieve text from other programs?

Kernel Panic

If the other program is .NET, read this:

http://msdn.microsoft.com/msdnmag/issues/06/04/managedspy/


Have the source code :)




Re: Visual C# General How do I Retrieve text from other programs?

AOsama

Hello ,
I have used this code as is but I don't know where should the result "Text" be , but I didn't find it in the "sb.ToString()" returns nothing , I manage to get the "hwnd" value using "Spy++" then I give it to my application manually through a text box , but I still don't read the text from another application , any idea why , I would greatly appreciate any further help please.
Thanks for your time




Re: Visual C# General How do I Retrieve text from other programs?

AOsama

Hello ,
I used this code as it is , I also managed to get the "hwnd" value using "Spy++" and I add it to my application manually via a text box , so I'm sure of the "hwnd" value , but the code always returns "null" , there is no text in the "sb" object , any idea what could be the problem knowing that "Spy++" gets the text correctly .

Thanks for your time