- 1 year ago
ReadFile and WriteFile with Overlapped IO result if not ERROR_IO_PENDING?
When using overlapped I/O with ReadFile
and WriteFile
in Windows, the return value of these functions can indicate different outcomes:
ReadFile
:- If
ReadFile
returnsTRUE
(non-zero), it means the operation completed synchronously, and the data was successfully read into the buffer. - If
ReadFile
returnsFALSE
and theGetLastError
function returnsERROR_IO_PENDING
, it means the operation was initiated asynchronously, and the data will be read into the buffer at a later time. You can use an I/O completion routine or other mechanisms to be notified when the operation completes.
- If
WriteFile
:- If
WriteFile
returnsTRUE
(non-zero), it means the operation completed synchronously, and the data was successfully written. - If
WriteFile
returnsFALSE
and theGetLastError
function returnsERROR_IO_PENDING
, it means the operation was initiated asynchronously, and the data will be written at a later time. You can use an I/O completion routine or other mechanisms to be notified when the operation completes.
- If
In the case of overlapped I/O, if the operation completes synchronously (returning TRUE
), you can immediately examine the contents of the buffer to access the read data or verify the successful write. However, if the operation is pending (ERROR_IO_PENDING
), you'll need to wait for the I/O completion notification to retrieve the results.
To be notified of completion, you can use techniques such as I/O completion ports, manual or automatic event objects, or using the GetOverlappedResult
function to check the status of the overlapped operation.
It's important to note that overlapped I/O can be a more complex programming model compared to synchronous I/O, as it involves managing asynchronous operation completion and handling the associated notification mechanisms.