如何在Windows中异步打开文件 [英] How to open a file in windows asynchronously

查看:231
本文介绍了如何在Windows中异步打开文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以在Windows中异步打开文件? CreateFile API函数仅具有FILE_FLAG_OVERLAPPED,它允许进行进一步的异步读取和写入.尽管如此,文件的打开似乎是同步的.鉴于此,它必须访问文件系统(并可能执行昂贵的IO操作),它可能是潜在的阻止者.

Is there a way to open a file in Windows asynchronously? The CreateFile API function has only FILE_FLAG_OVERLAPPED which allows for further asynchronous reads and writes. Nonetheless, the opening of the file seems to be synchronous. Given, it has to access the file system (and potentially perform expensive IO operations), it may be a potential blocker.

这实际上是一个潜在的问题,即是否可以异步在.NET中打开文件(因为无法等待FileStream ctor).但是,如果没有办法在操作系统中做到这一点,问题就毫无意义了.

This is actually an underlying question to, whether it is possible to open a file in .NET asynchronously (as FileStream ctor cannot be awaited). But the question is rather pointless if there is no way to do it in the OS.

推荐答案

不幸的是,在用户模式下无法异步创建/打开文件.即使驱动程序为STATUS_PENDING"rel =" noreferrer"> IRP_MJ_CREATE ,在这种情况下,系统将一直等待,直到驱动程序完成IRP,然后才从创建/打开文件功能之一返回控制权.

Unfortunately there is no way in user mode to create/open a file asynchronously. Even if the driver returns STATUS_PENDING for IRP_MJ_CREATE, the system will be waiting in this case until the driver completes the IRP before it returns control from one of the create/open file functions.

仅当我们处于内核模式时,如果您自己设置

Only if we are in kernel mode it's possible, if you yourself format IRP_MJ_CREATE and send it to the driver. But even in this case the drivers will almost always handle IRP_MJ_CREATE synchronously.

API是异步的-操作完成后必须以某种方式通知调用方

for API be asynchronous - must be some way notify caller when operation finished

Windows为此使用了3种方式

windows used 3 ways for this

  1. 参数中的一些回调例程,通常是APC(PIO_APC_ROUTINE) 操作完成后会调用
  2. 一些参数事件,操作完成后,在 信号状态.
  3. 在api调用中使用的
  4. 文件句柄已绑定到某些IOCP.什么时候 操作完成的数据包排队到IOCP. (我们稍后通过调用 GetQueuedCompletionStatus (ZwRemoveIoCompletion)或 KeRemoveQueue
  1. some callback routine in parameters, usually APC (PIO_APC_ROUTINE) which called when operation finished
  2. some Event in parameters, when operation finished, Event set in signal state.
  3. file handle, used in api call, is binded for some IOCP. when operation finished packet is queued to IOCP. (we remove this packet later by call GetQueuedCompletionStatus (ZwRemoveIoCompletion) or KeRemoveQueue

3)在我们的情况下是不可能的,因为尚未创建文件句柄,因此无法将其绑定到任何IOCP.关于1)和2),让我们查找文件打开/创建api签名:

3) is impossible in our case because file handle yet not created, so it can not be bind to any IOCP. about 1) and 2) let looks for file open/create api signatures:

在用户模式下,用于打开/创建文件的最低级别的api是 CreateFile 是外壳在ZwCreateFile上.在内核模式NtOpenFile-> NtCreateFile-> IoCreateFile -> IoCreateFileEx 甚至- IoCreateFileEx 调用ObOpenObjectByName(未记录,但已导出例程)-这里也不是1)或2)参数-同样,这是通过设计api同步的

in user mode the lowest level api for open/create file is ZwOpenFile and ZwCreateFile. CreateFile is shell over ZwCreateFile. in kernel mode NtOpenFile -> NtCreateFile -> IoCreateFile -> IoCreateFileEx even - IoCreateFileEx (the most low level api for create file) - have no Event or [Apc] callback parameter - so not asynchronously. IoCreateFileEx call ObOpenObjectByName (not documented, but exported routine) - here also no 1) or 2) parameters - again this is synchronous by design api

这篇关于如何在Windows中异步打开文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆