进程间通信之CreatePipe

发布时间:2025-12-09 13:42:25 浏览次数:4

CreatePipe function

创建匿名管道,返回读,写管道的handle。

BOOL WINAPI CreatePipe(
_Out_ PHANDLE hReadPipe,
_Out_ PHANDLE hWritePipe,
_In_opt_ LPSECURITY_ATTRIBUTES lpPipeAttributes,
_In_ DWORD nSize
);

参数:

hReadPipe [out]
返回对管道读的handle

hWritePipe [out]
返回对管道写的handle

lpPipeAttributes [in, optional]
指向SECURITY_ATTRIBUTES结构的指针。SECURITY_ATTRIBUTES决定了子进程是否可以继承管道的读写handle。
如果lpPipeAttributes是NULL,不能继承。

nSize [in]
管道的缓冲空间。只是一个建议值,系统会根据建议值计算出一个合适的值。如果nSize是0,使用缺省值。

返回值:
如果函数执行成功,返回值非0.
如果失败,返回0。可以通过GetLastError获得更多的信息。

说明:
CreatePipe创建包含适当大小缓冲空间的管道,返回通过ReadFile读, WriteFile写缓冲空间的handle.
ReadFile在如下情况返回:写操作完成,已读到请求大小的字节数,或者发生错误。
WriteFile只到写完所有的字节数才会返回。
若管道缓冲空间在写之前已经满了,那么WriteFile将一直不会返回直到ReadFile读管道后有足够的缓冲空间。
匿名管道是通过命名管道实现的。因此,你可以把匿名管道handle作为命名管道handle使用。
为了释放管道使用的资源,应用程序应该关闭那些不再使用的handles。

//in.txt

/*Creating a Child Process with Redirected Input and OutputThe example in this topic demonstrates how to create a child process using the CreateProcess function from a console process. It also demonstrates a technique for using anonymous pipes to redirect the child process's standard input and output handles. Note that named pipes can also be used to redirect process I/O.  PipServer                     PipClientg_hChildStd_IN_Wrg_hChildStd_IN_Rd/STD_INPUT_HANDLE  |----------------------->   anonymous pipe          <-----------------------|       g_hChildStd_OUT_Rdg_hChildStd_OUT_Wr/STD_OUTPUT_HANDLE*/

说明


1, http://msdn.microsoft.com/en-us/library/windows/desktop/aa365152(v=vs.85).aspx

2,http://msdn.microsoft.com/en-us/library/windows/desktop/ms682499(v=vs.85).aspx

需要做网站?需要网络推广?欢迎咨询客户经理 13272073477