Question

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!

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top