I'm needing a little help with using FindWindow, FindWindowEx, and SendMessage in order to pass a username and password to a login dialogue box from another application.
I am trying to pass a username & password to an application, and make it hit the "ok" button. The issue that I am having is that both of the text boxes used for username & password have the class of "Edit" and are both blank.
How do i determine which textbox is which, and send the correct login info to them?
I had this working with another application, but when I tried it with Internet Explorer login prompt, and with another app, I get an error message. Here is the code that I'm using, but I get a debug error.
I have to say that I'm not super efficient in coding, but I can normally adapt simple code to my needs, but I don't understand why I'm getting an error. All help is much appreciated!
I am trying to pass a username & password to an application, and make it hit the "ok" button. The issue that I am having is that both of the text boxes used for username & password have the class of "Edit" and are both blank.
How do i determine which textbox is which, and send the correct login info to them?
I had this working with another application, but when I tried it with Internet Explorer login prompt, and with another app, I get an error message. Here is the code that I'm using, but I get a debug error.
I have to say that I'm not super efficient in coding, but I can normally adapt simple code to my needs, but I don't understand why I'm getting an error. All help is much appreciated!
Code:
Option Explicit
'Run the control.exe in the app.path and execute this code.
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Integer, ByVal lParam As Any) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
'Used here to retrive the child windows from a given parent window
Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
'used here to obtain the top position of the Text Boxes in the Screen.
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
'Used to get the Class names of the controls
Private Const WM_SETTEXT = &HC
Private Const GW_HWNDNEXT = 2
Private Const GW_CHILD = 5
Private Type RECT 'GetWindowRect use this structure to store the values it ritrive.
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private StrClass As String * 255
Code:
Private Sub Command1_Click()
Dim lhnd As Long 'parent window handle
Dim lhndChild As Long 'child window handle
Dim TxtUnameHnd As Long 'The user name text box handle
Dim txtPassHnd As Long 'the password text box handle
Dim lngresult As Long
Dim i As Integer 'Used with instr() To manipulate the class text returned.
Dim StrClassName As String 'GetClassName used this buffer to store the class name
Dim rec As RECT
Dim strItems(1, 1) As Long 'Initializing a two Diamensional Array to hold the Handles and RECT's Top value of the two text boxes.
Dim p As Integer 'Used with arry element position.
lhnd = FindWindow(vbNullString, "User Logon") 'Returns the specified windows Handle
lhndChild = GetWindow(lhnd, GW_CHILD) 'Retrieves handles of the child windows in the 'user logon' window....not sure why this is needed.
Do
lhndChild = GetWindow(lhndChild, GW_HWNDNEXT) 'Get text of the fist child window
If lhndChild = 0 Then Exit Do 'IF no more child window to ritrive, then exit.
Call GetClassName(lhndChild, StrClass, 255) 'get class name of the first child window
i = InStr(1, StrClass, Chr$(0)) 'trim the Null Charactors of strClass buffer
If Left$(StrClass, i - 1) = "Edit" Then 'Check weather the contol class name is a TextBox (ThunderRT6TextBox).
GetWindowRect lhndChild, rec 'Get that windows screen top position.
strItems(p, 0) = lhndChild 'store the handle in the arrys. <------Run Time Error 9 Subscript out of range.
strItems(p, 1) = rec.Top ' Strore the top value in the arry.
p = p + 1
End If
Loop
'Find the Username text box by Manipulating the top positions of the two text boxes.
If strItems(0, 1) < strItems(1, 1) Then
TxtUnameHnd = strItems(0, 0) 'assign the handle
txtPassHnd = strItems(1, 0)
Else
TxtUnameHnd = strItems(1, 0) 'assign the handle
txtPassHnd = strItems(0, 0)
End If
lngresult = SendMessage(TxtUnameHnd, WM_SETTEXT, 0&, ByVal "Test")
lngresult = SendMessage(txtPassHnd, WM_SETTEXT, 0&, ByVal "Password")
End Sub