Reading Text From Dos Window With Vb Net

External Program

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:

  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 tin be used during the reading of the text file.

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 Jump
  • https://msdn.microsoft.com/en-the states/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-United states/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-procedure-in-vb-net/ Jump Past 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 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:

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

  1. Imports  System.Data
  2. Imports  System.Runtime.InteropServices
  3. Imports  System.Text
  4. Imports  System.Collections.Generic
  5. Declare Automobile Function  SendMessage Lib "user32.dll"  ( ByVal  hWnd Every bit  IntPtr, ByVal  msg Every bit Integer , _
  6. ByVal  wParam As  IntPtr, ByVal  lParam Every bit  IntPtr) As  IntPtr
  7.     <dllimport("user32.dll" , charset:= "CharSet.Motorcar)"  setlasterror:= "True," > _
  8. Public Shared Function  FindWindowEx( ByVal  parentHandle As  IntPtr, _
  9. ByVal  childAfter As  IntPtr, _
  10. ByVal  lclassName As String , _
  11. ByVal  windowTitle As String ) As  IntPtr
  12. End Function
  13. Declare Automobile Role  UpdateWindow Lib "user32.dll"  ( ByVal  hWnd As  Int32) Equally  Int32
  14. Declare Auto Function  redrawwindow Lib "user32.dll"  ( ByVal  hWnd As  Int32) As  Int32
  15. Declare Machine Office  FindWindow Lib "user32.dll"  ( ByVal  lpClassName As String , ByVal  lpWindowName Every bit String ) As  IntPtr
  16. Public Const  WM_SETTEXT = &HC
  17. Public Declare Function  SendMessageSTRING Lib "user32.dll" Alias "SendMessageA"  ( ByVal  hwnd As  Int32, _
  18. ByVal  wMsg As  Int32, ByVal  wParam Equally  Int32, ByVal  lParam As String ) As  Int32
  19. Private Declare Role  FindWindowEx Lib "user32.dll" Alias  _
  20. "FindWindowExA"  ( ByVal  hWnd1 As  Int32, ByVal  hWnd2 As  Int32, ByVal  lpsz1 As String , _
  21. ByVal  lpsz2 As String ) As  Int32
  22. </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

  1. Public Office  ReadtextfromNotepad()
  2. Dim  Hwnd As  IntPtr = SHANUEPTR.FindWindow( Nil , txtTitle.Text)
  3. Dim  NumText As Integer
  4. Dim  ChildHandle Equally  IntPtr = SHANUEPTR.FindWindowEx(Hwnd, IntPtr.Zero, "Edit" , Null )
  5. Dim  Hndl As  IntPtr = Marshal.AllocHGlobal(200)
  6.        NumText = SHANUEPTR.SendMessage(ChildHandle, SHANUEPTR.WM_GETTEXT, 200, Hndl)
  7.        Text = Marshal.PtrToStringUni(Hndl)
  8. If  Text = "AutoCompleteProxy" Then
  9.            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." )
  10. Leave Office
  11. Cease If
  12.        txtNotepadread.Text = Text
  13.        txtNotepadWrite.Text = Text
  14. Finish Role

Find the running Notepad window:

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

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

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

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

Use the following to find the running Notepad window.

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

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

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

  1. Public Function  ReadtextfromApplication()
  2.         ListView1.Items.Clear()
  3. Dim  Hndl As  IntPtr = Marshal.AllocHGlobal(200)
  4. Dim  NumText As Integer
  5. Static  count Equally Integer  = 0
  6. Dim  enumerator Equally New  SHANUEPTR()
  7. Dim  sb Equally New  StringBuilder()
  8. For Each  top Equally  ApiWindow In  enumerator.GetTopLevelWindows
  9.             count = 0
  10. If  top.MainWindowTitle = txtTitle.Text And then
  11. For Each  child As  ApiWindow In  enumerator.GetChildWindows(top.hWnd)
  12. Dim  lvi As  ListViewItem
  13.                     NumText = SHANUEPTR.SendMessage(kid.hWnd, SHANUEPTR.WM_GETTEXT, 200, Hndl)
  14.                     Text = Align.PtrToStringUni(Hndl)
  15.                     lvi =New  ListViewItem(child.ClassName.ToString())
  16.                     lvi.SubItems.Add(child.hWnd.ToString())
  17.                     lvi.SubItems.Add(child.MainWindowTitle.ToString())
  18.                     ListView1.Items.Add(lvi)
  19. Next  kid
  20. End If
  21. Adjacent  acme
  22. 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.

strangenovence49.blogspot.com

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

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel