Okay, since nobody took up my challenge. I've had to solve this
myself. I have written a simple but complete demo of inter-process
two-communication using named pipes implemented with version 3.5 of
the .Net Framework in VB. The complete solution can be downloaded
from here.
http://mysite.verizon.net/johnheitmuller/TxtMsg.zip
I've included all of the VisualStudio 2008 run-time libs needed to run
this example even if you do not have VS2008 installed.
Here is the source listing.
Imports System.Text ' required for UTF8Encoding
Imports System.IO
Imports System.IO.Pipes
Public Class TxtMsg
Private strPipeName As String
Private encoding As UTF8Encoding
Private pipeServer As NamedPipeServerStream
Private Const nbrTimeOut As Integer = 5000 ' 5 seconds
Private Sub TxtMsg_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
CheckForIllegalCrossThreadCalls = False ' required for
running in debugger. ConnectionCallback() runs in s separate thread
encoding = New UTF8Encoding()
Me.strPipeName = Process.GetCurrentProcess.ProcessName & "_" &
Process.GetCurrentProcess.Id
Me.UpdateStatus("Creating Named Pipe Server: " &
Me.strPipeName)
pipeServer = New NamedPipeServerStream(Me.strPipeName,
PipeDirection.In, 1, PipeTransmissionMode.Message,
PipeOptions.Asynchronous)
Me.UpdateStatus("Named Pipe Created.")
pipeServer.BeginWaitForConnection(AddressOf
ConnectionCallback, Nothing)
Me.UpdateStatus("Waiting for connection.")
End Sub
Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnSend.Click
If Not String.IsNullOrEmpty(Me.tbxMessage.Text) Then
Dim proc As Process = Process.GetCurrentProcess()
Dim strTrgProcessName = proc.ProcessName
If Me.cbxOtherInstanceInDebugger.Checked Then
strTrgProcessName &= ".vshost"
Dim processes() As Process =
Process.GetProcessesByName(strTrgProcessName)
If processes.Length > 0 Then
Dim bytesMessage() As Byte =
encoding.GetBytes(Me.tbxMessage.Text & vbCrLf)
Dim bytesProcID() As Byte =
encoding.GetBytes(proc.Id.ToString & vbCrLf)
For Each p As Process In processes
If p.Id <> proc.Id Then ' don't send to self
Dim strServerPipeName As String =
p.ProcessName & "_" & p.Id
Me.UpdateStatus("Attempting to connect to
server " & strServerPipeName & ". Timeout = " & nbrTimeOut.ToString &
" milliseconds.")
Dim pipeStream As NamedPipeClientStream = New
NamedPipeClientStream(".", strServerPipeName, PipeDirection.Out) '
"."=localhost
pipeStream.Connect(nbrTimeOut)
Me.UpdateStatus("Connection established.")
Me.UpdateStatus("Writing to server Proc ID: "
& proc.Id.ToString & ", Message: " & Me.tbxMessage.Text)
pipeStream.Write(bytesProcID, 0,
bytesProcID.Length)
pipeStream.Write(bytesMessage, 0,
bytesMessage.Length)
Me.tbxConversation.Text &=
DateTime.Now.ToLongTimeString() & ": " & proc.Id & " " &
Me.tbxMessage.Text & vbCrLf
Me.tbxMessage.Clear()
Me.UpdateStatus("Closing connection...")
pipeStream.Close()
Me.UpdateStatus("Connection closed.")
End If
Next ' next process
End If
End If
End Sub
Private Sub ConnectionCallback(ByVal ar As IAsyncResult)
Me.UpdateStatus("ConnectionCallback() fired.")
Me.pipeServer.EndWaitForConnection(ar)
Me.UpdateStatus("End Waiting for Connection.")
Dim sr As StreamReader = New StreamReader(Me.pipeServer)
Dim strProcID As String = sr.ReadLine()
Dim strMessage As String = sr.ReadLine()
Me.UpdateStatus("Received ProcID String (" & strProcID & "),
string length = " & strProcID.Length.ToString)
Me.UpdateStatus("Received Message String (" & strMessage & "),
string length = " & strMessage.Length.ToString)
Me.tbxConversation.Text &= DateTime.Now.ToLongTimeString() &
": " & strProcID & " " & strMessage & vbCrLf
Me.UpdateStatus("Disconnecting connection.")
Me.pipeServer.Disconnect()
pipeServer.BeginWaitForConnection(AddressOf
ConnectionCallback, Nothing)
Me.UpdateStatus("Waiting for connection.")
End Sub
Private Sub UpdateStatus(ByRef str As String)
Me.tbxStatus.AppendText(DateTime.Now.ToLongTimeString() & ":
" & str & vbCrLf)
End Sub
End Class