I want to create an exe which will get information from another, baring in mind that the other exe wont be mine, but someone else's, therefore i do not know the names of the text box i want to read. Is there a way to read all the data on the other exe and then display it in a text box on my exe.
If the above is not possible, the other exe is been given its information via a port, is there a way i can read the information passing through the port.
If you have any questions please ask, as the above may not be to clear.
The following would get the text box of Calculator, have a look
Code:
Option Explicit
Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" ( _
ByVal hWnd As Long, _
ByVal Msg As Long, _
wParam As Any, _
lParam As Any) As Long
Private Declare Function FindWindowEx Lib "user32.dll" Alias "FindWindowExA" ( _
ByVal hwndParent As Long, _
ByVal hwndChildAfter As Long, _
ByVal lpszClass As String, _
ByVal lpszWindow As String) As Long
Private Const WM_GETTEXT As Long = &HD
Private Const WM_GETTEXTLENGTH As Long = &HE
Private Sub Command1_Click()
Dim CalcHwnd As Long ' Calculator's handle
Dim EditWwnd As Long ' handle to the Calculator's Edit window
Dim slength As Long ' length of the found window's text
Dim wintext As String ' holds the window's text
Dim retval As Long ' return value
CalcHwnd = FindWindowEx(0, 0, "SciCalc", "Calculator") 'get calculator handle
EdithWnd = FindWindowEx(CalcHwnd, 0, "Edit", vbNullString) 'get its Edit control handle
If EdithWnd = 0 Then
Debug.Print "Did not find edit box"
Else
' First, determine how much space is necessary for the buffer.
' (1 is added for the terminating null character.)
slength = SendMessage(EdithWnd, WM_GETTEXTLENGTH, ByVal CLng(0), ByVal CLng(0)) + 1
' Make enough room in the buffer to receive the text.
wintext = Space$(slength)
' Copy the target window's text into the buffer.
retval = SendMessage(EdithWnd, WM_GETTEXT, ByVal slength, ByVal wintext)
' Remove the terminating null and extra space from the buffer.
wintext = Left$(wintext, retval)
' Display the result.
Debug.Print "Calculator's text is: "; wintext
End If
End Sub
The following would get the text box of Calculator, have a look
Code:
Option Explicit
Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" ( _
ByVal hWnd As Long, _
ByVal Msg As Long, _
wParam As Any, _
lParam As Any) As Long
Private Declare Function FindWindowEx Lib "user32.dll" Alias "FindWindowExA" ( _
ByVal hwndParent As Long, _
ByVal hwndChildAfter As Long, _
ByVal lpszClass As String, _
ByVal lpszWindow As String) As Long
Private Const WM_GETTEXT As Long = &HD
Private Const WM_GETTEXTLENGTH As Long = &HE
Private Sub Command1_Click()
Dim CalcHwnd As Long ' Calculator's handle
Dim EditWwnd As Long ' handle to the Calculator's Edit window
Dim slength As Long ' length of the found window's text
Dim wintext As String ' holds the window's text
Dim retval As Long ' return value
CalcHwnd = FindWindowEx(0, 0, "SciCalc", "Calculator") 'get calculator handle
EdithWnd = FindWindowEx(CalcHwnd, 0, "Edit", vbNullString) 'get its Edit control handle
If EdithWnd = 0 Then
Debug.Print "Did not find edit box"
Else
' First, determine how much space is necessary for the buffer.
' (1 is added for the terminating null character.)
slength = SendMessage(EdithWnd, WM_GETTEXTLENGTH, ByVal CLng(0), ByVal CLng(0)) + 1
' Make enough room in the buffer to receive the text.
wintext = Space$(slength)
' Copy the target window's text into the buffer.
retval = SendMessage(EdithWnd, WM_GETTEXT, ByVal slength, ByVal wintext)
' Remove the terminating null and extra space from the buffer.
wintext = Left$(wintext, retval)
' Display the result.
Debug.Print "Calculator's text is: "; wintext
End If
End Sub
Hi Again,
I have just tried the above code and it works great, but if the exe was called Game ID and i didn't know what the textbox was called that i wanted to read from, how would i change the code to achieve this.
Thanks in advance
Chris
It's FREE with VB6, or you can google and find a replacement that works the same way. How would you think that you would find a program named SPY++ on the Internet? www.live.com?
Hi, thanks for the help, just one more question (I hope), i have several text boxes on the other EXE and they all come up with the same class name, how do i choose the one i want.
One thing that I have in mind is that you could loop through the child boxes and after determining your target control then you could specify it as your condition in your next loop whenever you need to access that control. Below is a dummy code, not tested so no warranties guaranteed. ;-)
Code:
Private Sub FindTarget()
Dim a As Long
Dim ParentHwnd As Long
Dim editx As Long
ParentHwnd = FindWindow(vbNullString, vbNullString)
editx = FindWindowEx(ParentHwnd, 0&, "Edit", vbNullString)
a = 1
Do Until editx = 0&
'get its text and determine if it is the correct target
'if yes then store a then use it when you need to find the control again, of course you need to exit the loop if found
editx = FindWindowEx(ParentHwnd, editx, "Edit", vbNullString)
a = a + 1
Loop
MsgBox a
End Sub
'pass to this the value of a
Private Sub NextLoop(ByVal index As Long)
Dim a As Long
Dim ParentHwnd As Long
Dim editx As Long
ParentHwnd = FindWindow(vbNullString, vbNullString)
editx = FindWindowEx(ParentHwnd, 0&, "Edit", vbNullString)
a = 1
Do Until editx = 0& Or a = index
'get its text and determine if it is the correct target
'if yes then store a then use it when you need to find the control again, of course you need to exit the loop if found
editx = FindWindowEx(ParentHwnd, editx, "Edit", vbNullString)
a = a + 1
Loop
MsgBox a
End Sub
I believe you can also use the GetWindowLong API and the GWL_ID constant to determine your target textbox, have a search for it. BTW, the SPY++ can be found on the Microsoft Visual Studio 6.0 Tools.
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.