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
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"
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
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.
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"
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
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.
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.
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
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
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