Inter-process communication are methods that allow two running processes to exchange information. We already have one such tool. It is memory mapping. Pipes are another such tool. They represent a connection between two programs that is analygous to a phone connection
Pipes - a means of having one running process talk to another. They use ReadFile and WriteFile to communicate.
Anonymous Pipes - byte oriented, half-duplex (one-direction), and supports communications between processes that are related. We will not discuss in that they are relatively weak and need knowledge that we have skipped.
Named Pipes - full duplex, message oriented and allows network communications. (Network stuff will only work among WIN32 machines.) WIN32 named pipes are impressive in that they demonstrates most of the features that a pipe might have. The following is a list of features:
Client/Server model - the developers of named pipes had a client/server model in mind. Discuss this concept in general. So, with named pipes, it is expected that the server creates the named pipe and handles all connects and requests. The client connects to an existing named pipe, makes requests and get responses.
The following is the system call used to create a named pipe. I have edited it from the help page. The server may make this call multiple times to create multiple instances of the named pipe. A client can connect to each instance.
HANDLE CreateNamedPipe(
LPCTSTR lpName, // pointer to pipe name
DWORD dwOpenMode, // pipe open mode
DWORD dwPipeMode, // pipe-specific modes
DWORD nMaxInstances, // maximum number of instances
DWORD nOutBufferSize, // output buffer size, in bytes
DWORD nInBufferSize, // input buffer size, in bytes
DWORD nDefaultTimeOut, // time-out time, in milliseconds
LPSECURITY_ATTRIBUTES lpSecurityAttributes // pointer to security attributes
);
If the function succeeds, the return value is a handle to the server end of a named pipe instance. The server will use this in all other system calls to access the pipe.
If the function fails, the return value is INVALID_HANDLE_VALUE. To get extended error information, call GetLastError. The return value is ERROR_INVALID_PARAMETER if nMaxInstances is greater than PIPE_UNLIMITED_INSTANCES. This value is 255.
PIPE_ACCESS_DUPLEX
The pipe is bidirectional; both server and client processes can read from and write to the pipe. This mode gives the server the equivalent of GENERIC_READ | GENERIC_WRITE access to the pipe. The client can specify GENERIC_READ or GENERIC_WRITE, or both, when it connects to the pipe using the CreateFile function.
PIPE_ACCESS_INBOUND
The flow of data in the pipe goes from client to server only. This mode gives the server the equivalent of GENERIC_READ access to the pipe. The client must specify GENERIC_WRITE access when connecting to the pipe.
PIPE_ACCESS_OUTBOUND
The flow of data in the pipe goes from server to client only. This mode gives the server the equivalent of GENERIC_WRITE access to the pipe. The client must specify GENERIC_READ access when connecting to the pipe.
PIPE_TYPE_BYTE
Data is written to the pipe as a stream of bytes. This mode cannot be used with PIPE_READMODE_MESSAGE.
PIPE_TYPE_MESSAGE
Data is written to the pipe as a stream of messages. This mode can be used with either PIPE_READMODE_MESSAGE or PIPE_READMODE_BYTE.
PIPE_READMODE_BYTE
Data is read from the pipe as a stream of bytes. This mode can be used with either PIPE_TYPE_MESSAGE or PIPE_TYPE_BYTE.
PIPE_READMODE_MESSAGE
Data is read from the pipe as a stream of messages. This mode can be only used if PIPE_TYPE_MESSAGE is also specified.
PIPE_WAIT
Blocking mode is enabled. When the pipe handle is specified in the ReadFile, WriteFile, or ConnectNamedPipe function, the operations are not completed until there is data to read, all data is written, or a client is connected. Use of this mode can mean waiting indefinitely in some situations for a client process to perform an action.
PIPE_NOWAIT
Nonblocking mode is enabled. In this mode, ReadFile, WriteFile, and ConnectNamedPipe always return immediately. Note that nonblocking mode is supported for compatibility with Microsoft LAN Manager version 2.0 and should not be used to achieve asynchronous I/O with named pipes.
BOOL PeekNamedPipe(
HANDLE hNamedPipe, // handle to pipe to copy from
LPVOID lpBuffer, // pointer to data buffer
DWORD nBufferSize, // size, in bytes, of data buffer
LPDWORD lpBytesRead, // pointer to number of bytes read
LPDWORD lpTotalBytesAvail, // pointer to total number of bytes available
LPDWORD lpBytesLeftThisMessage // pointer to unread bytes in this message
);
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error information, call GetLastError.
This is a function that is pretty vital to any processing using pipes. If the client disconnects, the pipe is not reuseable. Not until the server disconnects it.
The DisconnectNamedPipe function disconnects the server end of a named pipe instance from a client process.
BOOL DisconnectNamedPipe(
HANDLE hNamedPipe // handle to named pipe
);
Parameters
hNamedPipe
Handle to an instance of a named pipe. This handle must be created by the CreateNamedPipe function.
Return Values
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error information, call GetLastError.
Remarks
If the client end of the named pipe is open, the DisconnectNamedPipe function forces that end of the named pipe closed. The client receives an error the next time it attempts to access the pipe. A client that is forced off a pipe by DisconnectNamedPipe must still use the CloseHandle function to close its end of the pipe.
When the server process disconnects a pipe instance, any unread data in the pipe is discarded. Before disconnecting, the server can make sure data is not lost by calling the FlushFileBuffers function, which does not return until the client process has read all the data.
The server process must call DisconnectNamedPipe to disconnect a pipe handle from its previous client before the handle can be connected to another client by using the ConnectNamedPipe function.