CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Join Date
    Jul 2008
    Posts
    45

    Reading one exe from another

    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.

    Thanks in Advance
    Chris

  2. #2
    Join Date
    Jan 2006
    Location
    Pearl of the orient
    Posts
    304

    Re: Reading one exe from another

    Have a search for FindWindow.

  3. #3
    Join Date
    Jul 2008
    Posts
    45

    Re: Reading one exe from another

    How would i do that? I'm newbie, sort off.
    Thanks in advance
    Chris

  4. #4
    Join Date
    Jan 2006
    Location
    Pearl of the orient
    Posts
    304

    Re: Reading one exe from another

    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

  5. #5
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: Reading one exe from another

    FindWindow will get the main application's Window handle ( text etc. )
    FindWindowEx will get will get all the child windows inside that window.

    I'd suggest you get a program such as Spy++ which will enable you to get the names of the child windows, so that you can make use of that info.

  6. #6
    Join Date
    Jul 2008
    Posts
    45

    Re: Reading one exe from another

    Quote Originally Posted by dee-u View Post
    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

  7. #7
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Reading one exe from another

    That's what SPY++ does. You click on a window or control, and it's handle is returned
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  8. #8
    Join Date
    Jul 2008
    Posts
    45

    Re: Reading one exe from another

    SPY++ ???? How do i get that.

  9. #9
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Reading one exe from another

    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?
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  10. #10
    Join Date
    Jul 2008
    Posts
    45

    Re: Reading one exe from another

    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.

  11. #11
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Reading one exe from another

    one last time:
    Attached Images Attached Images  
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  12. #12
    Join Date
    Jan 2006
    Location
    Pearl of the orient
    Posts
    304

    Re: Reading one exe from another

    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

  13. #13
    Join Date
    Jan 2006
    Location
    Pearl of the orient
    Posts
    304

    Re: Reading one exe from another

    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.

  14. #14
    Join Date
    Jul 2008
    Posts
    45

    Re: Reading one exe from another

    hi, again,
    the box i want to read is a label, is there a way of reading it as the spy++ only detects its frame

    Thanks Chris

  15. #15
    Join Date
    Jan 2006
    Location
    Pearl of the orient
    Posts
    304

    Re: Reading one exe from another

    A label has no handle so your luck seem to be diminishing. =(

Page 1 of 2 12 LastLast

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