Click to See Complete Forum and Search --> : Overlapped io


Sam Hobbs
March 18th, 2006, 07:50 PM
I have not used overlapped io but I think I am close to getting it working. I am currently getting the error:Overlapped I/O operation is in progress.The device is a USB device that requires that a DeviceIoControl be used initially to get the packet size. The following works; I get the correct data without errors.void DeviceControl(HANDLE hDevice, DWORD ControlCode, LPVOID OutBuffer, DWORD OutBufferSize) {
DWORD BytesReturned = 0;
OVERLAPPED Overlapped;
string Message;
int rv;
Overlapped.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
rv = DeviceIoControl(hDevice, ControlCode, 0, 0, OutBuffer,
OutBufferSize, &BytesReturned, &Overlapped);
if (!rv) {
GetErrorMessage(GetLastError(), Message);
cerr << "DeviceIoControl error " << Message << '\n';
}
else
if (!GetOverlappedResult(hDevice, &Overlapped, &BytesReturned, TRUE)) {
GetErrorMessage(GetLastError(), Message);
cerr << "DeviceControl OverlappedResult error " << Message << '\n';
}
CloseHandle(Overlapped.hEvent);
}Then I need to write some data but this is where I am getting the error that an Overlapped I/O operation is in progress.
DWORD BytesReturned = 0;
OVERLAPPED Overlapped;
string Message;
Overlapped.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
Overlapped.Offset = 0;
Overlapped.OffsetHigh = 0;
if (!WriteFile(Handle, &aPacket, BytesToWrite, &BytesReturned, &Overlapped)) {
GetErrorMessage(GetLastError(), Message);
cerr << "SendPacket WriteFile error " << Message << '\n';
return false;
}I realize that I might not be doing everything I am supposed to do, but this is just an initial test version. I will polish it up later but for now I want to do a minimum necessary. My version as it exists in this manner is an improvement over the vendor's sample for interfacing with the device.

There is not any io functions between the two I show here. Shouldn't the GetOverlappedResult be sufficient to ensure that the io has completed?

wildfrog
March 18th, 2006, 08:41 PM
If the write operation completes immediately your overlapped event will be signalled and WriteFile returns TRUE.

If the write operation is not completed immediately (which is often the case for asynchorouns/overlapped operations) the WriteFile function will return FALSE and GetLastError will report an ERROR_IO_PENDING. This is OK; when the operation eventually completes your overlapped event will be signaled.

- petter

Sam Hobbs
March 18th, 2006, 09:09 PM
Thank you, petter. I should have figured that out myself. I did not realize that "Overlapped I/O operation is in progress" is ERROR_IO_PENDING. I have read enough to know that ERROR_IO_PENDING is normal for this.

The main reason I was confused is because DeviceIoControl seems to work a little different, but I should have been able to figure this out. At least you were able to explain things well enough to get me going again.