Is the lpNumberOfBytesTransfered parameter of GetOverlappedResult valid when GetLastError returns ERROR_OPERATION_ABORTED?

StackOverflow https://stackoverflow.com/questions/19517842

Question

In my project I need to read from and write to a serial port (RS232). I am using overlapped IO and use two separate threads for reading and writing.

When I issue a write operation that does not complete immediately, I start waiting (WaitForMultipleObjects) on two events; the event assigned to the hEvent member of the overlapped structure, and a stop event. When I want to stop the thread, I signal the stop event by calling SetEvent(). This causes the wait function to return. Because the write operation is still pending I cancel the operation by calling CancelIO(). Then again, I wait for the operation to complete, now using GetOverlappedResult() with the bWait parameter set to True. When the operation completes, GetOverlappedResult() returns False and GetLastError() returns ERROR_OPERATION_ABORTED.

Now here's my question:

When GetOverlappedResult() returns False, and GetLastError() returns ERROR_OPERATION_ABORTED (indicating a completed, but canceled operation), is the lpNumberOfBytesTransfered parameter valid? In other words, does the lpNumberOfBytesTransfered give me the actual number of written bytes before it was canceled? Or is the lpNumberOfBytesTransfered parameter undefined in this case?

Was it helpful?

Solution

I will repeat Hans Passant's comment, because I think he gives an accurate answer to my question:

This is a driver implementation detail. There are many possible serial port drivers around, they often get emulated by a USB or BlueTooth device driver. Often of questionable quality. The standard Microsoft driver already says no, it forces the IRP.IOStatus.Information field to 0 when the IRP gets cancelled. Don't cancel I/O if you cannot afford data loss.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top