PeekNamedPipe 始终为totalBytesAvailable 返回 0
PeekNamedPipe(
tmp_pipe, // __in HANDLE hNamedPipe,
NULL, // __out_opt LPVOID lpBuffer,
0, // __in DWORD nBufferSize,
NULL, // __out_opt LPDWORD lpBytesRead,
&totalBytesAvailable, // __out_opt LPDWORD lpTotalBytesAvail,
NULL // __out_opt LPDWORD lpBytesLeftThisMessage
);
我已将字节写入其他地方的管道,但totalBytesAvailable始终为0
,为什么?
PeekNamedPipe(
tmp_pipe, // __in HANDLE hNamedPipe,
NULL, // __out_opt LPVOID lpBuffer,
0, // __in DWORD nBufferSize,
NULL, // __out_opt LPDWORD lpBytesRead,
&totalBytesAvailable, // __out_opt LPDWORD lpTotalBytesAvail,
NULL // __out_opt LPDWORD lpBytesLeftThisMessage
);
I have written bytes to the pipe somewhere else,but totalBytesAvailable is always 0
,why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我发现在 Windows 中,如果您在调用 ReadFile 之前调用 PeekNamedPipe,它将始终返回零字节,即使实际上有字节要读取。您必须调用
ReadFile
,然后调用PeekNamedPipe
,并继续循环,直到PeekNamedPipe
返回零字节。我注意到,即使在这些情况下,有时
PeekNamedPipe
返回零字节,即使还有剩余字节需要获取。一定是个时间问题。发送者必须在每条消息的前面加上字节数。叹...I have found that in Windows, if you call
PeekNamedPipe
before callingReadFile
, it will always return zero bytes, even if there are in fact bytes to be read. You have to callReadFile
, followed byPeekNamedPipe
, and keep looping untilPeekNamedPipe
returns zero bytes.I have noticed that even under these circumstances, sometimes
PeekNamedPipe
returns zero bytes even though there are bytes left to be gotten. Must be a timing thing. The sender is going to have to preface each message with a byte count. Sigh...这是一个老问题,但我没有在网上找到答案,所以我想无论如何我都会回答它。你必须循环直到管道读取,这是我的工作代码:
当然,只有当你确定有数据等待读取时,这才有效,否则你将陷入无限循环,因为实际上没有数据要读取读取,所以它永远是0。
It's an old question but I haven't found the answer online so I figured I'd answer it anyway. You have to loop until the pipe reads, here's my working code:
Of course, this only works if you are sure there is data waiting to be read, otherwise you will be stuck in an endless loop because there isn't actually data to be read, so it will always be 0.