An anonymous pipe is an unnamed, one-way pipe that transfers data between a parent process and a child process or between two child processes of the same parent process. Although it is possible to use anonymous pipes for communication between unrelated processes, it is easier to use named pipes for that purpose (for more information, see Named Pipes). Anonymous pipes are always local; they cannot be used for communication over a network.
The CreatePipe function creates an anonymous pipe and returns two handles, one to the read end and one to the write end of the pipe. The read handle has only read access to the pipe, and the write handle has only write access to the pipe. To communicate through the pipe, a handle to one of the ends must be passed to another process. Usually, this is done through inheritance; that is, a child process inherits a handle from its parent process. The inheritability of these handles is controlled by the creating process in the following ways:
·The CreatePipe function specifies a SECURITY_ATTRIBUTES structure. If the bInheritHandle member of this structure is TRUE, the handles can be inherited.
·The DuplicateHandle function can change the inheritability of a pipe handle. This function enables a process to create a noninheritable duplicate of an inheritable pipe handle or to create an inheritable duplicate of a noninheritable pipe handle.
·The CreateProcess function enables a parent process to specify whether the new child process will inherit all or none of the parent's inheritable handles.
When a child process inherits a handle, the system enables the process to access the pipe referred to by that handle. However, the parent process must communicate the handle value to the child process. Typically, this is done by using the SetStdHandle and GetStdHandle functions, which set and retrieve the standard handles (standard input, standard output, or standard error) of a process. The parent process can specify a pipe handle in a call to SetStdHandle before the child process is created. The current standard handles of the parent process are inherited by the child process. So, when the child process starts up, it can use the GetStdHandle function to retrieve the handle value. The parent process can change its own standard handles without affecting the standard handles of its existing child processes.
To read from the pipe, a process uses the read handle in a call to the ReadFile function. When a write operation of any number of bytes completes, the ReadFile call returns. The ReadFile call also returns when all handles to the write end of the pipe have been closed or if any errors occur before the read operation completes normally.
To write to the pipe, a process uses the write handle in a call to the WriteFile function. WriteFile does not return until the specified number of bytes has been written or an error occurs. If the pipe's buffer is full and bytes remain to be written, WriteFile does not return until some other process or thread reads from the pipe, making more buffer space available. CreatePipe enables the creating process to specify a buffer size for the pipe, or it can use the default buffer size.
Asynchronous (overlapped) read and write operations are not supported for anonymous pipes. This means that the ReadFileEx and WriteFileEx functions cannot be used with anonymous pipes. The lpOverLapped parameter of the ReadFile and WriteFile functions is ignored when used with an anonymous pipe.
An anonymous pipe exists until all handles to both read and write ends of the pipe are closed by the CloseHandle function.