A1VBCode Forums

Writing to Logon box


http://www.a1vbcode.com/vbforums/Topic13886.aspx

By macroman - 8/26/2005

Hi! I don't have much experience using API calls, but I am trying to write some code to fill in the username, password and click the OK button. I have been able to get the control handles with FindWindowEx and tried using SetWindowText to write the info, but it didn't work right. I have the Analyzer.exe from the APIGuide website and it confirms the name and password were written to the edit boxes, but they are not visible and clicking the OK button doesn't satisfy the requirements - the logon box comes right back up. Is there a better API call or a better method to use? Any suggestions are appreciated. Here's the code I tried:

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 FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function SetWindowText Lib "user32" Alias "SetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String) As Long
Dim tWnd As Long, bWnd As Long, cWnd As Long, ncWnd As Long


Private Sub main()
    'Get the logon window handle
    tWnd = FindWindow("#32770", vbNullString)
    'Get the username's window handle
    bWnd = FindWindowEx(tWnd, ByVal 0, "EDIT", vbNullString)
    'paste in username
    myuser = "user" & Chr$(0)
    SetWindowText bWnd, myuser
    'get password window handle
    cWnd = FindWindowEx(tWnd, bWnd, "EDIT", vbNullString)
    'paste in password
    mypass = "password" & Chr$(0)
    SetWindowText cWnd, mypass

    'if this works, need to focus and click the OK button or sendkey "enter"
End Sub


 

By macroman - 9/7/2005

Semi-success! I was able to write to the text boxes just fine but couldn't get the OK button to click, and I know from the Analyzer that I was supplying the right handle to the SendMessage command... are you sending the ENTER_KEY to the form or to a button on the form???

Is there a handy listing of the constants used with these commands? I tried to find such a table on the internet, searching Google with WM_SETTEXT. Mostly I got lost in the labyrnth of MSDN... I know somewhere I saw a _BUTTONCLICK constant mentioned, but haven't found my way back to wherever that was! Sooner or later I am going to get a book on this subject.