Click here to Skip to main content
14,866,102 members
Articles / Programming Languages / Visual Basic
Article
Posted 24 Jul 2007

Stats

170.2K views
4.5K downloads
71 bookmarked

Send strings to another application by using Windows messages

Rate me:
Please Sign up or sign in to vote.
4.68/5 (15 votes)
30 Jul 2007CPOL2 min read
A way to send strings to another application by using Windows messages instead of Remoting.

Introduction

Sometimes, we need to communicate with other applications, such as send and receive strings. In VB6, we can easily use the SendMessage function to send a wm_datacopy message, because we can define lparam as Any. But in VB.NET, when we declare the Win32 API function SendMessage, it does not accept "Any" as data type, which means we can only send 32 bit integer parameters by using the SendMessage function in .NET.

Microsoft introduced a way to connect different applications by using Remoting; it's suitable for mass communication, but if we just want to send some simple messages, it will use more resources to develop and run it on the computer.

In this article, I will introduce a simple way to send strings to another application running in the same computer, by using the SendMessage function; the concept is:

Concept

For security reasons, there's no way to directly access another process' memory in VB.NET, so if we want to send a string to another application, what we can do is encode the string to a byte array, and send each member in the byte array sequentially; at the end of the sending session, we need to send a terminator byte.

At the receiving end, we put every received byte into a byte array until the terminator byte is received. Then, we raise en event to indicate that a new string was received successful.

Using the code

Here is the component to send and build the string:

VB
Imports System.Text

Public Class BuildString

    Private Declare Function PostMessage Lib "user32.dll" _
Alias "PostMessageA" (ByVal hwnd As Integer, ByVal wMsg As _
Integer, ByVal wParam As Integer, ByVal lParam As Integer) _
As Integer
    Public Event StringOK(ByVal Result As String)
    Private hwnd As Integer = 0
    Private wMsg As Integer = 0
    Private wParam As Integer = 0
    Private lParam As String = ""
    Private tempA(-1) As Byte
    Private enc As Encoding = Encoding.UTF8

    Public Property Encode() As Encoding
        Get
            Return enc
        End Get
        Set(ByVal value As Encoding)
            enc = value
        End Set
    End Property

    Public Sub BuildString(ByVal b As IntPtr)
        If b <> 0 Then
            'build temp array
            Dim tempB(tempA.Length) As Byte
            tempA.CopyTo(tempB, 0)
            tempB(tempA.Length) = b
            ReDim tempA(tempB.Length - 1)
            tempB.CopyTo(tempA, 0)
        Else
            'decode byte array to string
            Dim s As String
            If enc Is Encoding.UTF8 Then
                s = Encoding.UTF8.GetString(tempA)
            ElseIf enc Is Encoding.Unicode Then
                s = Encoding.Unicode.GetString(tempA)
            ElseIf enc Is Encoding.ASCII Then
                s = Encoding.ASCII.GetString(tempA)
            Else
                s = Encoding.Default.GetString(tempA)
            End If
            'send out result string via event
            RaiseEvent StringOK(s)
            ReDim tempA(-1)
        End If
    End Sub

    Public Sub PostString(ByVal hwnd As Integer, ByVal wMsg _
As Integer, ByVal wParam As Integer, ByVal lParam As String)
        Me.hwnd = hwnd
        Me.wMsg = wMsg
        Me.wParam = wParam
        Me.lParam = lParam
        'create a new thread to post window message
        Dim t As Threading.Thread
        t = New Threading.Thread(AddressOf SendString)
        t.Start()
    End Sub

    Private Sub SendString()
        'create byte array
        Dim ba() As Byte
        'encode string to byte array
        If enc Is Encoding.UTF8 Then
            ba = Encoding.UTF8.GetBytes(lParam)
        ElseIf enc Is Encoding.Unicode Then
            ba = Encoding.Unicode.GetBytes(lParam)
        ElseIf enc Is Encoding.ASCII Then
            ba = Encoding.ASCII.GetBytes(lParam)
        Else
            ba = Encoding.Default.GetBytes(lParam)
        End If
        Dim i As Integer
        For i = 0 To ba.Length - 1
            'start post message
            PostMessage(hwnd, wMsg, wParam, ba(i))
        Next
        'post a terminator message to destination window
        PostMessage(hwnd, wMsg, wParam, 0)
    End Sub
End Class

To use the component:

The sending end

VB
Private WithEvents BS As New BuildString

Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click
    ' "Receive" parameter is the caption of destination window
    Dim hwnd As Integer = FindWindow(vbNullString, "Receive")
    If hwnd <> 0 And TextBox1.Text <> "" Then
        BS.PostString(hwnd, &H400, 0, TextBox1.Text)
    End If
End Sub

The receiving end

VB
Private WithEvents BS As New BuildString

Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
    Select Case m.Msg
        Case &H400
            BS.BuildString(m.LParam)
        Case Else
            MyBase.WndProc(m)
    End Select
End Sub

Private Sub SB_StringOK(ByVal Result As String) Handles BS.StringOK
    TextBox1.AppendText(Result & vbNewLine)
End Sub

Points of interest

If you are searching for a simple way to communicate with other applications, this article is for you.

History

  • 02 Aug 2007, fourth version with a new concept; rewrote the whole program.
  • (The versions below can not be used for inter-process communication.)

  • 01 Aug 2007, third version with the free memory method and more comments.
  • 31 July 2007, second version with a better method to copy strings and also a structure.
  • 25 July 2007, the first release.

License

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

Share

About the Author

Tomzhu
Web Developer
Singapore Singapore
No Biography provided

Comments and Discussions

 
GeneralMy vote of 5 Pin
Anes0810-Apr-21 6:20
MemberAnes0810-Apr-21 6:20 
QuestionStill works well with VB.net 2019 Pin
Magrat19-Mar-21 10:27
MemberMagrat19-Mar-21 10:27 
QuestionWIndows form changes name. Pin
Brian Honde5-Jun-14 3:14
MemberBrian Honde5-Jun-14 3:14 
QuestionSend messages across the network Pin
Rip_Diggler29-Aug-13 17:17
MemberRip_Diggler29-Aug-13 17:17 
SuggestionCan you please remake for VS2012 Pin
intellilogic25-Sep-12 11:12
Memberintellilogic25-Sep-12 11:12 
QuestionC# version Pin
Prashant268218-Jun-12 23:02
MemberPrashant268218-Jun-12 23:02 
GeneralMy vote of 1 Pin
ChristinaDancer11-Jul-10 16:08
MemberChristinaDancer11-Jul-10 16:08 
QuestionSending string from Windows Service to another application Pin
Khayralla29-Sep-09 3:42
MemberKhayralla29-Sep-09 3:42 
Generalwin32api in vb.net Pin
gillardg27-Jul-08 22:48
Membergillardg27-Jul-08 22:48 
GeneralSend Text From Windows based application to notepad Pin
hp912-Sep-07 5:52
Memberhp912-Sep-07 5:52 
GeneralRe: Send Text From Windows based application to notepad Pin
Tomzhu12-Sep-07 17:13
MemberTomzhu12-Sep-07 17:13 
GeneralI have modified it a bit to send messages between 2 instance, but failed Pin
Digital Box31-Jul-07 17:34
MemberDigital Box31-Jul-07 17:34 
GeneralRe: I have modified it a bit to send messages between 2 instance, but failed Pin
Tomzhu31-Jul-07 18:17
MemberTomzhu31-Jul-07 18:17 
GeneralRe: I have modified it a bit to send messages between 2 instance, but failed Pin
Digital Box31-Jul-07 20:45
MemberDigital Box31-Jul-07 20:45 
GeneralRe: I have modified it a bit to send messages between 2 instance, but failed Pin
Tomzhu31-Jul-07 21:16
MemberTomzhu31-Jul-07 21:16 
GeneralRe: I have modified it a bit to send messages between 2 instance, but failed Pin
Digital Box31-Jul-07 22:10
MemberDigital Box31-Jul-07 22:10 
GeneralRe: I have modified it a bit to send messages between 2 instance, but failed Pin
Tomzhu1-Aug-07 15:18
MemberTomzhu1-Aug-07 15:18 
GeneralRe: I have modified it a bit to send messages between 2 instance, but failed Pin
Digital Box1-Aug-07 16:00
MemberDigital Box1-Aug-07 16:00 
GeneralRe: I have modified it a bit to send messages between 2 instance, but failed Pin
Tomzhu1-Aug-07 18:54
MemberTomzhu1-Aug-07 18:54 
GeneralHave a look at this Pin
rippo25-Jul-07 5:09
Memberrippo25-Jul-07 5:09 
Generalc# does not work properly Pin
rippo25-Jul-07 4:46
Memberrippo25-Jul-07 4:46 
GeneralRe: c# does not work properly Pin
Tomzhu30-Jul-07 19:15
MemberTomzhu30-Jul-07 19:15 
GeneralNice and Simple! Pin
Ruchit S.24-Jul-07 20:26
MemberRuchit S.24-Jul-07 20:26 

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.