Creating Anonymous Pipes

An anonymous pipe passes information in only one direction, and both ends of the pipe must be on the same machine. The process that creates an anonymous pipe receives two handles: one for reading and one for writing. To communicate with another process, the server must pass one of the handles to the other process.

BOOL CreatePipe(
   PHANDLE phRead,              // variable for read handle (inbound)
   PHANDLE phWrite,             // variable for write handle (outbound)
   LPSECURITY_ATTRIBUTES lpsa,  // access privileges
   DWORD   dwPipeSize );        // size of pipe buffer (0=default)

The size of the pipe buffer determines how much information the pipe can hold before it overflows. No one can deposit messages in a full pipe until someone makes room by reading the old information from the other end.

If all goes well, CreatePipe returns TRUE and deposits two new valid handles in the variables indicated by the PHANDLE parameters. Next, the creating process usually needs to pass one of the handles to another process. Which handle you give away depends on whether you want the other process to send (write) or receive (read) information through the pipe. You can pass the handle to a child process on its command line or through its standard I/O handles. An unrelated process would need to receive the handle by other means, such as through a DDE conversation or a shared file. Connections through anonymous pipes are easier to arrange when the processes are related.

© 1998 SYBEX Inc. All rights reserved.