为什么 WaitForSingleObject() 认为提供的句柄无效?

Why is WaitForSingleObject() considering the supplied handle to be invalid?

我正在使用 Visual Studio 2010 来处理尝试从标准输入读取的 Win32 应用程序。它从 GetStdHandle(STD_INPUT_HANDLE) 中检索一个有效的句柄,并立即调用 WaitForSingleObject() 并将该句柄作为参数,但 return 值始终为 WAIT_FAILED。我已经验证文件句柄的值为 01,这是奇数 bc stdin 通常为 0,stdout 为 1,stderr 为 2,所以这可能是一个重要的线索。

当我使用 "Error Lookup" 工具时,代码 (6) 表示句柄无效。在 VS 输出 window 中,我从下面的代码中得到 "WAIT_FAILED. GetLastError() returned: 6"。非常感谢任何帮助。

   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 说:

The handle has GENERIC_READ and GENERIC_WRITE access rights

WaitForSingleObject 说:

The handle must have the SYNCHRONIZE access right.