Reading Text From Dos Window With Vb Net
Introduction
The main purpose of this article is to explicate how to create a simple plan that volition be used to read and edit the external program text, without modifying the external plan(Another Application or Another Programme) source lawmaking. Today, I will explain:
- How to read / edit Notepad Text from our program
- 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 tin be used during the reading of the text file.
I have fastened a "SampleProgramtoRead.exe" within the "SampleProgramtoRead" folder that can be used as an external sample application to read text in our chief 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(5=vs.85).aspx
- https://msdn.microsoft.com/en-the states/library/windows/desktop/ms632644(v=vs.85).aspx
- https://msdn.microsoft.com/en-us/library/aa922085.aspx
-
https://social.msdn.microsoft.com/Forums/vstudio/en-United states/02a67f3a-4a26-4d9a-9c67-0fdff1428a66/how-do-i-use-wmgettext-in-vbnet-to-get-text-from-another-applications-window?forum=vbgeneral
-
https://kellyschronicles.wordpress.com/2008/06/23/get-window-handles-associated-with-procedure-in-vb-net/ Past Kelly's Chronicles
-
https://msdn.microsoft.com/en-us/library/windows/desktop/ms632644(v=vs.85).aspx
Special Thanks to Kelly Martens his Go Window Handles Associated With Procedure in vb.net office helps me lot to complete the project.
WM_GETTEXT
The WM_GETTEXT nessage reads the external program text.
For example:
- SendMessage(ChildHandle, WM_GETTEXT, 200, Hndl)
Hither, we use a Windows SendMessage to become the text for the external program text.
ChildHandle is the control id. Each external program command 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 retention size of the control.
WM_SETTEXT
The WM_SETTEXT is used to write to the external program text.
For case:
SendMessageSTRING(Handle, WM_SETTEXT, TexttoWritetoNotepad.Length, TexttoWritetoNotepad)
Here, nosotros use Windows SendMessageSTRING to set the text for the external program text.
Handle is the control id. Each external program control will accept a unique id that we need to provide for the appropriate command id to gear up the text for that command.
WM_SETTEXT to read the external program text.
TexttoWritetoNotepad String that writes to the external programme.
Background
The primary purpose was to develop a uncomplicated and like shooting fish in a barrel to use .NET programme that reads and edits external program text. For a long fourth dimension, I accept been looking for a program to read the external program text. Just today, I got some fourth dimension to work on it and as a upshot you lot can see my commodity at present. I am planing to add more features to this program.
Using the code
The main purpose is to make the plan very elementary and easy to use; all the functions take been well commented in the project. I have attached my sample program in this article for more than details. Here, we volition run into the procedure to read/edit External Program text.
Windows API call
- Imports System.Data
- Imports System.Runtime.InteropServices
- Imports System.Text
- Imports System.Collections.Generic
- Declare Automobile Function SendMessage Lib "user32.dll" ( ByVal hWnd Every bit IntPtr, ByVal msg Every bit Integer , _
- ByVal wParam As IntPtr, ByVal lParam Every bit IntPtr) As IntPtr
- <dllimport("user32.dll" , charset:= "CharSet.Motorcar)" 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 Automobile Role UpdateWindow Lib "user32.dll" ( ByVal hWnd As Int32) Equally Int32
- Declare Auto Function redrawwindow Lib "user32.dll" ( ByVal hWnd As Int32) As Int32
- Declare Machine Office FindWindow Lib "user32.dll" ( ByVal lpClassName As String , ByVal lpWindowName Every bit 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 Equally Int32, ByVal lParam As String ) As Int32
- Private Declare Role 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 these Windows APIs will be used in our program to read the external programme text.
- FindWindow: This will be used to find the application plan 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 programme.
- SendMessageSTRING: Used to gear up the text to the external programme.
Read Notepad Text to our program
- Public Office ReadtextfromNotepad()
- Dim Hwnd As IntPtr = SHANUEPTR.FindWindow( Nil , txtTitle.Text)
- Dim NumText As Integer
- Dim ChildHandle Equally IntPtr = SHANUEPTR.FindWindowEx(Hwnd, IntPtr.Zero, "Edit" , Null )
- Dim Hndl As IntPtr = Marshal.AllocHGlobal(200)
- NumText = SHANUEPTR.SendMessage(ChildHandle, SHANUEPTR.WM_GETTEXT, 200, Hndl)
- Text = Marshal.PtrToStringUni(Hndl)
- If Text = "AutoCompleteProxy" Then
- MessageBox.Show("Enter the valid Notepad text file Name. Annotation : the note pad text file name should be equally total text as aforementioned as titile of note pad / The Note pad should be open if its closed the text tin not exist read.This sample will load only the active notepad text file text." )
- Leave Office
- Cease If
- txtNotepadread.Text = Text
- txtNotepadWrite.Text = Text
- Finish Role
Find the running Notepad window:
- Dim Hwnd As IntPtr = SHANUEPTR.FindWindow( Nil , txtTitle.Text)
FindWindow will bank check for the currently opened application with the aforementioned title as we have given.
Note here that txtTitle.Text is the Notepad file name. We need to provide the total title, the same every bit in Notepad, for case if your Notepad championship is "SHANUEPTR.txt - Notepad" and then provide the full proper name as it is.
Utilise the following to detect the Edit control of the running Notepad:
- Dim ChildHandle Every bit IntPtr = SHANUEPTR.FindWindowEx(Hwnd, IntPtr.Zero, "Edit" , Nothing )
FindWindowEx checks for the control from the awarding 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 ship the bulletin to the external program to get the text.
ChildHandle is the external awarding command ID from where nosotros read the text nosotros need to provide the proper command id to read the text .
WM_GETTEXT is used to get the Text.
Hndl here we will take the result of the text from the external program.
Employ the post-obit to write text to Notepad from our program.
- Public Function WritetextfromNotepad()
- Dim Hwnd As IntPtr = SHANUEPTR.FindWindow( Zip , txtTitle.Text)
- Dim Handle As IntPtr = SHANUEPTR.FindWindowEx(Hwnd, IntPtr.Zero, "Edit" , Nothing )
- Dim TexttoWritetoNotepad Every bit String = txtNotepadWrite.Text.Trim()
- SHANUEPTR.SendMessageSTRING(Handle, SHANUEPTR.WM_SETTEXT, TexttoWritetoNotepad.Length, TexttoWritetoNotepad)
Finish Function
Use the following to find the running Notepad window.
- Dim Hwnd As IntPtr = SHANUEPTR.FindWindow( Nothing , txtTitle.Text)
FindWindow: volition bank check for the currently opened application with the same championship equally 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 championship is "SHANUEPTR.txt - Notepad" then provide the total name equally it is.
Utilize the following to find the Edit control of the running Notepad:
- Dim Handle As IntPtr = SHANUEPTR.FindWindowEx(Hwnd, IntPtr.Zilch, "Edit" , Cipher )
FindWindowEx is to check for the control from the application from where to read the text.
Hwnd: is the Application id or Command ID
- Dim TexttoWritetoNotepad Every bit String = txtNotepadWrite.Text.Trim()
- SHANUEPTR.SendMessageSTRING(Handle, SHANUEPTR.WM_SETTEXT, TexttoWritetoNotepad.Length, TexttoWritetoNotepad)
SendMessageSTRING is used to send the bulletin to external program to Ready 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 up the Text.
TexttoWritetoNotepad is the string to be written to Notepad.
Use the post-obit code 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 Equally Integer = 0
- Dim enumerator Equally New SHANUEPTR()
- Dim sb Equally New StringBuilder()
- For Each top Equally ApiWindow In enumerator.GetTopLevelWindows
- count = 0
- If top.MainWindowTitle = txtTitle.Text And then
- For Each child As ApiWindow In enumerator.GetChildWindows(top.hWnd)
- Dim lvi As ListViewItem
- NumText = SHANUEPTR.SendMessage(kid.hWnd, SHANUEPTR.WM_GETTEXT, 200, Hndl)
- Text = Align.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 kid
- End If
- Adjacent acme
- End Function
This is the aforementioned as in the preceding example. Hither, this office will detect all the controls and child controls of the external application and read the text of each control. The resultant controls text volition be added to the list view.
Conclusion
I promise you like my article. I will exist happy if someone benefits from my article. In a beginning 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 accept 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 manufactures.
Source: https://www.c-sharpcorner.com/UploadFile/asmabegam/external-program-text-read-using-VB-Net/
0 Response to "Reading Text From Dos Window With Vb Net"
Enregistrer un commentaire