ReadDirectoryChangesW:异步使用时如何检测缓冲区溢出?
我将 ReadDirectoryChangesW (Windows API) 与 GetQueuedCompletionStatus 结合异步使用。如何检测可能的缓冲区溢出以了解至少一个文件系统更改事件已丢失?
I'm using ReadDirectoryChangesW (Windows API) asynchronously in combination with GetQueuedCompletionStatus. How can I detect a possible buffer overflow to understand that at least one file system change event has been lost?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当异步使用
ReadDirectoryChangesW
时,您将获得第一组事件,然后您必须再次调用它以获取更多事件。事件数量超过您的缓冲区的数量并不是一个错误。错误情况是事件数量超出了操作系统级缓冲区的容纳范围,您会发现如下情况:ReadDirectoryChangesW
启动的异步操作成功完成。您的缓冲区已填满,您的事件句柄已设置或 IOCP 已触发。ReadDirectoryChangesW
以开始异步重叠操作,检查自步骤 2 以来发生的任何事件。此调用同步失败,GetLastError() = = ERROR_NOTIFY_ENUM_DIR
,或成功并返回dwBytesTransferred == 0
,因为 文档说这也意味着重新-枚举目录When using
ReadDirectoryChangesW
asynchronously, you will get the first group of events, then you have to call it again for more events. Having more events than fit in your buffer is not an error. Having more events than fit in the OS-level buffer is the error condition, and you find out like so:ReadDirectoryChangesW
completes successfully. Your buffer is filled, your event handle is set or the IOCP is triggered.ReadDirectoryChangesW
again to begin an asynchronous overlapped operation checking for any events that happened since step 2. This call fails synchronously, withGetLastError() == ERROR_NOTIFY_ENUM_DIR
, or succeeds withdwBytesTransferred == 0
, since the documentation says this also means to re-enumerate the directory您可能无法通过这种方式完成检测,但是此处是一个很棒的教程,可能会有所帮助。
您还可以查看这个其他问题的答案。
You may not be able to accomplish your detections that way, but here is a great tutorial that may help.
You might also check out the answer to this other question.
从这里来看,似乎没有异步返回这样的错误代码。
建议:同步监视更改,但在专用线程中,并注意
ERROR_NOTIFY_ENUM_DIR
。Judging from here, it seems like there is no such error code returned asynchronously.
Suggestion: Monitor for changes synchronously, but in a dedicated thread, and watch out for
ERROR_NOTIFY_ENUM_DIR
.