Clear text from text box in a Windows application

Is it possible to clear text from a text field contained in a separate executable file? I have an application that has a debug window. The debug window generates a verbose log. However, the log is never saved anywhere and can only be viewed in the application. If the application throws an exception, I would like to write to myself, knowing that an exception was thrown so that I can jump and check the situation. There is also a button for copying a text field, so I thought about using Spy ++ to get information about the command. However, I do not know where to go from there. Any pointers are welcome.

I would prefer to use C # in .NET, but if I need to use C ++, I will.

UPDATE

Based on the comments, I tried to do the following:

Private Declare Function GETWINDOWTEXT Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Integer, ByVal lpString As String, ByVal cch As Integer) As Integer
Private Declare Function SetForegroundWindow Lib "user32.dll" (ByVal hwnd As Integer) As Integer
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindow As String) As IntPtr
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Integer, ByVal hWnd2 As Integer, ByVal lpsz1 As String, ByVal lpsz2 As String) As Integer
Private Declare Ansi Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As String) As Integer
Private Const WM_GETTEXT As Short = &HDS
Private Const WM_GETTEXTLENGTH As Short = &HES

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Dim hwnd As Integer = FindWindowEx(0, 0, "MyAppForm", "Hello World")

    If Not hwnd = 0 Then
        SetForegroundWindow(hwnd)

        'Dim LabelEx As Integer = FindWindowEx()
        Dim TextBoxEx As Integer = FindWindowEx(hwnd, 0, "MyAppTextBox", vbNullString)
        Dim txtLength As Long = SendMessage(TextBoxEx, WM_GETTEXTLENGTH, CInt(0), CInt(0)) + 1
        Dim txtBuff As String = Space(txtLength)
        Dim txtValue As Long = SendMessage(TextBoxEx, WM_GETTEXT, txtLength, txtBuff)

        MsgBox(txtBuff)
    End If
End Sub

However, I cannot find the handle of the text field control. When I list all the windows, I see only one TextBox, but I see the parent several times during the listing. How can I get pointers to controls inside a window?

UPDATE 2:

I downloaded a sample Windows application to show the type of application I'm trying to access. I am trying to get the values โ€‹โ€‹of both labels in addition to the text box. The text box is the most important. Win example application here: http://www.mediafire.com/file/172r2xapj7p4f2f/StatusSimulator.zip

+5
source share
3

. System.Windows.Automation. , :

AutomationElement.FromHandle(hwnd)
                 .GetCurrentPropertyValue(ValuePattern.ValueProperty) as string;

( , #, #, VB.)

+3

WM_GETTEXT, , . , , , , .

- ( ), , , DLL-. SetWindowsHook DLL. DLL , , . , .

. , . , , .

+2

P/Invoke GetWindowText . P/Invoke String , - . lpString StringBuffer, .

Note that you will also need to set the bandwidth of the StringBuffer to 1000 before passing it to GetWindowText, because this requires passing the allocated buffer, this will not determine the size of the StringBuffer itself.

See definition and example and pinvoke.net .

<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function GetWindowText(ByVal hwnd As IntPtr, ByVal lpString As StringBuilder, ByVal cch As Integer) As Integer
End Function

Private Declare Function SetForegroundWindow Lib "user32.dll" (ByVal hwnd As Integer) 

As Integer
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindow As String) As IntPtr
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Integer, ByVal hWnd2 As Integer, ByVal lpsz1 As String, ByVal lpsz2 As String) As Integer
Private Declare Ansi Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As String) As Integer
Private Const WM_GETTEXT As Short = &HDS
Private Const WM_GETTEXTLENGTH As Short = &HES

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Dim hwnd As Integer = FindWindowEx(0, 0, "MyAppForm", "Hello World")

    If Not hwnd = 0 Then
        SetForegroundWindow(hwnd)

        'Dim LabelEx As Integer = FindWindowEx()
        Dim TextBoxEx As Integer = FindWindowEx(hwnd, 0, "MyAppTextBox", vbNullString)
        Dim txtLength As Long = SendMessage(TextBoxEx, WM_GETTEXTLENGTH, CInt(0), CInt(0)) + 1
        Dim txtBuff As New System.Text.StringBuilder(txtLength + 1)
        GetWindowText(hWnd, txtBuff , txtBuff .Capacity)
        Dim txtValue As Long = SendMessage(TextBoxEx, WM_GETTEXT, txtLength, txtBuff)
        MsgBox(txtBuff.ToString())
    End If
End Sub
+1
source

All Articles