一个进程以独占方式打开CreateFile某个文件或设备后,是不是其他进程就没办法再访问该设备或文件了?除非等该进程退出?

解决方案 »

  1.   

    应该和进程无关吧,看CreateFile时的打开方式,如果没有设置Share标记,那么再次对同一文件或设备进行CreateFile将会失败,除非先前成功返回的Handle已经被关闭释放掉。
      

  2.   

    通常而言是的。但是可以通过DuplicateHandle远程关掉句柄或者Hook IoCheckShareAccess强行打开。
      

  3.   

    那就算设置了CreateFile时的独占方式也是不安全的了?那咋办?我如何实现我自己的进程独享该设备呢??
    CreateFile等函数是否是要经过文件系统和磁盘设备驱动?
    我能否绕过这些驱动,直接读写设备?
      

  4.   

    windows下很难保证你的要求,因为:1 你无法保证你的进程不会被恶意关闭2 你无法阻止别的进程关闭该句柄3 你无法阻止别人直接用磁盘IO直接从磁盘扇区读取文件信息4 你无法阻止别人将硬盘卸下后挂在其他PC上分析文件5 你无法阻止别人接管boot引导程序,在开机引导时对文件做操作
      

  5.   

    各种各样的ws方法导致很难在CreateFile的时候保证独占访问。不过你可以在设备驱动里面过滤请求,只处理你自己的exe的请求。
      

  6.   

    用CreateFile独占打开一个设备或文件后并获得句柄后,如何监控该句柄不被其他程序再次打开?直到我把它CloseHandle().才允许使用。
      

  7.   

    正常情况下,createFile独占打开后,其他程序再打开它将返回失败,直到你CloseHandle它,
    但是,如果你想禁止不正常情况,你可能需要在设备驱动里面过滤这些请求,具体的我也不知道
    帮你顶顶吧
      

  8.   

    dwShareMode [in] 
    The requested sharing mode of the file or device, which can be read, write, both, delete, all of these, or none (refer to the following table). Access requests to attributes or extended attributes are not affected by this flag.If this parameter is zero and CreateFile succeeds, the file or device cannot be shared and cannot be opened again until the handle to the file or device is closed. For more information, see the Res section.You cannot request a sharing mode that conflicts with the access mode that is specified in an existing request that has an open handle. CreateFile would fail and the GetLastError function would return ERROR_SHARING_VIOLATION.To enable a process to share a file or device while another process has the file or device open, use a compatible combination of one or more of the following values. For more information about valid combinations of this parameter with the dwDesiredAccess parameter, see Creating and Opening Files.Note  The sharing options for each open handle remain in effect until that handle is closed, regardless of process context.Value Meaning 
    0
    0x00000000 Prevents other processes from opening a file or device if they request delete, read, or write access.