CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15
  1. #1
    Join Date
    Jan 2013
    Posts
    12

    VB6 - Need help w SendMessage to pass credentials to textbox on another application

    Hey all, I'm new to the forum, and need some help building my application. I'm less than an amatuer when it comes to coding, so i'll apologize up front if this is a simple/dumb question.

    I'm needing some help with FindWindow/FindWindowEx/SendMessage. I need to be able to pass text from my application to another, so that I can automate the logon process.

    The application that I'm needing to pass the info to has 3 texboxes, all with the class of "Edit". The first textbox is disabled, the second is for username, and the third is for password.

    Since each textbox has the same class, how do I have my app distiguish which textbox is for username and which is for password? And then tell it to pass the login info to the appropriate texbox?

    I've googled and searched and found where some one mentioned that it's possible to loop through every textbox on the form, then get the position of each textbox but my skills are not that sharp yet.

    All help is greatly appreciated!

  2. #2
    Join Date
    Jan 2013
    Posts
    12

    Re: VB6 - Need help w SendMessage to pass credentials to textbox on another applicati

    I had a similar app that I pieced together in the past that would do this, but it was only written for a username and password text box. So with the introduction of the third textbox, it crashes my application.

    I'll post the code, maybe someone could tell me what I need to fix to make it work?

    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")
    'this will return the 'VNC Viewer : Connection Details " windows Handle
    
    lhndChild = GetWindow(lhnd, GW_CHILD) 'Retrieve handles of the child windows in the specified window
    
    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 retrieve, 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.
           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

  3. #3
    Join Date
    Jan 2013
    Posts
    12

    Re: VB6 - Need help w SendMessage to pass credentials to textbox on another applicati

    Here is a screen shot of what the application looks like:

    Name:  Login.jpg
Views: 3356
Size:  25.8 KB

  4. #4
    Join Date
    Jan 2013
    Posts
    12

    Re: VB6 - Need help w SendMessage to pass credentials to textbox on another applicati

    Here is where I'm getting the debug err msg:
    Code:
    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. <----Here is where the Debug Error pops up at.
               strItems(p, 1) = rec.Top ' Strore the top value in the arry.
           p = p + 1
        End If
    
    Loop

  5. #5
    Join Date
    Sep 2000
    Location
    FL
    Posts
    1,452

    Re: VB6 - Need help w SendMessage to pass credentials to textbox on another applicati

    You don't tell us what the error is, but it could be that you have 3 edit boxes, but you strItems is defined as 1,1 so you could be getting errors trying to add the 3rd box?

  6. #6
    Join Date
    Jan 2013
    Posts
    12

    Re: VB6 - Need help w SendMessage to pass credentials to textbox on another applicati

    Quote Originally Posted by sotoasty View Post
    You don't tell us what the error is, but it could be that you have 3 edit boxes, but you strItems is defined as 1,1 so you could be getting errors trying to add the 3rd box?
    Sorry I left that part out of it. Here is the error message:

    Run-time error '9': Subscript out of range

    Here is the line of code that it is erroring out at, in bold:

    Code:
    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
    I believe you are right, the array isn't setup for 3 textboxes, it was originally written for 2.
    This is the part that I need some help on though, I'm not good with arrays at all.

    Any ideas on how I can fix this?

  7. #7
    Join Date
    Sep 2000
    Location
    FL
    Posts
    1,452

    Re: VB6 - Need help w SendMessage to pass credentials to textbox on another applicati

    change....
    Code:
    Dim strItems(1, 1) As Long
    to .....
    Code:
    Dim strItems(2, 1) As Long

  8. #8
    Join Date
    Jan 2013
    Posts
    12

    Re: VB6 - Need help w SendMessage to pass credentials to textbox on another applicati

    Quote Originally Posted by sotoasty View Post
    change....
    Code:
    Dim strItems(1, 1) As Long
    to .....
    Code:
    Dim strItems(2, 1) As Long
    I changed that part of the code like you recommended and that fixed it, thanks! I think I understand how the array is working on this one.

  9. #9
    Join Date
    Jan 2013
    Posts
    12

    Re: VB6 - Need help w SendMessage to pass credentials to textbox on another applicati

    sotoasty - have time to help me with another issue?

    I'm having a hard time finding a specific window within this same application. I'm trying to find the Textbox and the Retrieve command button..

    When I try and first FindWindow for Schedule Adherence, the window isn't found. I did find that there is another Window called MDIClient. I can use FindWindow on the MDIClient window and it returns the handle, but I can't find anything else after that.

    Any ideas?

    Here is a screen shot, I tried to be as descriptive as I could.

    Name:  IEX MU.jpg
Views: 3211
Size:  88.2 KB

  10. #10
    Join Date
    Sep 2000
    Location
    FL
    Posts
    1,452

    Re: VB6 - Need help w SendMessage to pass credentials to textbox on another applicati

    Well in this case, it will most definitely be much more difficult. This will have to be a multi-step process. You will need to use the FindWindowEX function that allows you to limit the search for new windows based on a Parent window.

    1. Find the IEX (main application window)
    2. Find the MDIClient window. For this you will want to use the FindWindowEX function that allows you to pass in the parent, theb check the classname for the MDIClient, using the Window Handle of the IEX (main windows) as the parent..
    3. Use findwindowex to find the "Schedule Adherence" widow using the MDIClient Window handle as the parent.
    4. Use findwindowex to find the Blank window, that holds the button select using "SHED ADHER" window as parent.
    5. Use findwindowex to find the "Select" window, that holds the button select using "Blank" window handle as parent.
    6. Last find the handle of the textbox and button using FindWindowEX and the "SELECT" window as the parent.

    It will not be an easy task as you might find more windows as blank, and you might have to traverse each tree until you find the right one.

    It looks like you are using Spy++ to get the window information. Spy ++ should show you the hierarchy that you need to traverse.

  11. #11
    Join Date
    Jan 2013
    Posts
    12

    Re: VB6 - Need help w SendMessage to pass credentials to textbox on another applicati

    Quote Originally Posted by sotoasty View Post
    Well in this case, it will most definitely be much more difficult. This will have to be a multi-step process. You will need to use the FindWindowEX function that allows you to limit the search for new windows based on a Parent window.

    1. Find the IEX (main application window)
    2. Find the MDIClient window. For this you will want to use the FindWindowEX function that allows you to pass in the parent, theb check the classname for the MDIClient, using the Window Handle of the IEX (main windows) as the parent..
    3. Use findwindowex to find the "Schedule Adherence" widow using the MDIClient Window handle as the parent.
    4. Use findwindowex to find the Blank window, that holds the button select using "SHED ADHER" window as parent.
    5. Use findwindowex to find the "Select" window, that holds the button select using "Blank" window handle as parent.
    6. Last find the handle of the textbox and button using FindWindowEX and the "SELECT" window as the parent.

    It will not be an easy task as you might find more windows as blank, and you might have to traverse each tree until you find the right one.

    It looks like you are using Spy++ to get the window information. Spy ++ should show you the hierarchy that you need to traverse.
    Thanks, I got it to work!

    I was actually using a Spy program that doesn't show the entire hierarchy, it only shows the control that you have highlighted while using it. So I searched for a better Spy app and found one that shows the hierarchy. This makes it MUCH easier to drill down and find where the controls are located it.

    How would I go about accessing the File Menu? There is a specific option that I want to click on within the file menu, but I can't use the spy program on it b/c once the menu loses focus it goes away. Or is this even possible?

    I'm using SendKeys right now in order to hit the specific Menu options...and while this works, I'd rather use an alternate method if available since sendkeys can be interrupted easily.

  12. #12
    Join Date
    Sep 2000
    Location
    FL
    Posts
    1,452

    Re: VB6 - Need help w SendMessage to pass credentials to textbox on another applicati

    You will probably want to use the "SENDMESSAGE" api. If you use Spy++, there is an option to show the Messages that the window is receiving. So if you click on a Menu, you will see what message is sent to the window, for that menu option.

  13. #13
    Join Date
    Jan 2013
    Posts
    12

    Re: VB6 - Need help w SendMessage to pass credentials to textbox on another applicati

    Quote Originally Posted by sotoasty View Post
    You will probably want to use the "SENDMESSAGE" api. If you use Spy++, there is an option to show the Messages that the window is receiving. So if you click on a Menu, you will see what message is sent to the window, for that menu option.
    For some reason when I originally installed VB6, it didn't include Spy++ with it. Where is Spy++ located, or where can I download it from.

    I can't find my VB6 CD, only the CD for SP3.

    I found that my current spy app, just a basic free one I found through google..only gives the hierarchy of controls and windows, but doesn't detect File Menus or have any option to expand to file menus.

  14. #14
    Join Date
    Jan 2013
    Posts
    12

    Re: VB6 - Need help w SendMessage to pass credentials to textbox on another applicati

    Quote Originally Posted by sotoasty View Post
    You will probably want to use the "SENDMESSAGE" api. If you use Spy++, there is an option to show the Messages that the window is receiving. So if you click on a Menu, you will see what message is sent to the window, for that menu option.
    OK so I have Spy++ v8 now. I found the messaging option that you are talking about, but need some help deciphering what I'm looking at, and looking for.

    Do I need to use the Find Window option first, and then choose the messages option?

  15. #15
    Join Date
    Jan 2013
    Posts
    12

    Re: VB6 - Need help w SendMessage to pass credentials to textbox on another applicati

    any ideas?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured