Get value of textbox within a different application
Hi.
Im trying to get the value from a textbox on a different VB6 application. The application with the data has a window called Communicator and the textbox is called txtTotalCyclic. Below is the code i have so far which seems to get the window handle ok but than when trying to get the actual text i get load of square boxes instead of a string. I've tried several forums and seem to be going around in circles here. Thanks in advance for any suggestions.
Code:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal strClassName As Any, ByVal strWindowText As String) As Long
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As Any, ByVal lpsz2 As String) As Long
Private Declare Function GetWindow Lib "user32" Alias "GetWindowA" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
Private Declare Function SendMessageByString Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageW" (ByVal hwnd As Long, ByVal msg As Long, ByRef wParam As Any, ByRef lParam As Any) As Long
Const GW_CHILD = 5
Private Const WM_GETTEXT As Long = &HD
Private Const WM_GETTEXTLENGTH As Long = &HE
Function GetCommCyclic()
Dim lngParent As Long
Dim lngChild As Long
Dim lret As Long
Dim lBuff As String
Dim iText
CommsWindowTitle = "Communicator"
lngParent = FindWindow(0&, CommsWindowTitle)
If lngParent <> 0 Then
lngChild = FindWindowEx(lngParent, 0, 0&, vbNullString)
Do While lngChild <> 0
lngChild = FindWindowEx(lngParent, lngChild, vbNullString, vbNullString)
txtLen = SendMessage(lngChild, WM_GETTEXTLENGTH, ByVal 0, ByVal 0)
lBuff$ = String$(txtLen + 1, 0)
Call SendMessageByString(lngChild, WM_GETTEXT, txtLen + 1, a)
iText = lBuff$
Loop
End If
End Function
Re: Get value of textbox within a different application
Issues:
1) You ignore the first child-window found completely
2) You do realize that you return all Text-content of all (!) child-windows? that includes labels, pictureboxes and whatnot you have on that other Window that contains text
EDIT: Actually, you don't return anything since you use a parameter "a" in your SendMessageByString! Shouldn't that be "lBuff"?
Why don't you use GetWindowText?
Why don't you use the classname-parameter in FindWindowEx if you're looking for a textbox? --> https://docs.microsoft.com/en-us/win...-findwindowexw
Re: Get value of textbox within a different application
Can't edit my post
3) You're returning (if you return anything at all) only the value of the last child found