GetOverlappedResult
Retrieves the result of an asynchronous (overlapped) operation on a specified file, named pipe, or communications device.
Syntax
#include <windows.h>
BOOL GetOverlappedResult(
HANDLE hFile,
LPOVERLAPPED lpOverlapped,
LPDWORD lpNumberOfBytesTransferred,
BOOL bWait
);
Parameters
| Name | Type | Description |
|---|---|---|
| hFile | HANDLE | Handle to the file or pipe. Must have been opened with FILE_FLAG_OVERLAPPED. |
| lpOverlapped | LPOVERLAPPED | Pointer to the OVERLAPPED structure used in the asynchronous operation. |
| lpNumberOfBytesTransferred | LPDWORD | Pointer to a variable that receives the number of bytes transferred. |
| bWait | BOOL | If TRUE, the function does not return until the operation has completed. If FALSE, the function returns immediately. |
Return value
If the function succeeds, the return value is TRUE. If it fails, the return value is FALSE. Call GetLastError for extended error information.
Remarks
- The OVERLAPPED structure must remain valid until the operation completes.
- When
bWaitis FALSE and the operation has not completed, the function returnsFALSEwithERROR_IO_INCOMPLETE. - For pipe handles, the function can be used after
ReadFileorWriteFilewith overlapped I/O.
Example
#include <windows.h>
#include <stdio.h>
int main()
{
HANDLE hPipe = CreateFileW(
L"\\\\.\\pipe\\MyPipe",
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,
NULL);
if (hPipe == INVALID_HANDLE_VALUE) {
printf("CreateFile failed: %lu\\n", GetLastError());
return 1;
}
char buffer[128];
DWORD bytesRead;
OVERLAPPED ov = {0};
ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
BOOL ok = ReadFile(hPipe, buffer, sizeof(buffer), NULL, &ov);
if (!ok && GetLastError() != ERROR_IO_PENDING) {
printf("ReadFile failed: %lu\\n", GetLastError());
return 1;
}
// Wait for the read to complete
ok = GetOverlappedResult(hPipe, &ov, &bytesRead, TRUE);
if (!ok) {
printf("GetOverlappedResult failed: %lu\\n", GetLastError());
} else {
printf("Read %lu bytes: %.*s\\n", bytesRead, (int)bytesRead, buffer);
}
CloseHandle(ov.hEvent);
CloseHandle(hPipe);
return 0;
}