Hello,
I'm using ReadFile on a serial port handle opened in overlapped mode. A buffer is pre-allocated with AllocateReadBuffer and passed to ReadFile. In this scenario one of two things will happen under normal circumstances:
1. The call will return immediately with ERROR_IO_PENDING status and I would then use GetOverlappedResult to wait for the operation to finish and determine how much of the buffer contains valid data.
2. The call may, in some cases, finish synchronously. The returned status is 0 in this case.
The problem is that, according to Microsoft's documentation, GetOverlappedResult should NOT be used in the second case. Here's the exact quote:
"If, on the other hand, an operation is completed immediately, then &NumberOfBytesRead passed into ReadFile is valid for the number of bytes read. In this case, ignore the OVERLAPPED structure passed into ReadFile; do not use it with GetOverlappedResult or WaitForSingleObject."
Source: http://support.microsoft.com/kb/156932
Unfortunately, the way ReadFile is implemented in win32file.i, there is no way for python code to get access to the numRead variable, as it is called there. That variable is used correctly when I pass in the number of bytes to read for the second argument, but not when using a pre-allocated buffer.
A simple solution would be to return numRead instead of the original buffer object when ReadFile finishes with a status of 0.
I should note that using GetOverlappedResult does seem to work in the second case, but I'm worried if there are some situations where using it would be a bad idea. With serial ports, a simple way of getting ReadFile to behave synchronously, even when an overlapped structure is passed in, is to set a read timeout of 0 and wait for some incoming data to be buffered. In this case, the driver will usually complete the request without going into asynchronous mode.