4

Why does GetOverlappedResult need a file handle?

It seems like WaitForSingleObject(overlapped->hEvent) should be enough to wait on the event so that the overlapped I/O finishes, so what does GetOverlappedResult use the file handle for?

user541686
  • 205,094
  • 128
  • 528
  • 886
  • Well, it seems someone in the comments was wondering the same thing: `Is the first parameter (hFile) required? If you pass in NULL it seems to work correctly, yet it's annotated as an __in parameter, can someone please clarify whether this should in fact be an __optin parameter? kstarsmeare 5/18/2011` – chris May 24 '13 at 03:52
  • @chris: D'oh, yes, I didn't see that. I can't figure out why it's necessary either... – user541686 May 24 '13 at 03:54
  • 2
    This question is perfectly fine and I believe it can be answered. In fact it can be answered from the documentation. The part that says: *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. Use of file, named pipe, or communications-device handles for this purpose is discouraged. It is safer to use an event object because of the confusion that can occur when multiple simultaneous overlapped operations are performed on the same file, named pipe, or communications device.* – David Heffernan May 24 '13 at 08:12
  • @DavidHeffernan: Wow, I totally missed that, thanks a lot for pointing it out. Let's hope the question is reopened so that you can post it as an answer! :) – user541686 May 24 '13 at 08:21
  • Well, I cast my reopen vote already – David Heffernan May 24 '13 at 08:22
  • @DavidHeffernan: Yup same here. Not sure why people are so close-happy... – user541686 May 24 '13 at 08:28

1 Answers1

3

The remarks section of the documentation appears to explain when hFile is needed:

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. Use of file, named pipe, or communications-device handles for this purpose is discouraged. It is safer to use an event object because of the confusion that can occur when multiple simultaneous overlapped operations are performed on the same file, named pipe, or communications device. In this situation, there is no way to know which operation caused the object's state to be signaled.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490