locked
Get Text From Specific Textboxes From External Application - Visual Basic .Net RRS feed

  • Question

  • I can get text from external application text box but now I want to get text from my desired text box from external application. See Image Below, I want to get text from 2nd text box.

    The Below Code Return The First Text Box Value Only.

    Imports System.Runtime.InteropServices
    
    
    Public Class Form1
    
    Private Const WM_GETTEXT As Integer = &HD
    Declare Auto Function SendMessage Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal msg As Integer, _
    ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
    <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
    Private Shared Function FindWindowEx(ByVal parentHandle As IntPtr, _
                                     ByVal childAfter As IntPtr, _
                                     ByVal lclassName As String, _
                                     ByVal windowTitle As String) As IntPtr
    End Function
    
    Declare Auto Function FindWindow Lib "user32.dll" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
    
    
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'Find the running notepad window
        Dim Hwnd As IntPtr = FindWindow(Nothing, TextBox1.Text) 'Textbox1.text = my application or window name / title.
    
        'Alloc memory for the buffer that recieves the text
        Dim Handle As IntPtr = Marshal.AllocHGlobal(100)
    
        'send WM_GWTTEXT message to the notepad window
        Dim NumText As Integer = SendMessage(Hwnd, WM_GETTEXT, 50, Handle)
    
        'copy the characters from the unmanaged memory to a managed string
        Dim Text As String = Marshal.PtrToStringUni(Handle)
    
        'Display the string using a label
        Label1.Text = Text
    
        'Find the Edit control of the Running Notepad
        Dim ChildHandle As IntPtr = FindWindowEx(Hwnd, IntPtr.Zero, "Edit", Nothing)
    
        'Alloc memory for the buffer that recieves the text
        Dim Hndl As IntPtr = Marshal.AllocHGlobal(200)
    
        'Send The WM_GETTEXT Message
        NumText = SendMessage(ChildHandle, WM_GETTEXT, 200, Hndl)
    
        'copy the characters from the unmanaged memory to a managed string
        Text = Marshal.PtrToStringUni(Hndl)
    
        'Display the string using a label
        Label2.Text = Text
    
    
    End Sub
    
    End Class

    I have asked this same question on an other site but got no response, that's why I am posting the same question on MSDN.

    Sunday, December 30, 2012 1:52 PM

Answers

  • I altered the application I posted earlier to parse out the pagers received phnr and received text using a test application named "Pager test application".

    First the application gets all data for every top level window. With that data in a string I use regex to parse out the data relevent to Pager test application only to a match collection.

    Then the application retrieves just the Pager test application phnr line and text line from the match collection via index number (which appears to be the same whenever the Pager test application is launched).

    Once the Pager test application phnr line and text line are retrieved another match collection is used to retrieve just the SMS phnr and SMS text.

    Also there are some regex replaces used to make it simpler for searching with regex as well as cleaning up the final text (removing slantbars / ).

    You can use the following code to test with and once you verify that you can retrieve the data you want continually then you can add the code to your own program to display the SMS Phnr and SMS Text in your TextBoxes.

    Anyhow the code follows as well as an image of the application at work. You may need to add some try/catch statements to it in order to keep regex from bombing out if no matches are found for example if you run the code and your pager application is not running at the same time.

    Imports System.Collections.Generic
    Imports System.Runtime.InteropServices
    Imports System.Text
    Imports System.Text.RegularExpressions
    
    
    Public Class Form1
    
        Private Const WM_GETTEXT As Integer = &HD
    
        Declare Auto Function SendMessage Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
    
        <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
        Private Shared Function FindWindowEx(ByVal parentHandle As IntPtr, _
                                         ByVal childAfter As IntPtr, _
                                         ByVal lclassName As String, _
                                         ByVal windowTitle As String) As IntPtr
        End Function
    
        Declare Auto Function FindWindow Lib "user32.dll" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
    
        Dim PagerInformation As String = ""
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            PagerInformation = ""
            RichTextBox1.Text = ""
            Dim enumerator As New WindowsEnumerator()
            For Each top As ApiWindow In enumerator.GetTopLevelWindows()
                For Each child As ApiWindow In enumerator.GetChildWindows(top.hWnd)
                    PagerInformation += top.MainWindowTitle & "    " & child.ClassName & "    " & child.hWnd.ToString & "    " & child.MainWindowTitle & "////" & vbCrLf & vbCrLf
                Next child
            Next top
            RichTextBox1.Text = PagerInformation
            '.....................................................................Name of application goes here followed by (.*?)////
            Dim PagersData As MatchCollection = Regex.Matches(PagerInformation, "Pager Test Application(.*?)////", RegexOptions.IgnoreCase)
            
    
            'You will probably have more matches for your pager application because it has more controls on it I believe.
            'You just need to determine which index match the pager text and pager phnr are at. Whenever I start my pager test application
            'the data for controls, etc, always appears in the same order so when it is matched pager text and phnr are always
            'at the same index.
    
            Dim PagerText As String = PagersData.Item(4).ToString & vbCrLf
            Dim PagerPhnr As String = PagersData.Item(7).ToString & vbCrLf
    
            PagerText = Regex.Replace(PagerText, "    ", "/")
            PagerPhnr = Regex.Replace(PagerPhnr, "    ", "/")
    
            RichTextBox2.AppendText(PagerText & vbCrLf & PagerPhnr)
    
            Dim Text As MatchCollection = Regex.Matches(PagerText, "/(.*?)/", RegexOptions.IgnoreCase)
            Dim Phnr As MatchCollection = Regex.Matches(PagerPhnr, "/(.*?)/", RegexOptions.IgnoreCase)
    
            Dim SMSText As String = Text.Item(1).ToString
            Dim SMSPhnr As String = Phnr.Item(1).ToString
    
            SMSText = Regex.Replace(SMSText, "/", "")
            SMSPhnr = Regex.Replace(SMSPhnr, "/", "")
    
            RichTextBox3.AppendText(SMSText & vbCrLf & SMSPhnr)
    
            TextBox1.Text = SMSPhnr
            TextBox2.Text = SMSText
    
        End Sub
    
        Private Sub RichTextBox1_TextChanged(sender As Object, e As EventArgs) Handles RichTextBox1.TextChanged
    
        End Sub
    
        Private Sub RichTextBox2_TextChanged(sender As Object, e As EventArgs) Handles RichTextBox2.TextChanged
    
        End Sub
    
        Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
    
        End Sub
    
        Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged
    
        End Sub
    
        Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
    
        End Sub
    
        Private Sub RichTextBox3_TextChanged(sender As Object, e As EventArgs) Handles RichTextBox3.TextChanged
    
        End Sub
    
        Private Sub Label3_Click(sender As Object, e As EventArgs) Handles Label3.Click
    
        End Sub
    
        Private Sub Label4_Click(sender As Object, e As EventArgs) Handles Label4.Click
    
        End Sub
    End Class
    
    
    Public Class ApiWindow
        Public MainWindowTitle As String = ""
        Public ClassName As String = ""
        Public hWnd As Int32
    End Class
    
    ''' <summary> 
    ''' Enumerate top-level and child windows 
    ''' </summary> 
    ''' <example> 
    ''' Dim enumerator As New WindowsEnumerator()
    ''' For Each top As ApiWindow in enumerator.GetTopLevelWindows()
    '''    Console.WriteLine(top.MainWindowTitle)
    '''    For Each child As ApiWindow child in enumerator.GetChildWindows(top.hWnd) 
    '''        Console.WriteLine(" " + child.MainWindowTitle)
    '''    Next child
    ''' Next top
    ''' </example> 
    Public Class WindowsEnumerator
    
        Private Delegate Function EnumCallBackDelegate(ByVal hwnd As Integer, ByVal lParam As Integer) As Integer
    
        ' Top-level windows.
        Private Declare Function EnumWindows Lib "user32" _
         (ByVal lpEnumFunc As EnumCallBackDelegate, ByVal lParam As Integer) As Integer
    
        ' Child windows.
        Private Declare Function EnumChildWindows Lib "user32" _
         (ByVal hWndParent As Integer, ByVal lpEnumFunc As EnumCallBackDelegate, ByVal lParam As Integer) As Integer
    
        ' Get the window class.
        Private Declare Function GetClassName _
         Lib "user32" Alias "GetClassNameA" _
         (ByVal hwnd As Integer, ByVal lpClassName As StringBuilder, ByVal nMaxCount As Integer) As Integer
    
        ' Test if the window is visible--only get visible ones.
        Private Declare Function IsWindowVisible Lib "user32" _
         (ByVal hwnd As Integer) As Integer
    
        ' Test if the window's parent--only get the one's without parents.
        Private Declare Function GetParent Lib "user32" _
         (ByVal hwnd As Integer) As Integer
    
        ' Get window text length signature.
        Private Declare Function SendMessage _
         Lib "user32" Alias "SendMessageA" _
         (ByVal hwnd As Int32, ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As Int32) As Int32
    
        ' Get window text signature.
        Private Declare Function SendMessage _
         Lib "user32" Alias "SendMessageA" _
         (ByVal hwnd As Int32, ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As StringBuilder) As Int32
    
        Private _listChildren As New List(Of ApiWindow)
        Private _listTopLevel As New List(Of ApiWindow)
    
        Private _topLevelClass As String = ""
        Private _childClass As String = ""
    
        ''' <summary>
        ''' Get all top-level window information
        ''' </summary>
        ''' <returns>List of window information objects</returns>
        Public Overloads Function GetTopLevelWindows() As List(Of ApiWindow)
    
            EnumWindows(AddressOf EnumWindowProc, &H0)
    
            Return _listTopLevel
    
        End Function
    
        Public Overloads Function GetTopLevelWindows(ByVal className As String) As List(Of ApiWindow)
    
            _topLevelClass = className
    
            Return Me.GetTopLevelWindows()
    
        End Function
    
        ''' <summary>
        ''' Get all child windows for the specific windows handle (hwnd).
        ''' </summary>
        ''' <returns>List of child windows for parent window</returns>
        Public Overloads Function GetChildWindows(ByVal hwnd As Int32) As List(Of ApiWindow)
    
            ' Clear the window list.
            _listChildren = New List(Of ApiWindow)
    
            ' Start the enumeration process.
            EnumChildWindows(hwnd, AddressOf EnumChildWindowProc, &H0)
    
            ' Return the children list when the process is completed.
            Return _listChildren
    
        End Function
    
        Public Overloads Function GetChildWindows(ByVal hwnd As Int32, ByVal childClass As String) As List(Of ApiWindow)
    
            ' Set the search
            _childClass = childClass
    
            Return Me.GetChildWindows(hwnd)
    
        End Function
    
        ''' <summary>
        ''' Callback function that does the work of enumerating top-level windows.
        ''' </summary>
        ''' <param name="hwnd">Discovered Window handle</param>
        ''' <returns>1=keep going, 0=stop</returns>
        Private Function EnumWindowProc(ByVal hwnd As Int32, ByVal lParam As Int32) As Int32
    
            ' Eliminate windows that are not top-level.
            If GetParent(hwnd) = 0 AndAlso CBool(IsWindowVisible(hwnd)) Then
    
                ' Get the window title / class name.
                Dim window As ApiWindow = GetWindowIdentification(hwnd)
    
                ' Match the class name if searching for a specific window class.
                If _topLevelClass.Length = 0 OrElse window.ClassName.ToLower() = _topLevelClass.ToLower() Then
                    _listTopLevel.Add(window)
                End If
    
            End If
    
            ' To continue enumeration, return True (1), and to stop enumeration 
            ' return False (0).
            ' When 1 is returned, enumeration continues until there are no 
            ' more windows left.
    
            Return 1
    
        End Function
    
        ''' <summary>
        ''' Callback function that does the work of enumerating child windows.
        ''' </summary>
        ''' <param name="hwnd">Discovered Window handle</param>
        ''' <returns>1=keep going, 0=stop</returns>
        Private Function EnumChildWindowProc(ByVal hwnd As Int32, ByVal lParam As Int32) As Int32
    
            Dim window As ApiWindow = GetWindowIdentification(hwnd)
    
            ' Attempt to match the child class, if one was specified, otherwise
            ' enumerate all the child windows.
            If _childClass.Length = 0 OrElse window.ClassName.ToLower() = _childClass.ToLower() Then
                _listChildren.Add(window)
            End If
    
            Return 1
    
        End Function
    
        ''' <summary>
        ''' Build the ApiWindow object to hold information about the Window object.
        ''' </summary>
        Private Function GetWindowIdentification(ByVal hwnd As Integer) As ApiWindow
    
            Const WM_GETTEXT As Int32 = &HD
            Const WM_GETTEXTLENGTH As Int32 = &HE
    
            Dim window As New ApiWindow()
    
            Dim title As New StringBuilder()
    
            ' Get the size of the string required to hold the window title.
            Dim size As Int32 = SendMessage(hwnd, WM_GETTEXTLENGTH, 0, 0)
    
            ' If the return is 0, there is no title.
            If size > 0 Then
                title = New StringBuilder(size + 1)
    
                SendMessage(hwnd, WM_GETTEXT, title.Capacity, title)
            End If
    
            ' Get the class name for the window.
            Dim classBuilder As New StringBuilder(64)
            GetClassName(hwnd, classBuilder, 64)
    
            ' Set the properties for the ApiWindow object.
            window.ClassName = classBuilder.ToString()
            window.MainWindowTitle = title.ToString()
            window.hWnd = hwnd
    
            Return window
    
        End Function
    
    End Class


    You've taught me everything I know but not everything you know.



    Monday, December 31, 2012 8:04 PM

All replies

  • Use the same method and specify the classname for the desired textbox. Based on the screenshot you posted, the textbox is reside in groupbox control, so it means that you have to get handle to that groupbox before the textbox. Try to use Spy++ tool that came with VS to get each control class name.

    kaymaf


    CODE CONVERTER SITE

    http://www.carlosag.net/Tools/CodeTranslator/.

    http://www.developerfusion.com/tools/convert/csharp-to-vb/.

    Sunday, December 30, 2012 4:58 PM
  • This will give you running applications main window title of the application, child class name, child hWnd and child main window title. You can click on the main window title in Listview column 1 to display the associated childs main window title (in your case TextBox text) text in the TextBox of the Form.

    The Form has one Button, one TextBox (on top) and one ListView (on the bottom).

    Hope this helpls.

    Imports System.Collections.Generic
    Imports System.Runtime.InteropServices
    Imports System.Text
    
    Public Class Form1
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            ListView1.AllowColumnReorder = True
            ListView1.GridLines = True
            ListView1.View = View.Details
            ListView1.Sorting = SortOrder.Ascending
    
            ListView1.Columns.Add("Main Window Title", 300, HorizontalAlignment.Left)
            ListView1.Columns.Add("Child Class Name", 300, HorizontalAlignment.Left)
            ListView1.Columns.Add("Child hWnd", 200, HorizontalAlignment.Left)
            ListView1.Columns.Add("Child Main Window Title", 800, HorizontalAlignment.Left)
        End Sub
    
        Private Sub ListView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListView1.SelectedIndexChanged
    
        End Sub
    
        Private Sub ListView1_Click(sender As Object, e As EventArgs) Handles ListView1.Click
            TextBox1.Text = ListView1.FocusedItem.SubItems.Item(3).Text.ToString
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            ListView1.Items.Clear()
            Dim enumerator As New WindowsEnumerator()
            For Each top As ApiWindow In enumerator.GetTopLevelWindows()
                For Each child As ApiWindow In enumerator.GetChildWindows(top.hWnd)
                    Dim item1 As New ListViewItem(top.MainWindowTitle)
                    item1.SubItems.Add(child.ClassName)
                    item1.SubItems.Add(child.hWnd.ToString)
                    item1.SubItems.Add(child.MainWindowTitle)
                    ListView1.Items.AddRange(New ListViewItem() {item1})
                Next child
            Next top
        End Sub
    
        Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
    
        End Sub
    
    End Class
    
    
    Public Class ApiWindow
        Public MainWindowTitle As String = ""
        Public ClassName As String = ""
        Public hWnd As Int32
    End Class
    
    ''' <summary> 
    ''' Enumerate top-level and child windows 
    ''' </summary> 
    ''' <example> 
    ''' Dim enumerator As New WindowsEnumerator()
    ''' For Each top As ApiWindow in enumerator.GetTopLevelWindows()
    '''    Console.WriteLine(top.MainWindowTitle)
    '''    For Each child As ApiWindow child in enumerator.GetChildWindows(top.hWnd) 
    '''        Console.WriteLine(" " + child.MainWindowTitle)
    '''    Next child
    ''' Next top
    ''' </example> 
    Public Class WindowsEnumerator
    
        Private Delegate Function EnumCallBackDelegate(ByVal hwnd As Integer, ByVal lParam As Integer) As Integer
    
        ' Top-level windows.
        Private Declare Function EnumWindows Lib "user32" _
         (ByVal lpEnumFunc As EnumCallBackDelegate, ByVal lParam As Integer) As Integer
    
        ' Child windows.
        Private Declare Function EnumChildWindows Lib "user32" _
         (ByVal hWndParent As Integer, ByVal lpEnumFunc As EnumCallBackDelegate, ByVal lParam As Integer) As Integer
    
        ' Get the window class.
        Private Declare Function GetClassName _
         Lib "user32" Alias "GetClassNameA" _
         (ByVal hwnd As Integer, ByVal lpClassName As StringBuilder, ByVal nMaxCount As Integer) As Integer
    
        ' Test if the window is visible--only get visible ones.
        Private Declare Function IsWindowVisible Lib "user32" _
         (ByVal hwnd As Integer) As Integer
    
        ' Test if the window's parent--only get the one's without parents.
        Private Declare Function GetParent Lib "user32" _
         (ByVal hwnd As Integer) As Integer
    
        ' Get window text length signature.
        Private Declare Function SendMessage _
         Lib "user32" Alias "SendMessageA" _
         (ByVal hwnd As Int32, ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As Int32) As Int32
    
        ' Get window text signature.
        Private Declare Function SendMessage _
         Lib "user32" Alias "SendMessageA" _
         (ByVal hwnd As Int32, ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As StringBuilder) As Int32
    
        Private _listChildren As New List(Of ApiWindow)
        Private _listTopLevel As New List(Of ApiWindow)
    
        Private _topLevelClass As String = ""
        Private _childClass As String = ""
    
        ''' <summary>
        ''' Get all top-level window information
        ''' </summary>
        ''' <returns>List of window information objects</returns>
        Public Overloads Function GetTopLevelWindows() As List(Of ApiWindow)
    
            EnumWindows(AddressOf EnumWindowProc, &H0)
    
            Return _listTopLevel
    
        End Function
    
        Public Overloads Function GetTopLevelWindows(ByVal className As String) As List(Of ApiWindow)
    
            _topLevelClass = className
    
            Return Me.GetTopLevelWindows()
    
        End Function
    
        ''' <summary>
        ''' Get all child windows for the specific windows handle (hwnd).
        ''' </summary>
        ''' <returns>List of child windows for parent window</returns>
        Public Overloads Function GetChildWindows(ByVal hwnd As Int32) As List(Of ApiWindow)
    
            ' Clear the window list.
            _listChildren = New List(Of ApiWindow)
    
            ' Start the enumeration process.
            EnumChildWindows(hwnd, AddressOf EnumChildWindowProc, &H0)
    
            ' Return the children list when the process is completed.
            Return _listChildren
    
        End Function
    
        Public Overloads Function GetChildWindows(ByVal hwnd As Int32, ByVal childClass As String) As List(Of ApiWindow)
    
            ' Set the search
            _childClass = childClass
    
            Return Me.GetChildWindows(hwnd)
    
        End Function
    
        ''' <summary>
        ''' Callback function that does the work of enumerating top-level windows.
        ''' </summary>
        ''' <param name="hwnd">Discovered Window handle</param>
        ''' <returns>1=keep going, 0=stop</returns>
        Private Function EnumWindowProc(ByVal hwnd As Int32, ByVal lParam As Int32) As Int32
    
            ' Eliminate windows that are not top-level.
            If GetParent(hwnd) = 0 AndAlso CBool(IsWindowVisible(hwnd)) Then
    
                ' Get the window title / class name.
                Dim window As ApiWindow = GetWindowIdentification(hwnd)
    
                ' Match the class name if searching for a specific window class.
                If _topLevelClass.Length = 0 OrElse window.ClassName.ToLower() = _topLevelClass.ToLower() Then
                    _listTopLevel.Add(window)
                End If
    
            End If
    
            ' To continue enumeration, return True (1), and to stop enumeration 
            ' return False (0).
            ' When 1 is returned, enumeration continues until there are no 
            ' more windows left.
    
            Return 1
    
        End Function
    
        ''' <summary>
        ''' Callback function that does the work of enumerating child windows.
        ''' </summary>
        ''' <param name="hwnd">Discovered Window handle</param>
        ''' <returns>1=keep going, 0=stop</returns>
        Private Function EnumChildWindowProc(ByVal hwnd As Int32, ByVal lParam As Int32) As Int32
    
            Dim window As ApiWindow = GetWindowIdentification(hwnd)
    
            ' Attempt to match the child class, if one was specified, otherwise
            ' enumerate all the child windows.
            If _childClass.Length = 0 OrElse window.ClassName.ToLower() = _childClass.ToLower() Then
                _listChildren.Add(window)
            End If
    
            Return 1
    
        End Function
    
        ''' <summary>
        ''' Build the ApiWindow object to hold information about the Window object.
        ''' </summary>
        Private Function GetWindowIdentification(ByVal hwnd As Integer) As ApiWindow
    
            Const WM_GETTEXT As Int32 = &HD
            Const WM_GETTEXTLENGTH As Int32 = &HE
    
            Dim window As New ApiWindow()
    
            Dim title As New StringBuilder()
    
            ' Get the size of the string required to hold the window title.
            Dim size As Int32 = SendMessage(hwnd, WM_GETTEXTLENGTH, 0, 0)
    
            ' If the return is 0, there is no title.
            If size > 0 Then
                title = New StringBuilder(size + 1)
    
                SendMessage(hwnd, WM_GETTEXT, title.Capacity, title)
            End If
    
            ' Get the class name for the window.
            Dim classBuilder As New StringBuilder(64)
            GetClassName(hwnd, classBuilder, 64)
    
            ' Set the properties for the ApiWindow object.
            window.ClassName = classBuilder.ToString()
            window.MainWindowTitle = title.ToString()
            window.hWnd = hwnd
    
            Return window
    
        End Function
    
    End Class


    You've taught me everything I know but not everything you know.

    Monday, December 31, 2012 1:43 AM
  • Thank You for reply, I will try this tonight because now I am facing some emergency duty. Thanks again for response.
    Monday, December 31, 2012 11:31 AM
  • I have Spy++ tool,

    Take a look at this again,

    This is my external program, this program can receive and send SMS, when we receive SMS the below window will appear.

    [External Program Image]Click Here To See Image[/External Program image]

    as I mention in my question that I can get text from first textbox "the person mobile number" from the code which I have submitted.

    But I want to get Received message also which is "test"

    Ok now if we drag spy++ to textbox1 which is mobile number we get following.

    And When We Drag SPY++ to Second Text Box which is "Receive text message" we get the following.

    You can see that both have same class name ("Edit").

    I really Don't know hot get group box class first, then get textbox class.. because I am facing this topic first time.
    Can You Please Solve This For Me?

    Note: This project is for helping Chemistry Students. I am creating an auto reply service which reply All the information about ELEMENTS (Like atomic number, mass #, valency, E-configuration, period group etc.) without any charges.

    Monday, December 31, 2012 12:49 PM
  • I altered the application I posted earlier to parse out the pagers received phnr and received text using a test application named "Pager test application".

    First the application gets all data for every top level window. With that data in a string I use regex to parse out the data relevent to Pager test application only to a match collection.

    Then the application retrieves just the Pager test application phnr line and text line from the match collection via index number (which appears to be the same whenever the Pager test application is launched).

    Once the Pager test application phnr line and text line are retrieved another match collection is used to retrieve just the SMS phnr and SMS text.

    Also there are some regex replaces used to make it simpler for searching with regex as well as cleaning up the final text (removing slantbars / ).

    You can use the following code to test with and once you verify that you can retrieve the data you want continually then you can add the code to your own program to display the SMS Phnr and SMS Text in your TextBoxes.

    Anyhow the code follows as well as an image of the application at work. You may need to add some try/catch statements to it in order to keep regex from bombing out if no matches are found for example if you run the code and your pager application is not running at the same time.

    Imports System.Collections.Generic
    Imports System.Runtime.InteropServices
    Imports System.Text
    Imports System.Text.RegularExpressions
    
    
    Public Class Form1
    
        Private Const WM_GETTEXT As Integer = &HD
    
        Declare Auto Function SendMessage Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
    
        <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
        Private Shared Function FindWindowEx(ByVal parentHandle As IntPtr, _
                                         ByVal childAfter As IntPtr, _
                                         ByVal lclassName As String, _
                                         ByVal windowTitle As String) As IntPtr
        End Function
    
        Declare Auto Function FindWindow Lib "user32.dll" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
    
        Dim PagerInformation As String = ""
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            PagerInformation = ""
            RichTextBox1.Text = ""
            Dim enumerator As New WindowsEnumerator()
            For Each top As ApiWindow In enumerator.GetTopLevelWindows()
                For Each child As ApiWindow In enumerator.GetChildWindows(top.hWnd)
                    PagerInformation += top.MainWindowTitle & "    " & child.ClassName & "    " & child.hWnd.ToString & "    " & child.MainWindowTitle & "////" & vbCrLf & vbCrLf
                Next child
            Next top
            RichTextBox1.Text = PagerInformation
            '.....................................................................Name of application goes here followed by (.*?)////
            Dim PagersData As MatchCollection = Regex.Matches(PagerInformation, "Pager Test Application(.*?)////", RegexOptions.IgnoreCase)
            
    
            'You will probably have more matches for your pager application because it has more controls on it I believe.
            'You just need to determine which index match the pager text and pager phnr are at. Whenever I start my pager test application
            'the data for controls, etc, always appears in the same order so when it is matched pager text and phnr are always
            'at the same index.
    
            Dim PagerText As String = PagersData.Item(4).ToString & vbCrLf
            Dim PagerPhnr As String = PagersData.Item(7).ToString & vbCrLf
    
            PagerText = Regex.Replace(PagerText, "    ", "/")
            PagerPhnr = Regex.Replace(PagerPhnr, "    ", "/")
    
            RichTextBox2.AppendText(PagerText & vbCrLf & PagerPhnr)
    
            Dim Text As MatchCollection = Regex.Matches(PagerText, "/(.*?)/", RegexOptions.IgnoreCase)
            Dim Phnr As MatchCollection = Regex.Matches(PagerPhnr, "/(.*?)/", RegexOptions.IgnoreCase)
    
            Dim SMSText As String = Text.Item(1).ToString
            Dim SMSPhnr As String = Phnr.Item(1).ToString
    
            SMSText = Regex.Replace(SMSText, "/", "")
            SMSPhnr = Regex.Replace(SMSPhnr, "/", "")
    
            RichTextBox3.AppendText(SMSText & vbCrLf & SMSPhnr)
    
            TextBox1.Text = SMSPhnr
            TextBox2.Text = SMSText
    
        End Sub
    
        Private Sub RichTextBox1_TextChanged(sender As Object, e As EventArgs) Handles RichTextBox1.TextChanged
    
        End Sub
    
        Private Sub RichTextBox2_TextChanged(sender As Object, e As EventArgs) Handles RichTextBox2.TextChanged
    
        End Sub
    
        Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
    
        End Sub
    
        Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged
    
        End Sub
    
        Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
    
        End Sub
    
        Private Sub RichTextBox3_TextChanged(sender As Object, e As EventArgs) Handles RichTextBox3.TextChanged
    
        End Sub
    
        Private Sub Label3_Click(sender As Object, e As EventArgs) Handles Label3.Click
    
        End Sub
    
        Private Sub Label4_Click(sender As Object, e As EventArgs) Handles Label4.Click
    
        End Sub
    End Class
    
    
    Public Class ApiWindow
        Public MainWindowTitle As String = ""
        Public ClassName As String = ""
        Public hWnd As Int32
    End Class
    
    ''' <summary> 
    ''' Enumerate top-level and child windows 
    ''' </summary> 
    ''' <example> 
    ''' Dim enumerator As New WindowsEnumerator()
    ''' For Each top As ApiWindow in enumerator.GetTopLevelWindows()
    '''    Console.WriteLine(top.MainWindowTitle)
    '''    For Each child As ApiWindow child in enumerator.GetChildWindows(top.hWnd) 
    '''        Console.WriteLine(" " + child.MainWindowTitle)
    '''    Next child
    ''' Next top
    ''' </example> 
    Public Class WindowsEnumerator
    
        Private Delegate Function EnumCallBackDelegate(ByVal hwnd As Integer, ByVal lParam As Integer) As Integer
    
        ' Top-level windows.
        Private Declare Function EnumWindows Lib "user32" _
         (ByVal lpEnumFunc As EnumCallBackDelegate, ByVal lParam As Integer) As Integer
    
        ' Child windows.
        Private Declare Function EnumChildWindows Lib "user32" _
         (ByVal hWndParent As Integer, ByVal lpEnumFunc As EnumCallBackDelegate, ByVal lParam As Integer) As Integer
    
        ' Get the window class.
        Private Declare Function GetClassName _
         Lib "user32" Alias "GetClassNameA" _
         (ByVal hwnd As Integer, ByVal lpClassName As StringBuilder, ByVal nMaxCount As Integer) As Integer
    
        ' Test if the window is visible--only get visible ones.
        Private Declare Function IsWindowVisible Lib "user32" _
         (ByVal hwnd As Integer) As Integer
    
        ' Test if the window's parent--only get the one's without parents.
        Private Declare Function GetParent Lib "user32" _
         (ByVal hwnd As Integer) As Integer
    
        ' Get window text length signature.
        Private Declare Function SendMessage _
         Lib "user32" Alias "SendMessageA" _
         (ByVal hwnd As Int32, ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As Int32) As Int32
    
        ' Get window text signature.
        Private Declare Function SendMessage _
         Lib "user32" Alias "SendMessageA" _
         (ByVal hwnd As Int32, ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As StringBuilder) As Int32
    
        Private _listChildren As New List(Of ApiWindow)
        Private _listTopLevel As New List(Of ApiWindow)
    
        Private _topLevelClass As String = ""
        Private _childClass As String = ""
    
        ''' <summary>
        ''' Get all top-level window information
        ''' </summary>
        ''' <returns>List of window information objects</returns>
        Public Overloads Function GetTopLevelWindows() As List(Of ApiWindow)
    
            EnumWindows(AddressOf EnumWindowProc, &H0)
    
            Return _listTopLevel
    
        End Function
    
        Public Overloads Function GetTopLevelWindows(ByVal className As String) As List(Of ApiWindow)
    
            _topLevelClass = className
    
            Return Me.GetTopLevelWindows()
    
        End Function
    
        ''' <summary>
        ''' Get all child windows for the specific windows handle (hwnd).
        ''' </summary>
        ''' <returns>List of child windows for parent window</returns>
        Public Overloads Function GetChildWindows(ByVal hwnd As Int32) As List(Of ApiWindow)
    
            ' Clear the window list.
            _listChildren = New List(Of ApiWindow)
    
            ' Start the enumeration process.
            EnumChildWindows(hwnd, AddressOf EnumChildWindowProc, &H0)
    
            ' Return the children list when the process is completed.
            Return _listChildren
    
        End Function
    
        Public Overloads Function GetChildWindows(ByVal hwnd As Int32, ByVal childClass As String) As List(Of ApiWindow)
    
            ' Set the search
            _childClass = childClass
    
            Return Me.GetChildWindows(hwnd)
    
        End Function
    
        ''' <summary>
        ''' Callback function that does the work of enumerating top-level windows.
        ''' </summary>
        ''' <param name="hwnd">Discovered Window handle</param>
        ''' <returns>1=keep going, 0=stop</returns>
        Private Function EnumWindowProc(ByVal hwnd As Int32, ByVal lParam As Int32) As Int32
    
            ' Eliminate windows that are not top-level.
            If GetParent(hwnd) = 0 AndAlso CBool(IsWindowVisible(hwnd)) Then
    
                ' Get the window title / class name.
                Dim window As ApiWindow = GetWindowIdentification(hwnd)
    
                ' Match the class name if searching for a specific window class.
                If _topLevelClass.Length = 0 OrElse window.ClassName.ToLower() = _topLevelClass.ToLower() Then
                    _listTopLevel.Add(window)
                End If
    
            End If
    
            ' To continue enumeration, return True (1), and to stop enumeration 
            ' return False (0).
            ' When 1 is returned, enumeration continues until there are no 
            ' more windows left.
    
            Return 1
    
        End Function
    
        ''' <summary>
        ''' Callback function that does the work of enumerating child windows.
        ''' </summary>
        ''' <param name="hwnd">Discovered Window handle</param>
        ''' <returns>1=keep going, 0=stop</returns>
        Private Function EnumChildWindowProc(ByVal hwnd As Int32, ByVal lParam As Int32) As Int32
    
            Dim window As ApiWindow = GetWindowIdentification(hwnd)
    
            ' Attempt to match the child class, if one was specified, otherwise
            ' enumerate all the child windows.
            If _childClass.Length = 0 OrElse window.ClassName.ToLower() = _childClass.ToLower() Then
                _listChildren.Add(window)
            End If
    
            Return 1
    
        End Function
    
        ''' <summary>
        ''' Build the ApiWindow object to hold information about the Window object.
        ''' </summary>
        Private Function GetWindowIdentification(ByVal hwnd As Integer) As ApiWindow
    
            Const WM_GETTEXT As Int32 = &HD
            Const WM_GETTEXTLENGTH As Int32 = &HE
    
            Dim window As New ApiWindow()
    
            Dim title As New StringBuilder()
    
            ' Get the size of the string required to hold the window title.
            Dim size As Int32 = SendMessage(hwnd, WM_GETTEXTLENGTH, 0, 0)
    
            ' If the return is 0, there is no title.
            If size > 0 Then
                title = New StringBuilder(size + 1)
    
                SendMessage(hwnd, WM_GETTEXT, title.Capacity, title)
            End If
    
            ' Get the class name for the window.
            Dim classBuilder As New StringBuilder(64)
            GetClassName(hwnd, classBuilder, 64)
    
            ' Set the properties for the ApiWindow object.
            window.ClassName = classBuilder.ToString()
            window.MainWindowTitle = title.ToString()
            window.hWnd = hwnd
    
            Return window
    
        End Function
    
    End Class


    You've taught me everything I know but not everything you know.



    Monday, December 31, 2012 8:04 PM
  • Saqib1900 thanks for marking me as the answer. If you have any trouble using regex just copy all of your sms pager things information that is collected and I can probably use that text to simulate parsing out the information you need.

    You've taught me everything I know but not everything you know.

    Tuesday, January 1, 2013 4:49 AM
  • On another note if people send you slant bars (/) in text messages they will be deleted by regex replace.

    You've taught me everything I know but not everything you know.

    Tuesday, January 1, 2013 4:46 PM