什么原因导致WriteFile返回ERROR_ACCESS_DENIED? [英] What causes WriteFile to return ERROR_ACCESS_DENIED?

查看:685
本文介绍了什么原因导致WriteFile返回ERROR_ACCESS_DENIED?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们目前面临的问题是调用 WriteFile (或者,而是CFile :: Write - 但是只是在内部调用WriteFile)导致Win32错误 5 ERROR_ACCESS_DENIED



(编辑:现在我们所有的是一个日志文件,指示CFile :: Write的源行,并包含错误ERROR_ACCESS_DENIED!)



(编辑:文件是本地驱动器,实际上是一个文件,而不是目录。)



现在, WriteFiles的文档没有真正帮助,并尝试一个简单的测试 - 应用程序产生以下结果:


  1. WriteFile 导致ERROR_ACCESS_DENIED,如果调用的文件句柄不是
  2. 会导致ERROR_ACCESS_DENIED,如果

    • 句柄无效或文件根本未打开

    • 访问权限 之后修改文件的标记。 (如果在打开文件之前修改了,那么我们就无法访问WriteFile,因为打开文件将失败。)

    • 过程/句柄(这将最多会导致错误 32 ERROR_SHARING_VIOLATION)。




      这让我们失去了这种情况,如果文件实际上是使用读取标志而不是写入标志来打开,这显然是这种调用失败的唯一可能性。然而,看看我们的代码,这似乎是不可能的。 (由于我们的跟踪,我们可以确保WriteFile失败,并且可以确认错误是ERROR_ACCESS_DENIED,我们不能是100.1% )



      有没有其他已知的情况,Wri​​teFile(CFile :: Write)会导致ERROR_ACCESS_DENIED?

      p>

      请注意:若要进一步澄清此问题的上下文:




      • 所有执行的测试都表示文件被打开时不能
      • 该文件位于本地驱动器上,而不在网络驱动器上。



      我应该补充说,我们在WIndows XP sp3上运行,应用程序是用Visual Studio 2005编译的。

      解决方案

      问题是


      WriteFile会返回
      ERROR_ACCESS_DENIED? >

      并在问题

      中说明



      1. WriteFile将导致
        ERROR_ACCESS_DENIED,如果它被调用
        为未打开的文件句柄
        写入

      在为开放标记添加进一步的日志记录后和另一个事件,结果是这是正确的。打开标志的日志显示,在错误点,文件对象用CFile :: modeRead打开,因此我们得到ERROR_ACCESS_DENIED。



      没有找到但奇怪的代码路径导致这一点,但这只是表明:永远不要相信你自己的代码。 : - )



      (哦,和btw。不是 :: WriteFile code> :: FlushFileBuffers API,但显然返回相同的错误。)


      We currently face the problem of a call to WriteFile (or, rather CFile::Write - but that just calls WriteFile internally) causing the Win32 error 5 ERROR_ACCESS_DENIED.

      (EDIT: Note that we can't repro the behavior. All we have at the moment is a logfile indicating the source line where the CFile::Write was and containing as error ERROR_ACCESS_DENIED!)

      (EDIT: The file is on a local drive and it is in fact a file and not a directory.)

      Now, WriteFiles's documentation doesn't really help, and experimenting with a simple test-app yields the following results:

      1. WriteFile will cause ERROR_ACCESS_DENIED if it is called for a file handle that is not opened for writing (i.e. is opened for reading only).
      2. It will not cause ERROR_ACCESS_DENIED if
        • The handle is not valid or the file isn't open at all
        • The access rights, or the write protected flag for the file are modified after the file has been opened by the process. (If these are modified before the file is opened, then we never get to WriteFile because opening the file will fail.)
        • The file is somehow locked by another process/handle (This will at best result in error 32 ERROR_SHARING_VIOLATION).

      That leaves us with the situation, that apparently the only possibility for this call to fail if the file was actually opened with the read flag instead of the write flag. However, looking at our code, this seems extremely unlikely. (Due to our tracing, we can be sure that WriteFile failed and we can be sure that the error is ERROR_ACCESS_DENIED, we cannot be 100.1% sure of the opening flags, because these are not traced out.)

      Are there any other known circumstances where WriteFile (CFile::Write) would cause an ERROR_ACCESS_DENIED?

      Note: To additionally clarify the context of this question:

      • The file was open, therefore it can't be a directory or somesuch
      • All tests I performed indicate that while the file is open it cannot be deleted, so the file should still have been there on the call to WriteFile
      • The file is located on a local drive and not on a network drive.

      I should add that we're running on WIndows XP sp3 and the app is compiled with Visual Studio 2005.

      解决方案

      The question was

      What causes WriteFile to return ERROR_ACCESS_DENIED?

      and I stated in the question

      1. WriteFile will cause ERROR_ACCESS_DENIED if it is called for a file handle that is not opened for writing (i.e. is opened for reading only).

      After adding further logging for the open flags and another incident, it turns out this was correct. The logging for the open flags shows that at the point of error, the file object was opened with CFile::modeRead and therefore we got ERROR_ACCESS_DENIED.

      Haven't found out yet which weird code path leads to this, but this just goes to show: Never trust your own code. :-)

      (Oh, and btw. It wasn't ::WriteFile that failed, but the ::FlushFileBuffers API, but apparently that returns the same error.)

      这篇关于什么原因导致WriteFile返回ERROR_ACCESS_DENIED?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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