I was recently scratching my head for some time while i was working with named pipes.
So in my setup, i had the "reader" and the "writer" ends of a pipe created in "PIPE_TYPE_MESSAGE | PIPE_READMODE_BYTE" mode. The writer called twice the WriteFile api to send some data to the reader. After the second call to WriteFile by the writer, the reader used the PeekNamedPipe to peek at the data written by the writer. To my surprise, i was always getting only the data of the first WriteFile call even though the peek length included both writes.
After reading more carefully the documentation, i noticed the following paragraph in the remarks: "The data is read in the mode specified with CreateNamedPipe. For example, create a pipe with PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE. If you change the mode to PIPE_READMODE_BYTE with SetNamedPipeHandleState, ReadFile will read in byte mode, but PeekNamedPipe will continue to read in message mode."
So this appears to be related with my problem, it looks like the peek operation tries to read the data in message mode. The only issue is that i had the pipe opened with "PIPE_TYPE_MESSAGE | PIPE_READMODE_BYTE" which based on the documentation, should have allowed the peek operation to read all the data of the pipe.
Am i missing something? Is this a bug of the implementation, documentation or do i have a flaw in my thinking process?
I am also curious to hear the logic behind this design decision of peek operation, as it doesn't look like it makes much sense.
It is noted that i was using Windows 21H1, but the same behaviour was observed in all the versions i have experimented with.
Thanks