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!

Était-ce utile?

La 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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top