How to get control names using their hWnd handle


New member
Sep 28, 2009
Programming Experience
Hi, I'm working on an automation tool to fill textboxes in a 3rd. party application.

I already managed to get a Winform handle, its childs control handles, also to get the textbox contents and set new values for them using the following code:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim appProc() As Process
Dim hWnd As Long
Dim hTextbox As Long
Dim sText As String

appProc = Process.GetProcessesByName("testapp")
hWnd = appProc(0).MainWindowHandle
hTextbox = FindWindowEx(hWnd, 0&, "txtName", vbNullString)

' 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
sText = Space(size + 1)

SendMessage(hWnd, WM_GETTEXT, Len(sText), sText)
End If
size = SendMessage(hTextbox, WM_GETTEXTLENGTH, 0, 0)
If size > 0 Then
sText = Space(size + 1)

SendMessage(hTextbox, WM_GETTEXT, size + 1, sText)
End If

sText = "Test 1!"
SendMessage(hTextbox, WM_SETTEXT, Len(sText) + 1, sText)
End Sub

The problem I'm facing is that if a form has more than a textbox I have to check by its control name to be sure where I'm sending the text.

I researched and found WM_GETCONTROLNAME but I couldn't make it work with the code:

Dim msg As Long = RegisterWindowMessage("WM_GETCONTROLNAME")
sText = Space(100)
SendMessage(hWnd, msg, 100, sText)

Any help to find a way to get control names from their handles will be greatly appreciated!!
Top Bottom