After stumbling through several pages I found the solution how to retrieve the contents from a textbox from an external application. I think this code could be usefull for others too, so here is the result:
Module modApi
Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
Private Declare Function GetClassName Lib "user32.dll" Alias "GetClassNameA" (ByVal hwnd As IntPtr, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Private Declare Function SendMessageTimeoutString Lib "user32.dll" Alias "SendMessageTimeoutA" (ByVal hwnd As IntPtr, ByVal msg As Long, ByVal wParam As Long, ByVal lParam As String, ByVal fuFlags As Long, ByVal uTimeout As Long, ByVal lpdwResult As Long) As Long
Friend Declare Function EnumChildWindows Lib "user32.dll" (ByVal hWndParent As IntPtr, ByVal funcCallBack As funcCallBackChild, ByVal lParam As IntPtr) As Boolean
Public Delegate Function funcCallBackChild(ByVal hWnd As IntPtr, ByVal lParam As IntPtr) As Boolean
Public TextBoxStrings As ArrayList = New ArrayList
Public Sub ParseWindowControls(ByVal WindowTitle As String)
Dim hWnd As IntPtr = FindWindow(vbNullString, WindowTitle)
TextBoxStrings.Clear()
If hWnd Then
Dim MyCallBack As New funcCallBackChild(AddressOf EnumChildWindowsProc)
EnumChildWindows(hWnd, MyCallBack, IntPtr.Zero)
Else
MsgBox("Could not find window!", vbOKOnly + vbExclamation, "Error")
End If
End Sub
Private Function EnumChildWindowsProc(ByVal hWndParent As IntPtr, ByVal lParam As IntPtr) As Boolean
Dim Buffer As String = Space(256)
Dim Retval As Long = GetClassName(hWndParent, Buffer, Len(Buffer))
If Left(Buffer, Retval) = "WindowsForms10.EDIT.app.0.218f99c" Then
TextBoxStrings.Add(GetText(hWndParent))
End If
EnumChildWindowsProc = True
End Function
Private Function GetText(ByVal hwnd As IntPtr) As String
Dim sText As String = Space(1024)
If SendMessageTimeoutString(hwnd, &HD, 1024, sText, &H2, 1000, 0) <> 0 Then
GetText = Left(sText, InStr(sText, vbNullChar) - 1)
Exit Function
End If
GetText = ""
End Function
End Module
You can pass a window title to the sub ParseWindowControls()
. The sub tries to find the requested window. If the window is found it begins to gather all controls found in this application. The callback examines the found control if it meets my specifications (textbox) and stores the text in a arraylist. Later you just have to know the index of your requestet textbox and get it out of the arraylist. That's it. Hope this helps others too.