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:
and the client 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
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.
- You may receive several copies of any string you send. The following Microsoft KB issue Q127905 describes the problem.
|Name||Returns the name of the MailSlot.|
|Create||Creates a MailSlot.|
|Open||Opens an existing mailslot.|
|Read||Reads a string from a MailSlot.|
|Write||Writes a string to the MailSlot.|
|Close||Closes the MailSlot.|
|PendingMessages||Returns the number of pending messages.|
Source Code DependenciesMicrosoft Visual C++ 6.0
Microsoft ATL Library
- Copy the DLL to a directory of your choice and register it using the REGSVR32 utility.
|Binary Files (12 Kb)|
Source Code (42 Kb)