Search code examples
winapinamed-pipes

How is it possible to repeatedly read from a NamedPipe in Windows?


How is it possible to repeatedly read from a NamedPipe in Windows? I get an 109 error, saying it could not open pipe, if I have a ReadFile() function after another ReadFile() function.


Solution

  • of course this is possible and need do after your pipe is connected and until disconnect. 109 this is ERROR_BROKEN_PIPE - you got this error in ReadFile when another end is close pipe handle, by call CloseHandle. in this case you need call DisconnectNamedPipe and then wait for new client by call ConnectNamedPipe. after connection is complete - you need just call ReadFile , in read completion again call ReadFile and so on until disconnect - some error returned. if you got error ERROR_PIPE_NOT_CONNECTED in ReadFile (just or in completion) this mean that remote end call DisconnectNamedPipe - your pipe already disconnected, so you can skip call to DisconnectNamedPipe and just call ConnectNamedPipe.