c++multithreadingwinapithread-synchronizationoverlapped-io

How to signal file HANDLE waiting with WaitForSingleObject


This code, which I have no control over, reads a file using overlapped I/O:

// Read file asynchronously
HANDLE hFile = CreateFile(..., FILE_FLAG_OVERLAPPED, ...);
BYTE buffer[10];
OVERLAPPED oRead = { 0 };
ReadFile(hFile, buffer, 10, NULL, &oRead);

// Do work while file is being read
...

// Wait for read to finish
WaitForSingleObject(hFile, INFINITE);

// ReadFile has finished 
// buffer now contains data which can be used
...

In another thread (actually in an API hook of ReadFile), I need to signal the hFile to unblock the WaitForSingleObject. Normally Windows (or the device driver handling the ReadFile) does this, but I need to simulate it.

None of the APIs I found that normally do this work with hFile, including ReleaseMutex, ReleaseSemaphore, and SetEvent. They all return Error 6 (handle is invalid). Is there an API that works with a file, named pipe, or communications device?

I know it is not recommended to WaitForSingleObject(hFile), but the above code is a given, and I need to work with it. Thanks!


Solution

  • So far as I know, signaling the file handle takes place internally to Windows, and there is no API even when running in kernel mode. (I believe the file system driver simply tells Windows that the operation is complete and lets Windows figure out how to notify the user-mode process. I may be wrong.)

    One resolution would be to issue a genuine ReadFile (a zero-byte read might be sufficient) against the handle in order to signal it.

    But it would probably be more sensible to hook WaitForSingleObject, check whether it is being called on the file handle in question, and if so modify the behaviour as appropriate.