Read External Program Text Using VB.Net

External Program

Introduction

The main purpose of this article is to explain how to create a simple program that will be used to read and edit the external program text, without modifying the external program(Another Application or Another Program) source code. Today, I will explain:

  1. How to read / edit Notepad Text from our program
  2. How to read external program text from our program

In my Zip file, I have an attached sample program with the solution name "SHANU_EPTR_V1.0" developed using VB.NET to read the external program and Notepad text.

I have attached a "SHANUEPTR.txt" text file that can be used during the reading of the text file.

text file

I have attached a "SampleProgramtoRead.exe" inside the "SampleProgramtoRead" folder that can be used as an external sample application to read text in our main program.

To read the external program, we need to use Windows WM_GETTEXT and WM_SETTEXT.

Reference website:

  • https://msdn.microsoft.com/en-us/library/windows/desktop/ms633500(v=vs.85).aspx Jump
  • https://msdn.microsoft.com/en-us/library/windows/desktop/ms632644(v=vs.85).aspx Jump
  • https://msdn.microsoft.com/en-us/library/aa922085.aspx Jump
  • https://social.msdn.microsoft.com/Forums/vstudio/en-US/02a67f3a-4a26-4d9a-9c67-0fdff1428a66/how-do-i-use-wmgettext-in-vbnet-to-get-text-from-another-applications-window?forum=vbgeneral Jump

  • https://kellyschronicles.wordpress.com/2008/06/23/get-window-handles-associated-with-process-in-vb-net/ Jump By Kelly's Chronicles

  • https://msdn.microsoft.com/en-us/library/windows/desktop/ms632644(v=vs.85).aspx Jump

Special Thanks to Kelly Martens Jump his Get Window Handles Associated With Process in vb.net function helps me lot to complete the project.

WM_GETTEXT

The WM_GETTEXT nessage reads the external program text.

For example:
  1. SendMessage(ChildHandle, WM_GETTEXT, 200, Hndl) 

Here, we use a Windows SendMessage to get the text for the external program text.

ChildHandle is the control id. Each external program control will have a unique id that we need to provide to the appropriate control id in order 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 the external program text.

Handle is the control id. Each external program control will have a unique id that we need to provide for the appropriate control id to set the text for that control.

WM_SETTEXT to read the external program text.

TexttoWritetoNotepad String that writes to the external program.

Background

The main purpose was to develop a simple and easy to use .NET program that reads and edits external program text. For a long time, I have been looking for a program to read the external program text. But today, I got some time to work on it and as a result you can see my article now. I am planing to add more features to this program.

Using the code

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. Here, we will see the procedure to read/edit External Program text.

Windows API call

  1. 'Imports   
  2. Imports System.Data  
  3. Imports System.Runtime.InteropServices  
  4. Imports System.Text  
  5. Imports System.Collections.Generic  
  6.   
  7.   Declare Auto Function SendMessage Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal msg As Integer, _  
  8.     ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr  
  9.     <dllimport("user32.dll", charset:="CharSet.Auto)" setlasterror:="True,"> _  
  10.     Public Shared Function FindWindowEx(ByVal parentHandle As IntPtr, _  
  11.                                      ByVal childAfter As IntPtr, _  
  12.                                     ByVal lclassName As String, _  
  13.                                      ByVal windowTitle As StringAs IntPtr  
  14.     End Function  
  15.     Declare Auto Function UpdateWindow Lib "user32.dll" (ByVal hWnd As Int32) As Int32  
  16.     Declare Auto Function redrawwindow Lib "user32.dll" (ByVal hWnd As Int32) As Int32  
  17.     Declare Auto Function FindWindow Lib "user32.dll" (ByVal lpClassName As StringByVal lpWindowName As StringAs IntPtr  
  18.   
  19.   
  20.     Public Const WM_SETTEXT = &HC  
  21.     Public Declare Function SendMessageSTRING Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As Int32, _  
  22.    ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As StringAs Int32  
  23.   
  24.     '    Private Declare Function FindWindow Lib "user32.dll" Alias _  
  25.     '"FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Int32  
  26.     Private Declare Function FindWindowEx Lib "user32.dll" Alias _  
  27.   "FindWindowExA" (ByVal hWnd1 As Int32, ByVal hWnd2 As Int32, ByVal lpsz1 As String, _  
  28.   ByVal lpsz2 As StringAs Int32  
  29. </dllimport("user32.dll",> 

All these Windows APIs 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
  1. Public Function ReadtextfromNotepad()  
  2.        'Find the running notepad window  
  3.        Dim Hwnd As IntPtr = SHANUEPTR.FindWindow(Nothing, txtTitle.Text)  
  4.        Dim NumText As Integer  
  5.        'Find the Edit control of the Running Notepad  
  6.        Dim ChildHandle As IntPtr = SHANUEPTR.FindWindowEx(Hwnd, IntPtr.Zero, "Edit"Nothing)  
  7.   
  8.        'Alloc memory for the buffer that recieves the text  
  9.        Dim Hndl As IntPtr = Marshal.AllocHGlobal(200)  
  10.   
  11.        'Send The WM_GETTEXT Message  
  12.        NumText = SHANUEPTR.SendMessage(ChildHandle, SHANUEPTR.WM_GETTEXT, 200, Hndl)  
  13.   
  14.        'copy the characters from the unmanaged memory to a managed string  
  15.        Text = Marshal.PtrToStringUni(Hndl)  
  16.        If Text = "AutoCompleteProxy" Then  
  17.            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.")  
  18.            Exit Function  
  19.   
  20.        End If  
  21.        'Display the string using a label  
  22.        txtNotepadread.Text = Text  
  23.        txtNotepadWrite.Text = Text  
  24.    End Function 

Find the running Notepad window:

  1. 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 that 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:

  1. 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

  1. 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.

Use the following to write text to Notepad from our program.

  1. Public Function WritetextfromNotepad()  
  2.  Dim Hwnd As IntPtr = SHANUEPTR.FindWindow(Nothing, txtTitle.Text)  
  3.  Dim Handle As IntPtr = SHANUEPTR.FindWindowEx(Hwnd, IntPtr.Zero, "Edit"Nothing)  
  4.  Dim TexttoWritetoNotepad As String = txtNotepadWrite.Text.Trim()  
  5.  SHANUEPTR.SendMessageSTRING(Handle, SHANUEPTR.WM_SETTEXT, TexttoWritetoNotepad.Length, TexttoWritetoNotepad) 
    End
     Function 

Use the following to find the running Notepad window.

  1. 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:

  1. 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

  1. Dim TexttoWritetoNotepad As String = txtNotepadWrite.Text.Trim()  
  2. 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.

Use the following code to read text from the external application:

  1. Public Function ReadtextfromApplication()  
  2.         ListView1.Items.Clear()  
  3.         Dim Hndl As IntPtr = Marshal.AllocHGlobal(200)  
  4.         Dim NumText As Integer       
  5.         Static count As Integer = 0  
  6.         Dim enumerator As New SHANUEPTR()  
  7.         Dim sb As New StringBuilder()  
  8.         For Each top As ApiWindow In enumerator.GetTopLevelWindows  
  9.             count = 0  
  10.   
  11.             'If top.MainWindowTitle = "E2Max-MTMS - [공지사항]" Then  
  12.             If top.MainWindowTitle = txtTitle.Text Then  
  13.                 '  MessageBox.Show(top.MainWindowTitle)  
  14.   
  15.                 For Each child As ApiWindow In enumerator.GetChildWindows(top.hWnd)  
  16.                     '  Console.WriteLine(child.MainWindowTitle)  
  17.   
  18.                     Dim lvi As ListViewItem   
  19.                     NumText = SHANUEPTR.SendMessage(child.hWnd, SHANUEPTR.WM_GETTEXT, 200, Hndl)  
  20.                     Text = Marshal.PtrToStringUni(Hndl)  
  21.                     lvi = New ListViewItem(child.ClassName.ToString())  
  22.                     lvi.SubItems.Add(child.hWnd.ToString())  
  23.                     lvi.SubItems.Add(child.MainWindowTitle.ToString())  
  24.                     ListView1.Items.Add(lvi)                                         
  25.                 Next child  
  26.             End If  
  27.         Next top  
  28.     End Function 

This is the same as in the preceding 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.

Conclusion

I hope you like my article. I will be happy if someone benefits from my article. In a first step, I have created this simple program that will read and edit Notepad text. For the external application for now, I have shown how to read the text from a label, Text box, Combo box, Button controls; and for the edit, I have made a sample only for the edit box like a Text box and a combo box. Controls like Treeview, Menu, ListView, grid text read will be updated in my next articles.


Similar Articles