Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

ReadFile/GetOverlappedResult not completing when called from a service running on Windows 7

431 views
Skip to first unread message

jonc...@gmail.com

unread,
Jul 11, 2014, 1:05:29 PM7/11/14
to
I'm debugging a problem with a program that tries to read data from a USB/HID device. It calls CreateFile with FILE_FLAG_OVERLAPPED during initialization (followed by various HidP_ calls). On a polling thread, it calls ReadFile followed by GetOverlappedResult (if ReadFile returns 0 and GetLastError returns ERROR_IO_PENDING).

The program works correctly if I run it as a "regular" exe (e.g. started from command-prompt); namely, the ReadFile/GetOverlappedResult calls return when there is I/O to be read from the device.

However, if I start it as a service on windows 7 (32-bit), it does not work correctly. I have confirmed via debugging the service that the CreateFile/initialization stuff all works correctly (returns success codes, valid handles, etc...), and ReadFile returns 0 followed by GetLastError returning ERROR_IO_PENDING. However, GetOverlappedResult never returns even though I'm sure there is data to be read from the device. If I change CreateFile from FILE_FLAG_OVERLAPPED to FILE_ATTRIBUTE_NORMAL, then it blocks in ReadFile forever instead of GetOverlappedResult. So, it seems like somehow the service doesn't communicate with the device correctly, although I don't get any errors... it just blocks forever.

One other note: the same program works when running as a service on windows XP. So, there seems to be some magic that I'm missing when trying to run as a service on Windows 7.

Any ideas would be much appreciated - thanks!
Message has been deleted
Message has been deleted

malkut...@gmail.com

unread,
Jan 25, 2015, 7:12:12 PM1/25/15
to
Hi, I had the same issue reading from a sata disk, even passing wait = FALSE to GetOverlappedResult().
Then I found this statement which made me think:
"If the hEvent member of the OVERLAPPED structure is NULL, the system uses the state of the hFile handle to signal when the operation has been completed."
(ref. https://msdn.microsoft.com/en-us/library/windows/desktop/ms683209%28v=vs.85%29.aspx)

"...the system uses the state of the hFile handle..." appears to be false under Win7,
The issue seems to be fixed by setting hEvent with an event (manual reset) created on purpose:

{
OVERLAPPED overlapped;

{
DWORD i = 1 + sizeof overlapped;

do {
((BYTE*)&overlapped)[--i] = 0;
} while (i);
}

overlapped.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);

...

ReadFile(..., &overlapped);
...
GetOverlappedResult(..., &overlapped, ...);
}

Note:
if file is created with FILE_FLAG_NO_BUFFERING, nNumberOfBytesToRead in ReadFile() MUST be a multiple of sys mem page (i.e. 4KB)
0 new messages