I have a named pipe server with a main loop where I am reading data from a pipe:
while True:
bytes_available = DWORD()
ret_code = windll.kernel32.PeekNamedPipe(pipe, None, 0, None, byref(bytes_available))
if ret_code == 0 and windll.kernel32.GetLastError() == ERROR_BROKEN_PIPE:
reconnect()
buf = create_string_buffer(bytes_available.value)
ret_code = windll.kernel32.ReadFile(pipe, byref(buf), bytes_available.value, None, overlapped_struct_ptr)
if ret_code != 0 and bytes_available.value > 0:
# process(buf.raw)
pass
elif ret_code == 0 and windll.kernel32.GetLastError() == ERROR_BROKEN_PIPE:
reconnect()
windll.kernel32.GetQueuedCompletionStatus(iocp, byref(bytes_transferred), byref(completion_key),
byref(overlapped_struct_ptr), INFINITE)
Sometimes it crashes on the create_string_buffer() step with the following message:
Process finished with exit code -1073741819 (0xC0000005)
This seems strange to me, because if I replace create_string_buffer(bytes_available.value) with (c_char*bytes_available.value)() everything works fine. And if we look at the create_string_buffer() implementation, we will see, that it allocates the buffer the same way:
def create_string_buffer(init, size=None):
"""create_string_buffer(aString) -> character array
create_string_buffer(anInteger) -> character array
create_string_buffer(aString, anInteger) -> character array
"""
if isinstance(init, (str, unicode)):
if size is None:
size = len(init)+1
buftype = c_char * size
buf = buftype()
buf.value = init
return buf
elif isinstance(init, (int, long)):
buftype = c_char * init
buf = buftype()
return buf
raise TypeError(init)
So why does it fail?
windll.kernel32.PeekNamedPipe()call in your code seems to be missing an argument, there should be six, msdn