I'm using Visual Studio 2010 to work on a Win32 app that attempts to read from stdin. It retrieves a valid handle from GetStdHandle(STD_INPUT_HANDLE) and immediately calls WaitForSingleObject() with the handle as a parameter, but the return value is always WAIT_FAILED. I have verified that the value of the file handle is 01, which is odd bc stdin is usually 0, stdout 1, and stderr 2, so this is probably an important clue.
When I used the "Error Lookup" tool the code (6) means that the handle is invalid. In the VS output window I get "WAIT_FAILED. GetLastError() returned: 6" from the code below. Any help greatly appreciated.
hStdIn = GetStdHandle( STD_INPUT_HANDLE );
XTrace (L"hStdIn: %ul\r\n", hStdIn );
if (INVALID_HANDLE_VALUE != hStdIn)
{
INPUT_RECORD inputRecord[512];
DWORD nNumBytesRead;
switch ( WaitForSingleObject( hStdIn, 1000 ) )
{
case( WAIT_TIMEOUT ):
XTrace (L"WAIT_TIMEOUT\r\n" );
break; // return from this function to allow thread to terminate
case( WAIT_OBJECT_0 ):
// clear events
ReadConsoleInput( hStdIn, inputRecord, 512, &nNumBytesRead );
XTrace (L"Called ReadConsoleInput(). WAIT_OBJECT_0\r\n" );
break;
case( WAIT_FAILED ):
XTrace (L"WAIT_FAILED. GetLastError() returned: %d\r\n", GetLastError() );
break;
case( WAIT_ABANDONED ):
XTrace (L"WAIT_ABANDONED\r\n" );
break;
default:
XTrace (L"Unexpected result from WaitForSingleObject\r\n" );
}
}
GetStdHandle
says:
The handle has GENERIC_READ and GENERIC_WRITE access rights
WaitForSingleObject
says:
The handle must have the SYNCHRONIZE access right.