DetailPage-MSS-KB

Microsoft small business knowledge base

Article ID: 871044 - Last Review: January 11, 2007 - Revision: 2.4

On This Page

SUMMARY

This step-by-step article discusses how to use named pipes in Microsoft Visual Basic .NET or in Microsoft Visual Basic 2005 for interprocess communication. This article includes a code sample that demonstrates client/server communication by using a named pipe in Visual Basic .NET or in Visual Basic 2005. The article describes the interprocess communication by creating a named pipe server and a named pipe client. The communication over the pipe is performed in the following way:
  • Create a named pipe.
  • Block the server application by using the ConnectNamedPipe function until a client connects.
  • Connect to the server by using the CallNamedPipe function.
  • Call the ReadFile function or the WriteFile function to communicate over the pipe.
  • Call the DisconnectNamedPipe function when the process finishes using the pipe.
  • Call the CloseHandle function on the named pipe after you completed communicating over the pipe.

INTRODUCTION

A named pipe is a one-way or duplex pipe for communication between a pipe server and one or more pipe clients. You can user named pipes to provide communication between processes on the same computer or between processes on different computers across a network. The term "named pipe server" refers to a process that creates a named pipe, and the term "named pipe client" refers to a process that connects to an instance of a named pipe.

You can use Microsoft Visual Basic .NET or Microsoft Visual Basic 2005 to create applications that communicate to other processes by using named pipes. This article contains a code sample that uses a named pipe to communicate between two Visual Basic .NET or Visual Basic 2005 Windows applications.

Requirements

This article assumes that you are familiar with the following topics:
  • Windows applications
  • Visual Basic .NET programming
  • Using pipes
The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
  • Microsoft Visual Studio .NET or Microsoft Visual Studio 2005
  • Microsoft .NET Framework

Create a named pipe server

To create a named pipe server, and then communicate with the client over the pipe, follow these steps:
  1. Create a named pipe.
  2. Call the ConnectNamedPipe function to block the server until a client connects.
  3. Call the ReadFile function or the WriteFile function to communicate over the pipe.
  4. Call the DisconnectNamedPipe function when the process finishes using the pipe.
  5. Call the CloseHandle function on the named pipe.

Design a Windows application that creates a named pipe server

To design a Windows application that creates a named pipe server by using Visual Basic .NET or Visual Basic 2005, follow these steps:
  1. Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.
  2. On the File menu, point to New, and then click Project.
  3. Under Project Types, click Visual Basic Projects, and then click Windows Application under Templates.

    Note In Visual Studio 2005, click Visual Basic under Project Types.
  4. In the Name box, type MyServerApp, and then click OK. By default, a form that is named Form1 is created.
  5. Add three Button controls to the Form1 form.
  6. On the View menu, click Properties Window.
  7. Set the Text property of the Button controls to the following values:
    Collapse this tableExpand this table
    NameText
    Button1Create a named pipe
    Button2Wait for the client connections
    Button3Disconnect the server
  8. Add a Label control to the Form1 form. The Label1 Label control is added to the Form1 form.

Add all the declarations to a module in the Windows application

To add the function declarations that are required to implement the interprocess communication by using the named pipe, follow these steps:
  1. In Solution Explorer, right-click MyServerApp, point to Add, and then click Add Module.
  2. In the Add New Item - MyServerApp dialog box, click Open.
  3. Add the following code to the Module1.vb module:
    Public Const FILE_ATTRIBUTE_NORMAL As Short = &H80S
    Public Const FILE_FLAG_NO_BUFFERING As Integer = &H20000000
    Public Const FILE_FLAG_WRITE_THROUGH As Integer = &H80000000
    
    Public Const PIPE_ACCESS_DUPLEX As Short = &H3S
    Public Const PIPE_READMODE_MESSAGE As Short = &H2S
    Public Const PIPE_TYPE_MESSAGE As Short = &H4S
    Public Const PIPE_WAIT As Short = &H0S
    
    Public Const INVALID_HANDLE_VALUE As Short = -1
    
    Declare Function CreateNamedPipe Lib "kernel32" Alias "CreateNamedPipeA" _
    (ByVal lpName As String, ByVal dwOpenMode As Integer, _
    ByVal dwPipeMode As Integer, ByVal nMaxInstances As Integer, _
    ByVal nOutBufferSize As Integer, ByVal nInBufferSize As Integer, _
    ByVal nDefaultTimeOut As Integer, ByVal lpSecurityAttributes As IntPtr _
    ) As Integer
    
    Declare Function ConnectNamedPipe Lib "kernel32" _
        (ByVal hNamedPipe As Integer, ByVal lpOverlapped As Integer) As Integer
    
    Declare Function DisconnectNamedPipe Lib "kernel32" _
        (ByVal hNamedPipe As Integer) As Integer
    
    Declare Function WriteFile Lib "kernel32" _
    (ByVal hFile As Integer, ByRef lpBuffer() As Byte, _
    ByVal nNumberOfBytesToWrite As Integer, ByRef lpNumberOfBytesWritten As Integer, _
    ByVal lpOverlapped As Integer _
    ) As Integer
    
    Declare Function ReadFile Lib "kernel32" _
    (ByVal hFile As Integer, ByRef lpBuffer As Integer, _
    ByVal nNumberOfBytesToRead As Integer, ByRef lpNumberOfBytesRead As Integer, _
    ByVal lpOverlapped As Integer _
    ) As Integer
    
    Declare Function FlushFileBuffers Lib "kernel32" _
        (ByVal hFile As Integer) As Integer
    
    Declare Function CloseHandle Lib "kernel32" _
        (ByVal hObject As Integer) As Integer
    

Write code to create a named pipe server

After you create a named pipe, wait for the connections from the client. When the client connects to the server, read or write the data over the pipe. To do this, follow these steps:
  1. In Solution Explorer, right-click Form1, and then click Open.
  2. In Design view of the Form1 form, double-click Create a named pipe, and then add the following code to the Button1_Click procedure:
    Dim openMode, pipeMode As Integer
    'Create the named pipe
    openMode = PIPE_ACCESS_DUPLEX Or FILE_FLAG_WRITE_THROUGH
    pipeMode = PIPE_WAIT Or PIPE_TYPE_MESSAGE Or PIPE_READMODE_MESSAGE
    hPipe = CreateNamedPipe(pipeName, openMode, pipeMode, 10, 10000, 2000, 10000, IntPtr.Zero)
    Label1.Text = "Created the named pipe and waiting for the clients."
    Button1.Visible = False
    Button2.Visible = True
    Button3.Visible = True
  3. Add the following code before the Button1_Click procedure:
    Private Const pipeName As String = "\\.\pipe\MyPipe"
    Private Const BUFFSIZE As Short = 10000
    Private Buffer(BUFFSIZE) As Byte
    Private hPipe As Integer
  4. In Solution Explorer, double-click Form1.vb.
  5. In Design view of the Form1 form, double-click Wait for the client connections, and then add the following code to the Button2_Click procedure:
    Dim byteCount, i, res, cbnCount As Integer
    For i = 0 To BUFFSIZE - 1 'Fill an array of numbers
       Buffer(i) = i Mod 256
    Next i
    'Wait for a connection, block until a client connects
    Label1.Text = "Waiting for client connections"
    Me.Refresh()
    Do
       res = ConnectNamedPipe(hPipe, 0)
       'Read the data sent by the client over the pipe
       cbnCount = 4
       res = ReadFile(hPipe, byteCount, Len(byteCount), cbnCount, 0)
       If byteCount > BUFFSIZE Then 'Client requested for byteCount bytes
           byteCount = BUFFSIZE 'but only send up to 20000 bytes
       End If
       'Write the number of bytes requested by the client 
       res = WriteFile(hPipe, Buffer, byteCount, cbnCount, 0)
       res = FlushFileBuffers(hPipe)
       'Disconnect the named pipe.
       res = DisconnectNamedPipe(hPipe)
       'Loop until the client makes no more requests for data. 
    Loop Until byteCount = 0
    Label1.Text = "Read or Write completed"
    Button2.Visible = False
  6. In Design view, double-click Form1, and then add the following code to the Form1_Load procedure:
    Button2.Visible = False
    Button3.Visible = False
  7. In Solution Explorer, double-click Form1.vb.
  8. In Design view of the Form1 form, double-click Disconnect the server, and then add the following code to the Button3_Click procedure:
    Dim res As Integer
    'Close the pipe handle when the client makes no requests
    CloseHandle(hPipe)
    Label1.Text = "Disconnected the named pipe"
  9. On the Build menu, click Build Solution.

Create a named pipe client

To create a named pipe client that communicates with the server, follow these steps:
  1. Call the CreateFile function to create a handle to the named pipe.
  2. Call the ReadFile function or the WriteFile function to communicate over the pipe.
  3. Call the CloseHandle function on the handle that is created in the CreateFile function.
You can also use a named pipe transaction for client/server communication. The named pipe transaction combines a write operation and a read operation in a single network operation. A transaction can be used only on a duplex, message-type pipe. Processes can use the TransactNamedPipe function or the CallNamedPipe function to perform the named pipe transactions.

In this code sample, you use the CallNamedPipe function to connect to the named pipe server, write the data to the pipe, and then read the data from the pipe.

Design a Windows application that communicates with the named pipe server

To design a Visual Basic .NET or Visual Basic 2005 Windows application that is used to connect to the named pipe server, follow these steps:
  1. Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.
  2. On the File menu, point to New, and then click Project.
  3. Under Project Types, click Visual Basic Projects, and then click Windows Application under Templates.

    Note In Visual Studio 2005, click Visual Basic under Project Types.
  4. In the Name box, type MyClientApp, and then click OK. By default, a form that is named Form1 is created.
  5. Add a Button control to the Form1 form.
  6. Right-click Button1, and then click Properties.
  7. Set the Text property to Connect to the server.
  8. Add a Label control to the Form1 form. The Label1 Label control is added to Form1 form.
  9. Set the Visible property of Label1 Label control to False.
  10. Add two TextBox controls to the Form1 form.

Write code to connect to the named pipe server

Connect to the named pipe server by using the CallNamedPipe function. After connecting to the server, the CallNamedPipe function writes to the pipe, reads from the pipe, and then closes the pipe. To connect to the server, and then read or write the data, follow these steps:
  1. In Design view, double-click Connect to the server, and then add the following code to the Button1_Click procedure:
    Dim i, res, cbRead,numBytes As Integer
    Dim bArray() As Byte
    Dim temp As String
    
    numBytes = CInt(TextBox1.Text)
    If numBytes < 0 Then
        MessageBox.Show("Value must be at least 0.", MsgBoxStyle.OKOnly)
        Exit Sub
    End If
    If numBytes = 0 Then
        Label1.Visible = True
        Label1.Text = "The connection to the server is disconnected."
        Button1.Visible = False
        TextBox1.Visible = False
        TextBox2.Visible = False
    End If
    If numBytes > BUFFSIZE Then
        numBytes = BUFFSIZE
    End If
    
    ReDim bArray(numBytes) 'Create the return buffer
    'Call the CallNamedPipe function to do the transactions
     res = CallNamedPipe(pipeName, numBytes, Len(numBytes), bArray(0), numBytes, cbRead, 30000) 
    'Wait up to 30 seconds for a response
    'Format the data received, and then display the data in the text box
    If res > 0 Then
        temp = Format(bArray(0), " 000")
        For i = 1 To cbRead - 1
           If (i Mod 16) = 0 Then temp = temp & vbCrLf
              temp = temp & " " & Format(bArray(i), "000")
        Next i
        TextBox2.Text = temp
    Else
    MessageBox.Show("Error number " & Err.LastDllError & _
    "while trying to call the CallNamedPipe function.", MsgBoxStyle.OKOnly)
    End If
  2. Add the following code before the Button1_Click procedure:
    Private Const pipeName As String = "\\.\pipe\MyPipe"
    Private Const BUFFSIZE As Integer = 10000
    Private hpipe As Integer
        
    Public Const INVALID_HANDLE_VALUE As Short = -1
    Public Declare Function CallNamedPipe Lib "kernel32" Alias "CallNamedPipeA" _
    (ByVal lpNamedPipeName As String, _
    ByRef lpInBuffer As Integer, _
    ByVal nInBufferSize As Integer, _
    ByRef lpOutBuffer As Byte, _
    ByVal nOutBufferSize As Integer, _
    ByRef lpBytesRead As Integer, ByVal nTimeOut As Integer) As Integer
  3. On the Build menu, click Build Solution.

Verify that the code works

To verify that your code works, follow these steps:
  1. To start the server application, click Start on the Debug menu of the MyServerApp project.
  2. On the Form1 form, click Create a named pipe, and then click Wait for the client connections. The application is now blocked and waits for the client to connect.
  3. To start the client application, click Start on the Debug menu of the MyClientApp project.
  4. On the Form1 form, type 10 in the TextBox1 box, and then click Connect to the server. You can see the byte array that is received in the TextBox2 box.
  5. To disconnect the client application from the server, type 0 in the TextBox1 box in the client application, and then click Connect to the server.
  6. Close the client application.
  7. To disconnect the server end of the named pipe, and then close the server application, click Disconnect the server on the Form1 form of the server application.

REFERENCES

For more information, visit the following Microsoft Developer Network (MSDN) Web sites:
Named pipes
http://msdn2.microsoft.com/en-us/library/aa365590.aspx (http://msdn2.microsoft.com/en-us/library/aa365590.aspx)
Transactions on named pipes
http://msdn2.microsoft.com/en-us/library/aa365789.aspx (http://msdn2.microsoft.com/en-us/library/aa365789.aspx)
Pipe functions
http://msdn2.microsoft.com/en-us/library/aa365781.aspx (http://msdn2.microsoft.com/en-us/library/aa365781.aspx)

APPLIES TO
  • Microsoft Visual Basic 2005
  • Microsoft Visual Basic .NET 2003 Standard Edition
  • Microsoft Visual Basic .NET 2002 Standard Edition
Keywords: 
kbvs2005swept kbvs2005applies kbserver kbpipes kbipc kbclient kbhowtomaster kbhowto KB871044
Share
Additional support options
Ask The Microsoft Small Business Support Community
Contact Microsoft Small Business Support
Find Microsoft Small Business Support Certified Partner
Find a Microsoft Store For In-Person Small Business Support