views:

257

answers:

2

What happens if I ReadFile() 10 bytes (in overlapped mode without a timeout) but invoke CancelIo() after 5 bytes have been read? The documentation for CancelIo() says that it cancels any pending I/O, but what happens to the 5 bytes already read? Are they lost? Are they re-enqueued so the next time I ReadFile() I'll get them again?

I'm looking for the specification to indicate one way or another. I don't want to rely on empirical evidence.

+1  A: 

According to http://groups.google.ca/group/microsoft.public.win32.programmer.kernel/browse_thread/thread/4fded0ac7e4ecfb4?hl=en

It depends on how the driver writer implemented the device. The exact semantics of cancel on an operation are not defined to that level.

Gili
A: 

Either it doesn't matter because you are using overlapped I/O or you can just call SetFilePointer manually when you know you've cancelled I/O.

You don't have to rely on undocumented behavior if you just force the issue.

MSN
"(in overlapped mode without a timeout)" + "you can just call SetFilePointer" --> SetFilePointer works on COM ports?
Windows programmer
If it's a COM port then you're already in trouble.
MSN
I think Gili already knew about trouble, but wondered what situation to expect in order to recover from it. "I'm looking for the specification to indicate one way or another. I don't want to rely on empirical evidence." If empirical evidence is the only way, too bad. MSN, do you know the answer?
Windows programmer
You have to force the issue (i.e., communicate outside the API to guarantee what you need). Clearly you can't expect the API to define that because it would be prohibitive for anyone implementing the file interface, say, in a driver.
MSN
"Either it doesn't matter because you are using overlapped I/O"... Why doesn't it matter if you are using overlapped I/O?
Gili
If you are doing overlapped I/O you have to explicitly pass in the offset to read from in the OVERLAPPED structure, so you can just re-run the I/O from the same offset passing in the same buffer.
MSN
@MSN, not true if you're reading from a serial port.
Gili
If you really want to get specific, you can use the lpNumberOfBytesTransferred parameter from `GetOverlappedResult` to determine how many bytes were transferred with that OVERLAPPED structure. But in general, as I said before, the API doesn't expose that information, so you can't expect to get it through the API.
MSN