Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have been reading a lot about gettext and getwindowtext. I can get the windows name but how can I access a control in another application and extract text from a textbox?
Posted

Use Spy ++[^] to figure out how to get the handle of the ie control - for IE 9 it will have a class named "Internet Explorer_Server" - that's the window class (as registered with RegisterClassEx[^]), not the .Net class name - and it's not the top level window for internet explorer.

I'd be surprised if WM_GETTEXT works with the ie control, but simulating
ctrl+a
ctrl+c
using the SendKeys class[^] will put the contents of the control on the clipboard.

Best regards
Espen Harlinn
 
Share this answer
 
v2
Comments
Nish Nishant 31-Oct-11 15:51pm    
Voted 5. Good alternate technique (Ctrl-A/C).
Espen Harlinn 31-Oct-11 15:59pm    
Thank you, Nish :) Does WM_GETTEXT work with the IE control?
Nish Nishant 31-Oct-11 16:01pm    
No, it will not. Since those are not HWND-controls. The OP must have been mistaken.
wiswalld 31-Oct-11 15:55pm    
I get a handle like --- 000D0AC8

That make sense? And if it does then I get nothing
Espen Harlinn 31-Oct-11 16:00pm    
Did you figure out how to get at the correct handle? For IE 9 it will have a class named "Internet Explorer_Server" - that's the window class, not the .Net class name.
This simple post[^] might help you out.
 
Share this answer
 
From MSDN:


To retrieve the text of a control in another process, send a WM_GETTEXT message directly instead of calling GetWindowText.
 
Share this answer
 
Comments
wiswalld 31-Oct-11 14:23pm    
The code I posted incorrectly works with notepad but I was trying it with Access (the program I will use it on is at work but wanted to try access) I get nothing. I used Spy++ to get the name of the handle as "Form1" but not sure how to get to the child.
I have used this for Notepad but have not been able to get it to work for my app which opens in internet explorer. Not my choice I am just trying to read data from there.

VB
Imports System.Runtime.InteropServices

Public Class Form1
    Private Const WM_GETTEXT As Integer = &HD
    Declare Auto Function SendMessage Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal msg As Integer, _
    ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
    <dllimport("user32.dll",> _
    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 Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        
    End Sub


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'Find the running notepad window
        Dim Hwnd As IntPtr = FindWindow(Nothing, "Untitled - Notepad")

        'Alloc memory for the buffer that recieves the text
        Dim Handle As IntPtr = Marshal.AllocHGlobal(100)

        'send WM_GWTTEXT message to the notepad window
        Dim NumText As Integer = SendMessage(Hwnd, WM_GETTEXT, 50, Handle)

        'copy the characters from the unmanaged memory to a managed string
        Dim Text As String = Marshal.PtrToStringUni(Handle)

        'Display the string using a label
        Label1.Text = Text

        'Find the Edit control of the Running Notepad
        Dim ChildHandle As IntPtr = FindWindowEx(Hwnd, IntPtr.Zero, "Edit", Nothing)

        'Alloc memory for the buffer that recieves the text
        Dim Hndl As IntPtr = Marshal.AllocHGlobal(200)

        'Send The WM_GETTEXT Message
        NumText = SendMessage(ChildHandle, WM_GETTEXT, 200, Hndl)

        'copy the characters from the unmanaged memory to a managed string
        Text = Marshal.PtrToStringUni(Hndl)

        'Display the string using a label
        Label2.Text = Text
    End Sub
End Class
 
Share this answer
 
v2
I git it with

SQL
For Each ie As InternetExplorer In New ShellWindows()
           If ie.LocationURL.StartsWith("http://spwssjs1:8082/sjs/incident.html?_i_id=") Then

               'Get last name and first name
               Me.CaseNumberTextBox.Text = ie.Document.All("incident._i_case").Value
               Me.TodaysDateComboBox.Text = ie.Document.All("_i_report_date_date").Value
               Me.TimeTextBox.Text = ie.Document.All("_i_report_date_time").Value
               Me.LocationTextBox.Text = ie.Document.All("address._add_street").Value & "" & " " & ie.Document.All("address._add_apartment").Value & " " & ie.Document.All("address._add_city").Value


               MsgBox("Make sure the address is complete")
               Me.DefendantAddressTextBox.Select(0, DefendantAddressTextBox.Text.Length)

           End If
       Next
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900