Function Signature
BOOL CreatePipe(
PHANDLE hReadPipe,
PHANDLE hWritePipe,
LPSECURITY_ATTRIBUTES lpPipeAttributes,
DWORD nSize
);
Parameters
-
hReadPipeA pointer to a HANDLE that receives the handle to the read end of the pipe. The returned handle is overlapped. This parameter can be NULL if the read handle is not required.
-
hWritePipeA pointer to a HANDLE that receives the handle to the write end of the pipe. The returned handle is overlapped. This parameter can be NULL if the write handle is not required.
-
lpPipeAttributesA pointer to a
SECURITY_ATTRIBUTESstructure that specifies the security descriptor for the pipe. If this parameter is NULL, the pipe gets a default security descriptor. The MSDN documentation specifies that the pipe's default security descriptor allows access to the calling process and the child processes of the calling process. If the lpPipeAttributes parameter is NULL, the GetInheritanceSource function does not return a valid provider for the pipe. -
nSizeThe size, in bytes, of the buffer for the pipe. If this parameter is zero, the system default size is used. The system default size is 65,536 bytes. This parameter can be zero.
Return Value
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
A pipe is a unidirectional data stream. To create a bidirectional communication channel, create two pipes. For example, to create a channel where process A can write to process B and process B can write to process A, call CreatePipe twice.
The handles returned in hReadPipe and hWritePipe are overlapped handles. This means that you can use them with the ReadFileEx and WriteFileEx functions to perform asynchronous I/O operations.
Example
#include <windows.h>
#include <iostream>
int main() {
HANDLE hReadPipe, hWritePipe;
SECURITY_ATTRIBUTES sa;
sa.nLength = sizeof(sa);
sa.lpSecurityDescriptor = NULL;
sa.bInheritHandle = TRUE;
if (CreatePipe(&hReadPipe, &hWritePipe, &sa, 0)) {
std::cout << "Pipe created successfully." << std::endl;
// Use the pipe handles for inter-process communication
// ...
CloseHandle(hReadPipe);
CloseHandle(hWritePipe);
} else {
std::cerr << "Failed to create pipe. Error: " << GetLastError() << std::endl;
}
return 0;
}