PeekNamedPipe 始终为totalBytesAvailable 返回 0

发布于 2024-09-18 11:45:52 字数 506 浏览 5 评论 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心房敞 2024-09-25 11:45:52

我发现在 Windows 中,如果您在调用 ReadFile 之前调用 PeekNamedPipe,它将始终返回零字节,即使实际上有字节要读取。您必须调用 ReadFile,然后调用 PeekNamedPipe,并继续循环,直到 PeekNamedPipe 返回零字节。

我注意到,即使在这些情况下,有时 PeekNamedPipe 返回零字节,即使还有剩余字节需要获取。一定是个时间问题。发送者必须在每条消息的前面加上字节数。叹...

I have found that in Windows, if you call PeekNamedPipe before calling ReadFile, it will always return zero bytes, even if there are in fact bytes to be read. You have to call ReadFile, followed by PeekNamedPipe, and keep looping until PeekNamedPipe 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...

吃不饱 2024-09-25 11:45:52

这是一个老问题,但我没有在网上找到答案,所以我想无论如何我都会回答它。你必须循环直到管道读取,这是我的工作代码:

DWORD bytesAvail = 0;
while(bytesAvail==0){
    if( !PeekNamedPipe(pipeHandle, NULL, 0, NULL, &bytesAvail, NULL) ){
        printf("PeekNamedPipe error %d.\n", GetLastError()); //error check
    }
} 
printf("Bytes available: %d\n", bytesAvail);

当然,只有当你确定有数据等待读取时,这才有效,否则你将陷入无限循环,因为实际上没有数据要读取读取,所以它永远是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:

DWORD bytesAvail = 0;
while(bytesAvail==0){
    if( !PeekNamedPipe(pipeHandle, NULL, 0, NULL, &bytesAvail, NULL) ){
        printf("PeekNamedPipe error %d.\n", GetLastError()); //error check
    }
} 
printf("Bytes available: %d\n", bytesAvail);

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文