Results 1 to 16 of 16

Thread: Control other application

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2005
    Posts
    105

    Control other application

    Hi all,
    I want to control a third party software, read texts from textboxes, write to texboxes and other controls.

    I tried find out the handle of cotrols using Spy++ but the handle number changes every time the program loads.

    So how can we dynamically find out the correct handle number of a specific component ? How can use Spy++ to do this work

    Regards,
    Nasreen
    Last edited by nasreen; Jan 15th, 2012 at 01:00 AM.

  2. #2

    Thread Starter
    Lively Member
    Join Date
    Apr 2005
    Posts
    105

    Re: Control other application

    I searched the Forum, there are some thread discussing about Spy++ but I couldn't found a solution to fnd out the correct handle number that subject to change every loading time.
    So I expect from any experts to respond my query

  3. #3
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Control other application

    Handles will always change. They are allocated by Windows, not by the application. For example, if you add a textbox to your form and run the form with the following code in Form_Load, you will see this each time you run the project
    Code:
    MsgBox Text1.hWnd: Unload Me
    So, what you need to do is locate the target textbox. This is usually done with the FindWindowEx API. Recommend you search the forum for examples, using the keyword: FindWindowEx textbox
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Apr 2005
    Posts
    105

    Re: Control other application

    Thanks LaVolpe,
    I searched the forum I see many threads but didn't get an idea how to do this.
    Assume I have an application developed by a third party. Now I want pass user name and password to this application and read texts from its text boxes using my application.
    I used Spy++ and see handles in numbers that changes every time the application loads. So how can I locate to the text box I want to read and write ?
    I expect a detailed reply with help of samples
    Thanks in advance

  5. #5
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Control other application

    Quote Originally Posted by nasreen View Post
    I expect a detailed reply with help of samples
    They exist on this site, just search.

    Besides looking for examples using FindWindowEx, also look for ones using these two APIs: GetWindowText, SetWindowText
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Apr 2005
    Posts
    105

    Re: Control other application

    Dear Mr. LaVolpe
    As per your valued advice, these days I have been doing search for words GetWindowText, SetWindowText and I got many threads.
    But I couldn't get a solution to write or read a text box in another application. The main issue I am facing is the change of handle each time the application start. So I can't to locate the correct text box I expect.

    Now I attach a sample project with two text boxes and a command button and no code. Here I request you to send me another sample to write a text in Text1 and read text of Text2 of my attached sample. Your help is highly appreciated.

    With regards,
    Nasreen
    Attached Files Attached Files

  7. #7
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Control other application

    Quote Originally Posted by nasreen View Post
    The main issue I am facing is the change of handle each time the application start. So I can't to locate the correct text box I expect.
    The odds of the handle being the same are about the same odds of me winning the national lottery. I already mentioned that you need to find the main level window and then navigate to find the textbox window using the APIs FindWindow & FindWindowEx. There are examples on this site.

    Also, I don't see any code in that zip file. Use a tool like Spy++ to find the target window you are after. Look at the parent hierarchy all the way to the main level window. For each level of hierarchy, you'll need a FindWindowEx call. If there are more than one textbox contained in the parent, you'll need to figure out how you can tell one from the other. It may be the ZOrder, it may be the physical position in relation to other sibling windows, it could be a static control ID.

    A custom search for the textbox needs to be done. There is no one solution to fit all applications because 1) the window to be found can be different, 2) the ZOrder of the window can be different, and 3) the window's parent can be different.

    Bottom line. This is not something that someone else can do for you. You have the application you are trying to remotely automate, you'll have to do the work. Examples found here and on the web can be used just as examples. None found are going to be 'plug & play'. Maybe a screenshot of the hierarchy shown by Spy++ will allow others to help too

    Note: Spy++ comes with your VB installation disks
    Last edited by LaVolpe; Jan 30th, 2012 at 06:48 PM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Apr 2005
    Posts
    105

    Re: Control other application

    Dear LaVolpe

    Also, I don't see any code in that zip file
    It is clear there is no code in my attachment and I already mentioned it.
    What I expect with the attachment is to get another code as sample from you to read text1 and write to text2 in my attachment, so I can follow this sample code provided by you in my real application.
    So I hope you will consider my request

    With Regards,
    Nasreen

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Apr 2005
    Posts
    105

    Re: Control other application

    I am back once again to this project.
    Still I am in confuse how can I read and write a string of Textbox in an another application.
    I searched GetWindowText and get many examples, but all are focus for a single text box like Calculator etc.
    In my case there are 5 textboxes and the class of all are same. So I can't to locate to a specific textbox.
    For example, consider google talk messenger. It has two text boxes for user name and password. The class name for both of these textboxes is Edit. In this case how can I locate to UserName textbox and password textbox individually.
    So can anybody post a sample code to read and write to two textboxes of GTalk Messenger.

    Thanks in advance
    Nasreen

  10. #10
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Control other application

    Quote Originally Posted by nasreen View Post
    In my case there are 5 textboxes and the class of all are same. So I can't to locate to a specific textbox.
    As LaVolpe said, "you'll need to figure out how you can tell one from the other."

    One way might be to get the control IDs using the GetWindowLong API. If the IDs are the same every time the app is run then you should be able to identify which textbox is which.

    ID = GetWindowLong(hWnd, GWL_ID)

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Apr 2005
    Posts
    105

    Re: Control other application

    Thanks for your grate advice

    Now I have an issue in find out each text boxes

    I use a code posted by Mr. LaVolpe in a related thread to enumerate through each text boxes , but the same doesn't provide the expected result as mentioned in the commented line.

    Here is the code:

    Code:
    Private Sub Command1_Click()
    Dim editHwnd As Long, nextHwnd As Long, cID As Long
         ' supply the parent/password window's hWnd below as pHwnd
         
            Dim pHwnd As Long
            Dim sWindowText As String, lTextLen As Long, ChldHwnd As Long
            lHwnd = FindWindow(vbNullString, "Transaction Master")
            ChldHwnd = FindWindowEx(lHwnd, 0&, "ThunderRT6Frame", vbNullString)
         Do
             nextHwnd = FindWindowEx(ChldHwnd, 0&, "ThunderRT6TextBox", vbNullString)
             If nextHwnd = 0& Then Exit Do
             editHwnd = nextHwnd
             cID = GetWindowLong(editHwnd, GWL_ID)
             Debug.Print "Edit hWnd "; editHwnd; " ID is "; cID
         Loop
    ' now you'll have a list of edit control windows & their IDs within the parent window.
    ' run several times to see if the control id changes or not, the editHwnd will probably change, but no biggie
    
    End Sub
    Every time I get the same result of same text box without jumping to the next text box.

    Here is the result

    Code:
    Edit hWnd  4588562  ID is  3 
    Edit hWnd  4588562  ID is  3 
    Edit hWnd  4588562  ID is  3 
    Edit hWnd  4588562  ID is  3 
    Edit hWnd  4588562  ID is  3 
    Edit hWnd  4588562  ID is  3 
    Edit hWnd  4588562  ID is  3
    So please advise me how can I fix it.

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Apr 2005
    Posts
    105

    Re: Control other application

    Hi,
    Can anybody replay me pls ?

  13. #13
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Control other application

    Something like this perhaps
    Code:
    Option Explicit
    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 GetWindowLong Lib "user32" Alias "GetWindowLongA" _
                    (ByVal hwnd As Long, ByVal nIndex As Long) As Long
    
    Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    
    Private Const GWL_ID = (-12)
    
    Private Sub Command_Click()
    Dim lngHWND As Long
    Dim lngHWNDF As Long
    Dim lngID As Long
    Dim lngChild As Long
    Dim lngRet As Long
    lngHWND = FindWindow(vbNullString, "Doogle's Folly")
    lngHWNDF = FindWindowEx(lngHWND, 0&, "ThunderFrame", vbNullString)
    Do
        lngChild = FindWindowEx(lngHWNDF, lngChild, "ThunderTextBox", vbNullString)
        If lngChild <> 0 Then
            lngID = GetWindowLong(lngChild, GWL_ID)
            Debug.Print "Child HWND: " & Hex(lngChild) & " ID: " & lngID
            lngRet = CloseHandle(lngChild)
        End If
    Loop Until lngChild = 0
    lngRet = CloseHandle(lngHWNDF)
    lngRet = CloseHandle(lngHWND)
    End Sub
    
    Private Sub Form_Load()
    Me.Caption = "Doogle's Folly"
    End Sub
    Last edited by Doogle; Mar 23rd, 2012 at 02:13 AM. Reason: Added CloseHandle and comments

  14. #14

    Thread Starter
    Lively Member
    Join Date
    Apr 2005
    Posts
    105

    Re: Control other application

    Thanks a lot,
    Code of Mr. Doogle meets my needs.
    Let me go ahead in this project and will back if any issue

    Nasreen

  15. #15

    Thread Starter
    Lively Member
    Join Date
    Apr 2005
    Posts
    105

    Re: Control other application

    Hi All,

    Is it possible to track if a button n a third party application is Clicked or Not i ?.
    I think it is possible by API WM_Command, but I don't know how to apply
    Please explain with sample code

    Nasreen

  16. #16

    Thread Starter
    Lively Member
    Join Date
    Apr 2005
    Posts
    105

    Re: Control other application

    How I can read text of a selected item in a listview.

    I am in stuck now, pls help me

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