1 回答
TA贡献1921条经验 获得超9个赞
实际上可以WriteFile直接使用 Win32 API 编写探测零长度字节字符串:如果管道关闭,那么这将返回“不成功”(false/0),如果管道是活着,然后“成功”(真/!= 0)。正是我所要求的。因为我发现这比使用更简单,NtQueryInformationFile所以我现在使用空写方法;这是一个简化的示例:
import ctypes
from ctypes import byref, c_ulong, c_char_p
import msvcrt
kernel32 = ctypes.WinDLL('kernel32', use_last_error=True)
fifo = open('//./pipe/somepipe', 'wb')
data = b''
written = c_ulong(0)
if not kernel32.WriteFile(
msvcrt.get_osfhandle(fifo.fileno()),
c_char_p(data), 0,
byref(written),
None):
last_error = ctypes.get_last_error()
if last_error in (
0x000000E8, # ERROR_NO_DATA
# enable as required: 0x000000E9, # ERROR_PIPE_NOT_CONNECTED
):
# pipe broken
pass
else:
# something else is wrong...
pass
else:
# pipe still okay
pass
添加回答
举报