?
Solved

screen scrape contents of an emulator window

Posted on 2006-04-24
7
Medium Priority
?
487 Views
Last Modified: 2008-02-01
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
0
Comment
Question by:Autoeforms
  • 3
  • 2
6 Comments
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 total points
ID: 16527542
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...
0
 
LVL 8

Author Comment

by:Autoeforms
ID: 16527784
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
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16527951
GetWindowText only gets the window title.

Bob
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16528106
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.
0
 
LVL 8

Author Comment

by:Autoeforms
ID: 16528133
got that but using spy, the command window show only 1 main window
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16528448
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
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses
Course of the Month17 days, 9 hours left to enroll

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question