screen scrape contents of an emulator window

I have been reading and trying different suggestions for hours.

I need to grab and process the data that is displayed in a terminal emulator window. My clients use different emulators but all are text based.

I am assuming that i will be able to identify the window handle. I have does this in testing and have tried to issue a select all, copy and the deselect and then
get the data from clipboard. nothing shows in the clipboard.

for testing i am using an open and running copy of notepad.

           caption = "notepad.exe"
            hWnd = FindWindow(vbNullString, Caption)
            ix = SendMessage(hWnd, EM_SETSEL, 0, -1) 'start selecting
            ix = SendMessage(hWnd, WM_COPY, 0, 0)
            ix = SendMessage(hWnd, EM_SETSEL, -1, 0) 'end selecting
            sTemp = Clipboard.GetDataObject.GetData(DataFormats.Text)
            MsgBox(sTemp)


the clipboard get does yield the last information in the clipboard, but not what i wanted to select out of note pad.

What am i missing?
Is there a better way?

thansk in advance.

greg
LVL 8
AutoeformsAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
Here is how to grab the text out of NotePad.  Notice that we have to get a handle to the "Edit" window based on the main NotePad handle window.  The edit window is a child window of the main window is the control that actually has the text in it:

    Private Const WM_GETTEXT As Integer = &HD
    Private Const WM_GETTEXTLENGTH As Integer = &HE

    Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" _
            (ByVal hWnd1 As IntPtr, ByVal hWnd2 As IntPtr, ByVal lpsz1 As String, _
            ByVal lpsz2 As String) As Integer

    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
        (ByVal hWnd As IntPtr, ByVal wMsg As Integer, _
        ByVal wParam As Integer, ByVal lParam As Integer) As Integer

    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
        (ByVal hWnd As IntPtr, ByVal wMsg As Integer, _
        ByVal wParam As Integer, ByVal lParam As System.Text.StringBuilder) As Integer

    Private Function GetWindowText(ByVal hWnd As IntPtr) As String
        Dim textLength As Integer = SendMessage(hWnd, WM_GETTEXTLENGTH, 0, 0) + 1
        Dim sb As New System.Text.StringBuilder(textLength)
        If textLength > 0 Then
            Call SendMessage(hWnd, WM_GETTEXT, textLength, sb)
        End If
        Return sb.ToString
    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim ps() As Process = Process.GetProcessesByName("notepad")
        If ps.Length > 0 Then
            Dim noteWnd As IntPtr = ps(0).MainWindowHandle
            Dim editWnd As Integer = FindWindowEx(noteWnd, IntPtr.Zero, "Edit", "")
            Dim sTemp As String = GetWindowText(editWnd)
            MsgBox(sTemp)
        End If
    End Sub



You may have to do a similar thing for your "terminal emulator window".  Use the Spy++ tool (or the free WinSpector...Google for it) to determine if your target app is using a child window as NotePad does.  You may not be able to use this technique to get the text out of your target app.  It all depends on the type of control used to display the text in it...

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AutoeformsAuthor Commented:
you have gotten me further then i got this morning, for the life of me i could not get it to work, i was only getting the window title.

i tried changing the code to a console window and only got the title back. any ideas on how to grab the contents of this

g
Bob LearnedCommented:
GetWindowText only gets the window title.

Bob
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
There is a GetWindowText() API:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/windows/windowreference/windowfunctions/getwindowtext.asp

So I probably should not have named my function that!

Look closely, ~my~ GetWindowText() function gets the text of whatever hWnd you pass in using the SendMessage() API with the WM_GETTEXT msg:

    Private Function GetWindowText(ByVal hWnd As IntPtr) As String
        Dim textLength As Integer = SendMessage(hWnd, WM_GETTEXTLENGTH, 0, 0) + 1
        Dim sb As New System.Text.StringBuilder(textLength)
        If textLength > 0 Then
            Call SendMessage(hWnd, WM_GETTEXT, textLength, sb)
        End If
        Return sb.ToString
    End Function

So if you pass in a Handle to the main window, then yes, you will get the windows title back.

*** BUT *** notice in my code, I am NOT passing in the main window handle.  I am gettting a handle to the child "edit" window in NotePad and passing that in instead:

     Dim noteWnd As IntPtr = ps(0).MainWindowHandle
     Dim editWnd As Integer = FindWindowEx(noteWnd, IntPtr.Zero, "Edit", "")
     Dim sTemp As String = GetWindowText(editWnd)

I am using the FindWindowEx() API to get a handle to the correct child "edit" window in NotePad.
AutoeformsAuthor Commented:
got that but using spy, the command window show only 1 main window
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
As I said earlier though...

"You may not be able to use this technique to get the text out of your target app.  It all depends on the type of control used to display the text in it..."

A command window is not using an Edit control as NotePad does!

I think for a command window you would need to use the ReadConsoleOutput() API as Bob himself demonstrates here:
http://www.experts-exchange.com/Programming/Programming_Languages/C_Sharp/Q_21817129.html
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.