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?
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?