我无法控制的这段代码使用重叠 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
...
在另一个线程中(实际上是在 ReadFile 的 API 挂钩中),我需要发出信号hFile
来解除对WaitForSingleObject
. 通常 Windows(或处理 的设备驱动程序ReadFile
)会这样做,但我需要模拟它。
我发现没有一个 API 通常可以与 . 一起使用hFile
,包括ReleaseMutex
、ReleaseSemaphore
和SetEvent
. 它们都返回错误 6(句柄无效)。是否有适用于文件、命名管道或通信设备的 API?
我知道不建议这样做WaitForSingleObject(hFile)
,但上面的代码是给定的,我需要使用它。谢谢!