viksoe.dk

Mail Slot


This article was submitted .


A light-weight component for Visual Basic 6 programmers giving access to the Mail Slot API.
Mail Slots are part of the Windows IPC (inter-process communication) services. They allow one computer to send messages to another or to broadcast messages to several other computers.

Messages smaller than 425 bytes are sent as datagrams and can be broadcasted. The ability to broadcast a message over the network to any number of computers is very desirable in many cases. Using datagrams means that no open connection is needed to the remote computer(s).

The sequence for setting up a Mail Slot is easy. One computer creates the Mail Slot, and can only read data. A remote computer can open the Mail Slot and write data. Mail Slots are identified by names. You also supply a computer name for the target computer - or use the * notation to broadcast to all Mail Slots with a given name on any computer.

Here is a sample VB6 server code:

  Dim Slot As New MailSlot
  Dim s as String
  Call Slot.Create("m_name")
  Do While True
    If Slot.PendingMessages > 0 Then
      s = Slot.Read
    End If
    DoEvents
    ' Add exit-loop code here
  Loop
  Call Slot.Close
and the client code...
  Dim Slot As New MailSlot
  Call Slot.Open("*", "m_name")
  Call Slot.Write("This is a test...")
  Call Slot.Close

This MailSlot component has a package size limit of about 200 characters. Characters are sent in UNICODE.

One example of how handy a Mail Slot can be is the MS SQL Server's ability to provide a list of all known SQL Servers on the network. With the simple broadcast technique that Mail Slots provide, a SQL Server simply broadcasts its existence on the network when it boots up.
You may also use Mail Slots to communicate across process boundaries on the local machine.

Known Bugs

  • You may receive several copies of any string you send. The following Microsoft KB issue Q127905 describes the problem.

Interface

 NameDescription
NameReturns the name of the MailSlot.
CreateCreates a MailSlot.
OpenOpens an existing mailslot.
ReadReads a string from a MailSlot.
WriteWrites a string to the MailSlot.
CloseCloses the MailSlot.
PendingMessagesReturns the number of pending messages.

Source Code Dependencies

Microsoft Visual C++ 6.0
Microsoft ATL Library

Installation Guide

  • Copy the DLL to a directory of your choice and register it using the REGSVR32 utility.

Download Files

DownloadBinary Files (12 Kb)
Source Code (42 Kb)

To the top