Overlapped structure required on overlapped file?

Question for all the smart guys out there:

I’ve read that on device files opened with FILE_FLAG_OVERLAPPED, all I/O to that file must be overlapped (have a valid OVERLAPPED structure). If this is in fact true, what is the reason behind it?

Thanks,
Josh

xxxxx@rtd.com wrote:

Question for all the smart guys out there:

I’ve read that on device files opened with FILE_FLAG_OVERLAPPED, all I/O to that file must be overlapped (have a valid OVERLAPPED structure). If this is in fact true, what is the reason behind it?

Do you mean, “why not allow asynchronous calls too”? It’s a reasonable
question.

Again, this is more of a design philosophy thing than a technical
barrier. There is no fundamental reason why that flag has to be part of
the global state for the file handle. They could have skipped the
FILE_FLAG_OVERLAPPED flag altogether, and taken a sync vs async path for
each request based strictly on the presence or absence of an OVERLAPPED
struct. But, they didn’t do that.

It’s also possible that the Win95 I/O subsystem DID have to take some
global action based on this flag.


Tim Roberts, xxxxx@probo.com
Providenza & Boekelheide, Inc.

Thanks for the response. I guess the real reason I’m asking this is because I’m using overlapped and non-overlapped I/O on a file opened with the overlapped flag. I’ve been running it this way for months and everything has been working great. I’m wondering if I shouldn’t be doing it and that I’ve just been really, really lucky so far.

Josh

Rush out and buy a lottery ticket. Your luck is good.

There are only a few requests such as read, write, and IoCtl that can have
an overlapped structure. If those are not being abused by skipping the
overlapped structure, then you should be OK, but if you have some without
it, it can fail at any time. Possibly some of the times require an
exception condition to occur before it would be a problem, but MSDN Library
is very specific about mixing overlapped and non-overlapped requests for a
file object that was opened in overlapped mode.

IIRC, someone from Microsoft posted to a newsgroup or a RSS feed about this
issue in the last couple of years. It may have been Doron Holon.

wrote in message news:xxxxx@ntdev…
> Thanks for the response. I guess the real reason I’m asking this is
> because I’m using overlapped and non-overlapped I/O on a file opened with
> the overlapped flag. I’ve been running it this way for months and
> everything has been working great. I’m wondering if I shouldn’t be doing
> it and that I’ve just been really, really lucky so far.
>
> Josh
>

> ----------

From: xxxxx@lists.osr.com[SMTP:xxxxx@lists.osr.com] on behalf of xxxxx@rtd.com[SMTP:xxxxx@rtd.com]
Reply To: Windows System Software Devs Interest List
Sent: Thursday, October 18, 2007 10:47 PM
To: Windows System Software Devs Interest List
Subject: RE:[ntdev] Overlapped structure required on overlapped file?

Thanks for the response. I guess the real reason I’m asking this is because I’m using overlapped and non-overlapped I/O on a file opened with the overlapped flag. I’ve been running it this way for months and everything has been working great. I’m wondering if I shouldn’t be doing it and that I’ve just been really, really lucky so far.

Yes, you shouldn’t do it. Instead, write simple synchronous wrappers around overlapped calls and use them instead of sync Win32 APIs directly. Works well for me.

If it worked for you, it probably means OS and drivers involved completed your requests quickly enough so you already had results available after sync API return. It can also mean you don’t check APIs results and use wrong data. Or maybe OS has an undocumented support for it and makes proper synchronization when overlapped structure isn’t passed to call (as one would expect…).

As for reasons, NT-based OSes are internally asynchronous for overlapped IO is the natural way of work. Win32 sync APIs cover it to make app programmers’ life easier. So if a programmer explicitly says s/he wants overlapped IO, designers probably expected s/he has a reason and knows what to do.

Best regards,

Michal Vodicka
UPEK, Inc.
[xxxxx@upek.com, http://www.upek.com]

xxxxx@rtd.com wrote:

Thanks for the response. I guess the real reason I’m asking this is because I’m using overlapped and non-overlapped I/O on a file opened with the overlapped flag. I’ve been running it this way for months and everything has been working great. I’m wondering if I shouldn’t be doing it and that I’ve just been really, really lucky so far.

You’ve been lucky. As the docs say, if you call an I/O function on an
FILE_FLAG_OVERLAPPED file handle without using an OVERLAPPED structure,
it’s possible for the API to return before the I/O request is really
complete.


Tim Roberts, xxxxx@probo.com
Providenza & Boekelheide, Inc.

Thanks for the clarification everyone. Looks like I have some fixes to make.

Josh