VB.NET: External Program Text Read
Introduction
The main aim of this article is to explain you all how to create simple program which will be used to read and edit the external program text, without modifying the external program (Another Application or Another Program) source code.
How to Read / Edit Notepad Text from our program
https://code.msdn.microsoft.com/site/view/file/145771/1/2.gif
As we can see here I have opened a notepad named as “SHANUEPTR.txt – 메모장” .In our External program Text Read program I will give the Note pad file name with title displayed. Since we are going to Read and Edit Notepad Text, Here we will select radio option as Notepad and click “Load External Program Button”.
Once we clicked on the button we can see the Notepad text will be displayed in both Text Box. The First Textbox is to display the notepad text .The second Textbox is to edit the notepad text. After we have edited the Notepad text from our program we can click on “Write to Notepad button” to updated the text on Notepad.
Note: The text file should be opened. In my attached zip folder you can find the folder name as**“SampleProgramtoRead”**.Inside this folder you can find my sample text file which need to be used for Read/Write. You can also use any text file but you have to give the same title text of each Notepad to read.
How to Read External Program Text from our program
https://code.msdn.microsoft.com/site/view/file/145774/1/1.gif
Same like reading Notepad text now let’s see how to Read and Edit External program text from our program. We need to give the same title of our other external application text. Here for example we can see there is another program running and the program title text is **“SHANU_SAMPLE_APPLICATION”.**We will give this title to our program and select External program radio option and click on Load External program. We can see all controls of the form with control Type, control ID and Control Text will be listed. We can also change of textbox text of external program from our own program.
Note: The External program should be opened and running.In my attached zip folder you can find the folder name as**“SampleProgramtoRead”**.Inside this folder you can find my sample external program exe as “SampleProgramtoRead.exe” file which need to be used for Read/Write. You can also use any program of yours but sure to give the exact title of that program to read.
Building the Sample
To read the External program we need to use Windows WM_GETTEXT and WM_SETTEXT
WM_GETTEXT
The **WM_GETTEXT **is used to read the external program text .
For Example :
SendMessage(ChildHandle, WM_GETTEXT, 200, Hndl)
Here we use Windows SendMessage to get the text for external program text.
ChildHandle is the control id .Each external program controls will have unique id we need to give the appropriate control id to get the text from that control.
WM_GETTEXT to read the external program text.
Hndl is the buffer memory size of the control.
WM_SETTEXT
The **WM_SETTEXT **is used to Write to the external program text .
For Example :
SendMessageSTRING(Handle, WM_SETTEXT, TexttoWritetoNotepad.Length, TexttoWritetoNotepad)
Here we use Windows SendMessageSTRING to set the text for external program text.
Handle is the control id .Each external program controls will have unique id we need to give the appropriate control id to set the text for that control..
WM_SETTEXT to read the external program text.
TexttoWritetoNotepad String which need to be write in external program.
Reference website:
https://msdn.microsoft.com/en-us/library/windows/desktop/ms633500(v=vs.85).aspx
https://msdn.microsoft.com/en-us/library/windows/desktop/ms632644(v=vs.85).aspx
https://kellyschronicles.wordpress.com/2008/06/23/get-window-handles-associated-with-process-in-vb-net/ By Kelly's Chronicles
https://msdn.microsoft.com/en-us/library/windows/desktop/ms632644(v=vs.85).aspx
Special Thanks to Kelly Martens his Get Window Handles Associated With Process in vb.net function helps me lot to complete the project.
Description
The main purpose is to make the program very simple and easy to use; all the functions have been well commented in the project. I have attached my sample program in this article for more details.
Windows API call
'Imports
Imports System.Data
Imports System.Runtime.InteropServices
Imports System.Text
Imports System.Collections.Generic
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", charset:="CharSet.Auto)" setlasterror:="True,"> _
Public 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 UpdateWindow Lib "user32.dll" (ByVal hWnd As Int32) As Int32
Declare Auto Function redrawwindow Lib "user32.dll" (ByVal hWnd As Int32) As Int32
Declare Auto Function FindWindow Lib "user32.dll" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
Public Const WM_SETTEXT = &HC
Public Declare Function SendMessageSTRING Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As Int32, _
ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As String) As Int32
' Private Declare Function FindWindow Lib "user32.dll" Alias _
'"FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Int32
Private Declare Function FindWindowEx Lib "user32.dll" Alias _
"FindWindowExA" (ByVal hWnd1 As Int32, ByVal hWnd2 As Int32, ByVal lpsz1 As String, _
ByVal lpsz2 As String) As Int32
</dllimport("user32.dll",>
All this Windows API will be used in our program to read the external program text.
- FindWindow: This will be used to find the application program that is currently opened.
- FindWindowEx: This wil be used to find the control of the application from where the text will be read or written.
- SendMessage: Used to get the text from the external program.
- SendMessageSTRING: Used to set the text to the external program.
**Read Notepad Text to our program **
Public Function ReadtextfromNotepad()
'Find the running notepad window
Dim Hwnd As IntPtr = SHANUEPTR.FindWindow(Nothing, txtTitle.Text)
Dim NumText As Integer
'Find the Edit control of the Running Notepad
Dim ChildHandle As IntPtr = SHANUEPTR.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 = SHANUEPTR.SendMessage(ChildHandle, SHANUEPTR.WM_GETTEXT, 200, Hndl)
'copy the characters from the unmanaged memory to a managed string
Text = Marshal.PtrToStringUni(Hndl)
If Text = "AutoCompleteProxy" Then
MessageBox.Show("Enter the valid Notepad text file Name. Note : the note pad text file name should be as full text as same as titile of note pad / The Note pad should be open if its closed the text can not be read.This sample will load only the active notepad text file text.")
Exit Function
End If
'Display the string using a label
txtNotepadread.Text = Text
txtNotepadWrite.Text = Text
End Function
Find the running Notepad window:
Dim Hwnd As IntPtr = SHANUEPTR.FindWindow(Nothing, txtTitle.Text)
FindWindow: will check for the currently opened application with the same title as we have given.
Note here txtTitle.Text is the Notepad file name. We need to provide the full title, the same as in Notepad, for example if your Notepad title is "SHANUEPTR.txt - Notepad" then provide the full name as it is.
Use the following to find the Edit control of the running Notepad:
Dim ChildHandle As IntPtr = SHANUEPTR.FindWindowEx(Hwnd, IntPtr.Zero, "Edit", Nothing)
FindWindowEx checks for the control from the application from where to read the text.
Hwnd: is the Application id or Control ID
NumText = SHANUEPTR.SendMessage(ChildHandle, SHANUEPTR.WM_GETTEXT, 200, Hndl)
SendMessage: is used to send the message to the external program to get the text.
ChildHandle: is the external application control ID from where we read the text we need to provide the proper control id to read the text .
WM_GETTEXT: is used to get the Text.
Hndl: here we will have the result of the text from the external program.
Write Text to Notepad from our program
** **Use the following to write text to Notepad from our program:
Public Function WritetextfromNotepad()
Dim Hwnd As IntPtr = SHANUEPTR.FindWindow(Nothing, txtTitle.Text)
Dim Handle As IntPtr = SHANUEPTR.FindWindowEx(Hwnd, IntPtr.Zero, "Edit", Nothing)
Dim TexttoWritetoNotepad As String = txtNotepadWrite.Text.Trim()
SHANUEPTR.SendMessageSTRING(Handle, SHANUEPTR.WM_SETTEXT, TexttoWritetoNotepad.Length, TexttoWritetoNotepad)
End Function
Use the following to find the running Notepad window:
Dim Hwnd As IntPtr = SHANUEPTR.FindWindow(Nothing, txtTitle.Text)
FindWindow: will check for the currently opened application with the same title as we gave.
Note that here txtTitle.Text is the Notepad file name. We need to provide the full title, the same as in Notepad. For example if your notepad title is "SHANUEPTR.txt - Notepad" then provide the full name as it is.
Use the following to find the Edit control of the running Notepad:
Dim Handle As IntPtr = SHANUEPTR.FindWindowEx(Hwnd, IntPtr.Zero, "Edit", Nothing)
FindWindowEx is to check for the control from the application from where to read the text.
Hwnd: is the Application id or Control ID
- Dim TexttoWritetoNotepad As String = txtNotepadWrite.Text.Trim()
- SHANUEPTR.SendMessageSTRING(Handle, SHANUEPTR.WM_SETTEXT, TexttoWritetoNotepad.Length, TexttoWritetoNotepad)
SendMessageSTRING is used to send the message to external program to SET the text.
Handle is the external application control ID from where we read the text we need to provide the proper control id to read the text.
WM_SETTEXT is used to SET the Text.
TexttoWritetoNotepad is the string to be written to Notepad.
Read Text from External Application
Use the following to read text from the external application:
public Function ReadtextfromApplication()
ListView1.Items.Clear()
Dim Hndl As IntPtr = Marshal.AllocHGlobal(200)
Dim NumText As Integer
Static count As Integer = 0
Dim enumerator As New SHANUEPTR()
Dim sb As New StringBuilder()
For Each top As ApiWindow In enumerator.GetTopLevelWindows
count = 0
'If top.MainWindowTitle = "E2Max-MTMS - [공지사í•]" Then
If top.MainWindowTitle = txtTitle.Text Then
' MessageBox.Show(top.MainWindowTitle)
For Each child As ApiWindow In enumerator.GetChildWindows(top.hWnd)
' Console.WriteLine(child.MainWindowTitle)
Dim lvi As ListViewItem
NumText = SHANUEPTR.SendMessage(child.hWnd, SHANUEPTR.WM_GETTEXT, 200, Hndl)
Text = Marshal.PtrToStringUni(Hndl)
lvi = New ListViewItem(child.ClassName.ToString())
lvi.SubItems.Add(child.hWnd.ToString())
lvi.SubItems.Add(child.MainWindowTitle.ToString())
ListView1.Items.Add(lvi)
Next child
End If
Next top
End Function
This is the same as in the receding example. Here this function will find all the controls and child controls of the external application and read the text of each control. The resultant controls text will be added to the list view.
Note: You can download the Source Code from the link ** Source Code Download Link**