Writergate: reading from the stdout of a child process

I have checked the Windows API ReadFile(). For a FILE_FLAG_OVERLAPPED file handle, ReadFile() can return ERROR_IO_PENDING. It means “the read has been queued, but not finished yet”, not an error condition.

The typical case to handle ERROR_IO_PENDING as a blocking read is to wait on the OVERLAPPED.hEvent.

OVERLAPPED ol = {0};
ol.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);

BOOL ok = ReadFile(hFile, buffer, size, NULL, &ol);
if (!ok) {
    if (GetLastError() == ERROR_IO_PENDING) {
        WaitForSingleObject(ol.hEvent, INFINITE);
        DWORD bytesRead;
        GetOverlappedResult(hFile, &ol, &bytesRead, TRUE);
    }
}