c++ - Windows中的管道句柄如何关闭? (_pclose() 的 fclose())?

标签 c++ c winapi file pipe

如果我以这种方式在 Windows 中创建管道:

CreatePipe(hRead, hWrite, &sec_atr, NULL)

然后以这种方式从 hRead 生成 FILE *:

int fd = _open_osfhandle((intptr_t)hRead, _O_RDONLY|_O_TEXT);
FILE *pipe_read = _fdopen(fd, "rt");

那我该如何关闭这个文件(pipe_read)呢?

  1. fclose(pipe_read)
  2. _pclose(pipe_read)
  3. CloseHandle((HANDLE)_get_osfhandle(fileno(pipe_read)))

最佳答案

来自示例here , 使用 fclosefclose 的文档进一步阐明:

When these functions are used to close a stream, the underlying file descriptor and OS file handle (or socket) are closed, as well as the stream. Thus, if the file was originally opened as a file handle or file descriptor and is closed with fclose, do not also call _close to close the file descriptor; do not call the Win32 function CloseHandle to close the file handle; and do not call closesocket to also close the socket.

关于c++ - Windows中的管道句柄如何关闭? (_pclose() 的 fclose())?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5548273/

相关文章:

c++ - 使用 ns2 以混杂模式监控数据包

c++ - 为什么 getchar_unlocked() 比其他方法更快?

正文为 "dynamic"的 C++ lambda 表达式

c++ - 很多变量,没有嵌套循环的最佳方法

c - 数组向左旋转(递归)

c++ - 在 C/C++ 中是否有任何用于 html 验证的库?

c++ - C++中的按键事件

c++ - HitTest 矩形

c++ - 窗口 : detect same device on both bluetooth api and setupapi

c - 如何使用 C 发送和捕获发送到 PID 的信号