Get Text From Specific Textboxes From External Application - Visual Basic .Net

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
<span style="font-family:Arial,Liberation Sans,DejaVu Sans,sans-serif; font-size:14px I can get text from external application text box but now I want to get text from my desired text box from external application. See Image Below, I want to get text
from 2nd text box.
<span style="font-family:Arial,Liberation Sans,DejaVu Sans,sans-serif; font-size:14px <img alt="" src="http://i.stack.imgur.com/U5aNi.jpg
<span style="font-family:Arial,Liberation Sans,DejaVu Sans,sans-serif; font-size:14px
<span style="font-family:Arial,Liberation Sans,DejaVu Sans,sans-serif; font-size:14px The Below Code Return The First Text Box Value Only.
<pre class="prettyprint 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", 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 Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Find the running notepad window
Dim Hwnd As IntPtr = FindWindow(Nothing, TextBox1.Text) Textbox1.text = my application or window name / title.

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[/code]
<br/>
<span style="font-family:Arial,Liberation Sans,DejaVu Sans,sans-serif; font-size:14px I have asked this same question on an other site but got no response, thats why I am posting the same question on MSDN.<span style="font-family:Arial,Liberation Sans,DejaVu Sans,sans-serif; font-size:14px

View the full article
 
Back
Top