Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET
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 31-Oct-11 8:17am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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
  Permalink  
v2
Comments
Nishant Sivakumar at 31-Oct-11 15:51pm
   
Voted 5. Good alternate technique (Ctrl-A/C).
Espen Harlinn at 31-Oct-11 15:59pm
   
Thank you, Nish :) Does WM_GETTEXT work with the IE control?
Nishant Sivakumar at 31-Oct-11 16:01pm
   
No, it will not. Since those are not HWND-controls. The OP must have been mistaken.
wiswalld at 31-Oct-11 15:55pm
   
I get a handle like --- 000D0AC8
 
That make sense? And if it does then I get nothing
Espen Harlinn at 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.
Amir Mahfoozi at 2-Nov-11 1:09am
   
+5
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

This simple post[^] might help you out.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

From MSDN:
 

To retrieve the text of a control in another process, send a WM_GETTEXT message directly instead of calling GetWindowText.
  Permalink  
Comments
wiswalld at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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.
 
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
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

I git it with
 
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
  Permalink  

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



Advertise | Privacy | Mobile
Web01 | 2.8.1411022.1 | Last Updated 4 Nov 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100