Link to home
Start Free TrialLog in
Avatar of W Coder
W Coder

asked on

Get text from child window in another program (VB.net)

I'm writing a simple app in VB.Net to carry out a task after a third party application has downloaded information from the internet.

When the third party application is not connected to the internet, the status bar at the bottom of its window is blank. When the third party application is connected to the internet and is downloading data, text appears in the status bar.

Using the Microsoft utlitity Inspect Objects (Inspect.exe), I can see that the program's main window has four children: a pane, a menu bar, a status bar, and the title bar of the main window.

I have been able to get the hWnd of the status bar using this code:

Imports System.Runtime.InteropServices
Imports System.Text

Public Class Form1
    <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
    Private Shared Function FindWindowEx(ByVal parentHandle As IntPtr, _
                                         ByVal childAfter As IntPtr, _
                                         ByVal lclassName As String, _
                                         ByVal windowTitle As String) As IntPtr
    End Function
    Declare Auto Function FindWindow Lib "user32.dll" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim ParentHWND As IntPtr = FindWindow("WindowsForms10.Window.8.app.0.378734a", "Control Center - W1")
        Dim ChildHWND As IntPtr = IntPtr.Zero
        Const ChildClassName As String = "WindowsForms10.Window.8.app.0.378734a"
        Dim StatusStripHWND As IntPtr = IntPtr.Zero

        ChildHWND = FindWindowEx(ParentHWND, ChildHWND, ChildClassName, String.Empty)
        Label_FirstChild.Text = ChildHWND
        ChildHWND = FindWindowEx(ParentHWND, ChildHWND, ChildClassName, String.Empty)
        Label_SecondChild.Text = ChildHWND
        ChildHWND = FindWindowEx(ParentHWND, ChildHWND, ChildClassName, String.Empty)
        Label_ThirdChild.Text = ChildHWND

        StatusStripHWND = ChildHWND
    End Sub
End Class

Open in new window


Inspect.exe shows that the status bar has three children, all of which are text controls (ControlType: UIA_TextControlTypeId (0xC364)).

My question is: How can I retreive the text currently displayed on the second text control in the status bar (highlighted in blue on the attached screenshot).

User generated image
Avatar of ste5an
ste5an
Flag of Germany image

Depends on the control. Whether the text is displayed in a windo or not. I would use Get Window and GetWindowText or SendMessage() with WM_GETTEXT.
Avatar of W Coder
W Coder

ASKER

Thanks ste5an for your reply.  As far as I understand, GetWindowText cannot be used to get text on a control in another app.

I have tried the following using WM_GETTEXT, but it unfortunately returns nothing in the message box.

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        GetText(StatusBar_Hwnd)
    End Sub

    Private Sub GetText(ByVal WindowHandle As IntPtr)
        Dim TextLen As Integer = SendMessage(WindowHandle, WM_GETTEXTLENGTH, 0, 0) + 1
        Dim Buffer As String = New String(" "c, TextLen)
        SendMessageByString(WindowHandle, WM_GETTEXT, TextLen, Buffer)
        MsgBox(Buffer)
    End Sub

Open in new window


The text I am trying the retrieve appears in the 'Name' field in the right-hand pane of Inspect.exe (see attached screenshot) Could the fact that the text appears in the Name field offer any clue as to how to retrieve it?  

What I'm trying to say is that if Inspect.exe can see the text, it should be retrievable somehow :)
ASKER CERTIFIED SOLUTION
Avatar of W Coder
W Coder

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of W Coder

ASKER

It took me literally days of effort trying to get a solution that would work.  Thanks to TnTinMN at VBForums.com and IronRazer at DreamInCode.net, I was able to get things to work.  Although I'd prefer not to mark my own comment as a solution, I'm sure the info would benefit others if they are faced with a similar problem.