I'm confused by the Windows API. I try to see if there are bytes left in an anonymous pipe using PeekNamedPipe. This function fails, but GetLastError() does not return an error.

The relevant part of my code is

if( !PeekNamedPipe( handle, NULL, 0, NULL, &numBytesAvailable, NULL ) )
  throw PeekNamedPipeException( "GetLastError() returned " + std::to_string( GetLastError() ) );

This throws an error, generating the following message:

terminate called after throwing an instance of 'PeekNamedPipeException'
  what():  GetLastError() returned 0

But if PeekNamedPipe fails, GetLastError should not return 0 (== ERROR_SUCCESS), right?

Thank you for your help!

有帮助吗?

解决方案

It's probably because more system functions are called (behind the scenes) between the PeekNamedPipe and your call to GetLastError.

Store the value from GetLastError in a temporary variable, and use that when constructing the string.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top