Skip to content

Instantly share code, notes, and snippets.

@AstraLuma
Created March 17, 2017 18:01
Show Gist options
  • Save AstraLuma/0a2dd66e53c61cc362200fa988da8169 to your computer and use it in GitHub Desktop.
Save AstraLuma/0a2dd66e53c61cc362200fa988da8169 to your computer and use it in GitHub Desktop.
Testing select-like with pipes on windows
import time
import os
import ctypes
import ctypes.wintypes
PeekNamedPipe = ctypes.windll.kernel32.PeekNamedPipe
PeekNamedPipe.restype = ctypes.wintypes.BOOL
def try_peek(pipe, desc=''):
lpTotalBytesAvail = ctypes.wintypes.DWORD(0)
if isinstance(pipe, int):
pipe = ctypes.wintypes.HANDLE(pipe)
status = PeekNamedPipe(
pipe,
None,
ctypes.wintypes.DWORD(0),
None,
ctypes.byref(lpTotalBytesAvail),
None,
)
print(status, ctypes.GetLastError(), lpTotalBytesAvail)
pin = ctypes.wintypes.HANDLE(0)
pout = ctypes.wintypes.HANDLE(0)
ctypes.windll.kernel32.CreatePipe(ctypes.byref(pout), ctypes.byref(pin), None, 4096)
print(pin, pout)
try_peek(pout, 'CreatePipe:Open-Empty')
pin, pout = os.pipe()
print(pin, pout)
try_peek(pout, 'os.pipe:Open-Empty')
# p.side_in.write(b'Hello')
# try_peek(p, 'Open-HasData')
# print(repr(p.side_out.read(4000)))
# try_peek(p, 'Open-Empty')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment