Click here to Skip to main content
12,402,202 members (66,607 online)
Click here to Skip to main content
Add your own
alternative version

Stats

31.7K views
2.9K downloads
27 bookmarked
Posted

External Program Text Read using VB.NET

, 2 Mar 2015 CPOL
Rate this:
Please Sign up or sign in to vote.
Read External application(Another application) text using VB.NET.

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. 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 attached sample program Solution name "SHANU_EPTR_V1.0" which is developed using VB.NET to read the external program and Notepad text.
I have attached a "SHANUEPTR.txt" Text file which can be used during read the text file.

I have attached a "SampleProgramtoRead.exe" inside "SampleProgramtoRead" folder which can be used as 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
https://msdn.microsoft.com/en-us/library/windows/desktop/ms632627(v=vs.85).aspx
https://msdn.microsoft.com/en-us/library/aa922085.aspx
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
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

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.
 

Background

The main aim was to develop a simple and easy to use .NET program which read and edit external program text. For long time I was been looking for some program to read the external program text. But today i got some time to work for it as a result you can see my article now. Iam planing to add more features to this program.

Using the code

The main aim 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 Steps to Read/Edit External Program text

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 which are currently opened.
'FindWindowEx -> This wil be used to find the control of application from where the text will be read or write.
'SendMessage -> Used to get the Text from external program.
'SendMessageSTRING -> Used to set the Text to 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 currently opened application with the same title as we have given.
' Note here txtTitle.Text is the notepad file name.we need to give the full title same as in notepade for example if your notepad title as "SHANUEPTR.txt - Notepad" then give the full name as it is.

'Find the Edit control of the Running Notepad
        Dim ChildHandle As IntPtr = SHANUEPTR.FindWindowEx(Hwnd, IntPtr.Zero, "Edit", Nothing)

'FindWindowEx is to check for the control from application from where need 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 external program to get the text.
'ChildHandle -> is the external application control ID from where we read the text we need to give 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 external program.

 

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

'Find the running notepad window

Dim Hwnd As IntPtr = SHANUEPTR.FindWindow(Nothing, txtTitle.Text)

'FindWindow - > will check for currently opened application with the same title as we have given.
' Note here txtTitle.Text is the notepad file name.we need to give the full title same as in notepade for example if your notepad title as "SHANUEPTR.txt - Notepad" then give the full name as it is.

'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 application from where need 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 give the proper control id to read the text .
'WM_SETTEXT- > is used to SET the Text.
'TexttoWritetoNotepad-> This is the String which need to be write to note pad.

 

Read Text from 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 same like above example.Here this function will find all the controls and Child controls of external application and read the text of each controls.The resultant controls text will be added to the list view.

Conclusion

Hope you like my Article.I will be happy if some one get benefited from my article.In a first step I have created this simple program which will read and edit Notepad Text. For the External application for now I have created to read text from label ,Text box, Combo box ,Button controls, and for edit I have made sample only for edit box like Text box and combo box. Controls like Treeview, Menu, Listview, grid text read will be updated in my next versions.

If you like my article leave me a comments.

History

  • Initial release: 2014/07/03.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

syed shanu
Team Leader
India India

Microsoft MVP 2016


My Interview on Microsoft TechNet Wiki Ninja Link




His Blog


Syed Shanu is basically from Madurai, Tamil Nadu, India.He was been working in South Korea for past 8 years. He started programming with Basic Language and C language from his high school at 1999.In 2005 he completed his Master of Computer Application. He started his working Career from Dec 2004 with ASP.

He has 10+ years of Experience in Microsoft Technologies.

His work experience with Language and Technology start’s from ASP and SQL Server, Then VB.NET and C# for PDA Application, Touch Screen Application Development, Desktop Application, ASP.NET Web Application Development, MVC and WPF.

He loves to work with Microsoft technology as he started to work on .Net Frame Work version from 1.0 to 4.5.

He had worked with HMI (Human Machine Interface) programs like PLC, Nutrunner Tools, and Sensor programs, RFID programs, Barcode programs and etc.

He usually uses his free time to spend with his Family and go outing. He loves photography and Hiking.

ASP.NET Article of the Day

1) Title : Draw ASP.NET Bar Chart Using HTML5 and jQuery Date : July 8, 2015
2) Title : ASP.NET Web Photo Editing Tool using HTML 5 Date : July 8, 2015
3) Title : MVC AngularJS and WCF Rest Service For Mind Reader Quiz Date : May 18, 2015
4) Title : AngularJS Shopping Cart Using MVC and WCF Rest Service Date : April 10, 2015
5) Title : Insert select update delete in asp.net with Simple Log
6) Title : Project Scheduling Using ASP.Net GridView Date : December 26, 2014
7) Title : ASP.NET Web Painting Tool using HTML 5 Date : September 16, 2014

You may also be interested in...

Comments and Discussions

 
QuestionPisses me off when you steal code without giving credit!! Pin
Tino Fourie24-May-16 2:42
memberTino Fourie24-May-16 2:42 
AnswerRe: Pisses me off when you steal code without giving credit!! Pin
syed shanu24-May-16 2:59
mvpsyed shanu24-May-16 2:59 
GeneralRe: Pisses me off when you steal code without giving credit!! Pin
Tino Fourie24-May-16 3:11
memberTino Fourie24-May-16 3:11 
GeneralRe: Pisses me off when you steal code without giving credit!! Pin
syed shanu24-May-16 3:16
mvpsyed shanu24-May-16 3:16 
GeneralRe: Pisses me off when you steal code without giving credit!! Pin
Tino Fourie24-May-16 3:21
memberTino Fourie24-May-16 3:21 
GeneralRe: Pisses me off when you steal code without giving credit!! Pin
syed shanu24-May-16 3:23
mvpsyed shanu24-May-16 3:23 
QuestionExternal Program Text Read using VB.NET failed when the external program is a wpf.net project Pin
Muhammed Shafeek V21-Apr-16 2:49
memberMuhammed Shafeek V21-Apr-16 2:49 
QuestionExternal Program Text Read using VB.NET failed when the external program is a java swing project Pin
Muhammed Shafeek V19-Apr-16 22:45
memberMuhammed Shafeek V19-Apr-16 22:45 
QuestionMessage Removed Pin
Muhammed Shafeek V18-Apr-16 21:37
memberMuhammed Shafeek V18-Apr-16 21:37 
QuestionThanks & Small question Pin
gabozeweal8-Oct-15 14:53
membergabozeweal8-Oct-15 14:53 
AnswerRe: Thanks & Small question Pin
syed shanu8-Oct-15 15:03
mvpsyed shanu8-Oct-15 15:03 
GeneralRe: Thanks & Small question Pin
gabozeweal9-Oct-15 4:05
membergabozeweal9-Oct-15 4:05 
GeneralRe: Thanks & Small question Pin
gabozeweal9-Oct-15 8:03
membergabozeweal9-Oct-15 8:03 
GeneralRe: Thanks & Small question Pin
syed shanu9-Oct-15 19:26
mvpsyed shanu9-Oct-15 19:26 
SuggestionGreat Work Sir! Pin
Dhandapani SivaKumar12-Sep-15 1:51
memberDhandapani SivaKumar12-Sep-15 1:51 
QuestionDatagridview, combobox. Pin
Orlando Torrico Fernandez2-Mar-15 10:28
memberOrlando Torrico Fernandez2-Mar-15 10:28 
QuestionI have to say thank you. This is exactly what I needed to learn this stuff. I was having such a difficult time. Pin
Justinwashere20-Feb-15 16:53
memberJustinwashere20-Feb-15 16:53 
QuestionGreat project but unstable Pin
Member 1051949320-Aug-14 5:07
memberMember 1051949320-Aug-14 5:07 
AnswerRe: Great project but unstable Pin
syed shanu20-Aug-14 14:24
membersyed shanu20-Aug-14 14:24 
GeneralMy vote of 5 Pin
Mihai MOGA13-Aug-14 1:43
professionalMihai MOGA13-Aug-14 1:43 
GeneralRe: My vote of 5 Pin
syed shanu17-Aug-14 15:10
membersyed shanu17-Aug-14 15:10 
QuestionGreat! My vote of 5 Pin
Peter_jnr7-Jul-14 17:33
memberPeter_jnr7-Jul-14 17:33 
AnswerRe: Great! My vote of 5 Pin
syed shanu7-Jul-14 18:13
membersyed shanu7-Jul-14 18:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160721.1 | Last Updated 2 Mar 2015
Article Copyright 2015 by syed shanu
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid