none
Get External Application Text RRS feed

  • Question

  • I need to get the text from a Application and Im traing to use this:

     <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)>
        Shared Function GetClassName(ByVal hWnd As IntPtr, ByVal lpClassName As StringBuilder, ByVal nMaxCount As Integer) As Integer
        End Function
    
        Declare Auto Function FindWindow Lib "user32.dll" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
    
    
        <DllImport("user32.dll", CharSet:=CharSet.Ansi, SetLastError:=True, ExactSpelling:=True)>
        Friend Shared Function EnumChildWindows(ByVal hWndParent As IntPtr, ByVal funcCallBack As funcCallBackChild, ByVal lParam As IntPtr) As Boolean
        End Function
    
        <DllImport("user32.dll", EntryPoint:="SendMessageTimeout", SetLastError:=True, CharSet:=CharSet.Auto)>
        Public Shared Function SendMessageTimeoutText(ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal countOfChars As Integer, ByVal text As StringBuilder, ByVal flags As UInteger, ByVal uTImeoutj As UInteger, ByVal result As UInteger) As UInteger
        End Function
    
        Public Delegate Function funcCallBackChild(ByVal hWnd As IntPtr, ByVal lParam As IntPtr) As Boolean
    
        Public Shared TextBoxStrings As ArrayList = New ArrayList()
    
       Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
            ParseWindowControls("BMC Remedy User - [Incidente (Modify)]")
    
            For Each a In TextBoxStrings
                Debug.Print(a)
            Next
        End Sub
    
        Public Shared Sub ParseWindowControls(ByVal WindowTitle As String)
            Dim hWnd As IntPtr = FindWindow(Nothing, WindowTitle)
            TextBoxStrings.Clear()
            ' Dim MyCallBack As funcCallBackChild = EnumChildWindowsProc()
            Dim MyCallBack As New funcCallBackChild(AddressOf EnumChildWindowsProc)
    
            EnumChildWindows(hWnd, MyCallBack, IntPtr.Zero)
        End Sub
    
    
        Private Shared Function EnumChildWindowsProc(ByVal hWndParent As IntPtr, ByVal lParam As IntPtr) As Boolean
            Dim buffer = New StringBuilder(256)
            Dim Retval As Long = GetClassName(hWndParent, buffer, buffer.Capacity)
    
            TextBoxStrings.Add(GetText(hWndParent))
            Return True
        End Function
    
        Private Shared Function GetText(ByVal hwnd As IntPtr) As String
            Dim text = New StringBuilder(1024)
            If SendMessageTimeoutText(hwnd, 13, 1024, text, 2, 1000, 0) <> 0 Then
                Return text.ToString()
            End If
    
            Return ""
        End Function

    Application :

    I want o get the fiel "Chave*+"

    Monday, February 19, 2018 10:33 AM

All replies

  • Check the windows hierarchy with Spy++ then use EnumChildWindows() or FindWindowEx() to get the final Edit handle (if it is a standard Edit control)

    Monday, February 19, 2018 10:48 AM
  • Can you show me how ?
    Monday, February 19, 2018 12:33 PM
  • For example, if I want to get the text in WordPad.

    I look the hierarchy with Spy++ :

    Then I can do from the class names I see :

    Dim hWndWordPad As IntPtr = FindWindow("WordPadClass", Nothing)
    Dim hWndEdit As IntPtr = FindWindowEx(hWndWordPad, IntPtr.Zero, "RICHEDIT50W", Nothing)
    Dim pText As IntPtr = Marshal.AllocHGlobal(255)
    Dim nNbChars As Integer = SendMessage(hWndEdit, WM_GETTEXT, 255, pText)
    Dim sStringEdit = Marshal.PtrToStringUni(pText)
    Marshal.FreeHGlobal(pText)

    with declarations :

    Public Const WM_GETTEXT As Integer = &HD
    
    <DllImport("User32.dll", SetLastError:=True, CharSet:=CharSet.Auto)>
    Public Shared Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
    End Function
    <DllImport("User32.dll", SetLastError:=True, CharSet:=CharSet.Auto)>
    Public Shared Function FindWindowEx(ByVal hWndParent As IntPtr, ByVal hWndChildAfter As IntPtr, ByVal lpszClass As String, ByVal lpszWindow As String) As IntPtr
    End Function    		
    <DllImport("User32.dll", EntryPoint:="SendMessageW", SetLastError:=True)>
    Public Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As Integer, ByVal lParam As IntPtr) As Integer
    End Function


    • Edited by Castorix31 Monday, February 19, 2018 5:13 PM
    Monday, February 19, 2018 1:22 PM
  • This ?

    and then this :

    Imports System.Runtime.InteropServices
    
    
    Public Class Form2
    
        Public Const WM_GETTEXT As Integer = &HD
    
        <DllImport("User32.dll", SetLastError:=True, CharSet:=CharSet.Auto)>
        Public Shared Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
        End Function
        <DllImport("User32.dll", SetLastError:=True, CharSet:=CharSet.Auto)>
        Public Shared Function FindWindowEx(ByVal hWndParent As IntPtr, ByVal hWndChildAfter As IntPtr, ByVal lpszClass As String, ByVal lpszWindow As String) As IntPtr
        End Function
        <DllImport("User32.dll", EntryPoint:="SendMessageW", SetLastError:=True)>
        Public Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As Integer, ByVal lParam As IntPtr) As Integer
        End Function
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim hWndWordPad As IntPtr = FindWindow("AfxWnd70", Nothing)
            Dim hWndEdit As IntPtr = FindWindowEx(hWndWordPad, IntPtr.Zero, "Cliente", Nothing)
            Dim pText As IntPtr = Marshal.AllocHGlobal(255)
            Dim nNbChars As Integer = SendMessage(hWndEdit, WM_GETTEXT, 255, pText)
            Dim sStringEdit = Marshal.PtrToStringUni(pText)
            Marshal.FreeHGlobal(pText)
        End Sub
    End Class

    Now how can I get the text ?, how I print in MsgBox ?

    Monday, February 19, 2018 5:03 PM
  • Seaerch this forum for 'GetWindowText' without the quotes and select most recent thread fur the results and you should find what you need.

    La vida loca

    Monday, February 19, 2018 11:22 PM
  • This ?

    and then this :


    No.

    Where is the hierarchy ?

    FindWindow is used to find the main window.

    FindWindowEx or EnumChildWindows are used to find child windows.

    And you put the same window in FindWindow and FindWindowEx



    • Edited by Castorix31 Tuesday, February 20, 2018 6:40 AM
    Tuesday, February 20, 2018 6:40 AM
  • I used Spy++ to get the hierarchy.

    And main window is 'ArFrame'

    So I use :

    Dim hWndWordPad As IntPtr = FindWindow("ArFrame", Nothing)
            Dim hWndEdit As IntPtr = FindWindowEx(hWndWordPad, IntPtr.Zero, "ArFrame", Nothing)
            Dim pText As IntPtr = Marshal.AllocHGlobal(255)
            Dim nNbChars As Integer = SendMessage(hWndEdit, WM_GETTEXT, 255, pText)
            Dim sStringEdit = Marshal.PtrToStringUni(pText)
            Marshal.FreeHGlobal(pText)
            MsgBox(sStringEdit)

    And get :

    Help :/

    Tuesday, February 20, 2018 11:36 AM
  • I don't see the Edit control with "TEST" in the tree (if it is an Edit control...)

    And again you have put the same window in FindWindow and FindWindowEx : there is no child with "ArFrame" class name, as you can see that the first child is "MDIClient".

    And from the complex hierarchy, you won't get it of course with just 1 level of FindWindowEx...


    • Edited by Castorix31 Tuesday, February 20, 2018 11:49 AM
    Tuesday, February 20, 2018 11:49 AM
  • Sorry i change de value "TEST" for "B991" and I fund on hierarchy:

    How I print this ?


    Tuesday, February 20, 2018 12:10 PM
  • From this tree you should get it with successive FindWindowEx calls (or maybe EnumChildWindows because there seem to be (from the other screencopy) 2 windows with "Incidente (Modify)" text, but if it is always in the first one, it is ok)

    To test with FindWindowEx, you find first (either by class or by text, Nothing in the other parameter)

    "MDIClient" class,

    then"Incidente (Modify)" text,

    then "AfxMDIFrame70" class,

    then "FormView" text (or AfxFrameOrView70 class if there is only 1 at this level)

    then "SysTabControl32" class

    then "Cliente" text

    then "RichEdit20A" class




    • Edited by Castorix31 Tuesday, February 20, 2018 12:36 PM
    Tuesday, February 20, 2018 12:35 PM
  • Can you help, I'm not getting how can I do that.
    Tuesday, February 20, 2018 3:02 PM
  • You have all the elements to do a test.

    The first windows :

    Dim hWndArFrame As IntPtr = FindWindow("ArFrame", Nothing)
    Dim hWndMDIClient As IntPtr = FindWindowEx(hWndArFrame, IntPtr.Zero, "MDIClient", Nothing)
    Dim hWndIncidente As IntPtr = FindWindowEx(hWndMDIClient, IntPtr.Zero, Nothing, "Incidente (Modify)")
      ' then "AfxMDIFrame70" class, and so on... (and you should test the validity of windows handles)



    • Edited by Castorix31 Tuesday, February 20, 2018 4:10 PM
    Tuesday, February 20, 2018 4:09 PM
  • Can you help, I'm not getting how can I do that.
    Did you bother reading my post and searching this forum? This qeustion has been asked many times and many threads have the code u need.

    La vida loca

    Tuesday, February 20, 2018 4:09 PM
  • I do my research on this forum and others and I dont get how to do the code, just how it works and what to do, I want more tam the field "chave" but after I do this field I will studie the code and make another code to get others fiels, so I'm asking help becouse I dont get how to do it :/
    Tuesday, February 20, 2018 5:06 PM
  • I do my research on this forum and others and I dont get how to do the code, just how it works and what to do, I want more tam the field "chave" but after I do this field I will studie the code and make another code to get others fiels, so I'm asking help becouse I dont get how to do it :/

    Well those threads typically have explanations in them. But never mind if you don't want to bother with them. Of course they would not explain in detail something a novice could understand probably. But if you read enough of them perhaps things will become clearer.

    La vida loca

    Tuesday, February 20, 2018 8:09 PM
  • I do my research on this forum and others and I dont get how to do the code, just how it works and what to do, I want more tam the field "chave" but after I do this field I will studie the code and make another code to get others fiels, so I'm asking help becouse I dont get how to do it :/

    You would probably benefit greatly by creating a test MFC MDI application that has a less complicated window hierarchy than the one you show above.  Such a test application would allow you to write and debug the code that implements the concepts and uses the functions that Castorix31 and Mr. Monkeyboy have suggested to search for and retrieve text from a child window. 

    When you have gained a good understanding of the principles and techniques you will then be able to apply that knowledge to writing code that can traverse a more complicated window hierarchy and achieve your objectives.  You will learn more from making mistakes and correcting them than by trying to understand code that has been written by someone else.


    • Edited by RLWA32 Tuesday, February 20, 2018 8:26 PM
    Tuesday, February 20, 2018 8:25 PM