Destroying a Pipe

Like most of the objects we’ve discussed so far, a pipe object remains in memory until all the handles to it are closed. Whenever any process finishes with its end of a pipe, it should call CloseHandle. If you forget, the ExitProcess command closes all your remaining handles for you.

Distinguishing Pipes and Mailslots

Pipes are similar to another object called a mailslot. Like a pipe, a mailslot is a buffer where processes leave messages for each other. Mailslots, however, always work in only one direction, and many applications may open the receiving end of the same mailslot.

A program that opens a handle to receive messages in a mailslot is a mailslot server. A program that opens a handle to broadcast messages through a mailslot is the mailslot client. When a client writes to a mailslot, copies of the message are posted to every server that has a handle to the same mailslot.

A pipe takes information from one end and delivers it to the other end. A mailslot takes information from one end and delivers it to many other ends.

Mailslots have string names just as named pipes do. The commands for mailslot operations resemble the pipe API functions:

To create a server’s read-only mailslot handle, call CreateMailslot.

To retrieve or modify its attributes, call GetMailslotInfo and SetMailslotInfo.

To create the client’s write-only mailslot handle, call CreateFile.

To send and receive mailslot messages, call WriteFile and ReadFile.

To destroy a mailslot, close its handles with CloseHandle.

Besides the advantage of broadcasting a message to multiple recipients, mailslots make it easier to connect with processes over a network. For a pipe client to connect with a remote server, the client must first ascertain the name of the server’s machine, which may require a slow enumeration of network servers and repeated attempts to connect with each until the pipe is found. But in naming a mailslot for CreateFile, the mailslot client may use an asterisk (*) to represent the current network domain. (A domain is any group of linked workstations and network servers to which an administrator has assigned a common name. One network system may contain a single all-inclusive domain, or it may be divided into several associated domains.)

\\*\mailslot\<mailslotname>

Using a name of that form, the handle the client receives broadcasts messages to all processes in the current domain that have opened a mailslot using the <mailslotname> string. This ability to broadcast across a domain suggests one of the ways mailslots might be used. Processes that want to connect through a remote pipe link may prefer to find each other through a mailslot first, using this procedure:

  1. Both processes open a mailslot with the same name.

  2. The client broadcasts the name of its pipe, including in the name string the name of its own network server.

  3. The recipient uses the broadcast string to connect with the pipe and avoids laboriously enumerating all the available servers.

  4. The client and server find each other through a one-way mailslot, and then establish a private two-way pipe to continue the conversation.

© 1998 SYBEX Inc. All rights reserved.