简体   繁体   English

MemoryMappedFile.CreateFromFile始终抛出UnauthorizedAccessException

[英]MemoryMappedFile.CreateFromFile always throws UnauthorizedAccessException

I realize .NET 4.0 is in Beta, but I'm hoping someone has a resolution for this. 我意识到.NET 4.0处于测试阶段,但我希望有人能够解决这个问题。 I'm trying to create a memory mapped file from a DLL: 我正在尝试从DLL创建一个内存映射文件:

FileStream file = File.OpenRead("C:\mydll.dll");
using (MemoryMappedFile mappedFile = MemoryMappedFile.CreateFromFile(file,
    "PEIMAGE", 1024 * 1024, MemoryMappedFileAccess.ReadExecute))
{
    using (MemoryMappedViewStream viewStream = mappedFile.CreateViewStream())
    {
        // read from the view stream
    }
}

Unfortunately, no matter what I do, I always get an UnauthorizedAccessException , for which the MSDN documentation states: 不幸的是,无论我做什么,我总是得到一个UnauthorizedAccessExceptionMSDN文档说明了这一点:

The operating system denied the specified access to the file; 操作系统拒绝对文件的指定访问权限; for example, access is set to Write or ReadWrite, but the file or directory is read-only. 例如,access设置为Write或ReadWrite,但文件或目录是只读的。

I've monitored my application with Sysinternals Process Monitor, which shows that the file is indeed being opened successfully. 我用Sysinternals Process Monitor监视我的应用程序,它显示该文件确实正在成功打开。 I've also tried memory mapping other non-DLL files, but with the same result. 我也尝试过映射其他非DLL文件的内存,但结果相同。

Well, I've got an example based on the above which runs without exceptions. 好吧,我有一个基于上面的例子,没有例外。 I've made two important changes: 我做了两个重要的改变:

  • I'm only specified MemoryMappedFileAccess.Read when creating the MemoryMappedFile . 我在创建MemoryMappedFileAccess.Read时只指定了MemoryMappedFile You've opened it for read, so you can only read. 你打开它进行阅读,所以你只能阅读。 I haven't tried fixing it to allow execute as well by changing how the FileStream is opened. 我还没有尝试通过改变FileStream的打开方式来修复它以允许执行。
  • I've made the CreateViewStream call explicitly use MemoryMappedFileAccess.Read as well. 我已经使CreateViewStream调用显式地使用了MemoryMappedFileAccess.Read I'm not sure why it doesn't use the existing access rights by itself, but there we go. 我不确定为什么它本身不使用现有的访问权限,但我们去了。

Full program: 完整计划:

using System.IO;
using System.IO.MemoryMappedFiles;

class Test
{
    static void Main()
    {
        FileStream file = File.OpenRead("Test.cs");
        using (MemoryMappedFile mappedFile = MemoryMappedFile.CreateFromFile
               (file, "PEIMAGE", file.Length, MemoryMappedFileAccess.Read, null, 0, false))
        {
            using (var viewStream = mappedFile.CreateViewStream
                   (0, file.Length, MemoryMappedFileAccess.Read))
            {
                // read from the view stream
            }
        }
    }
}

I had the same behaviour when calling the CreateViewAccessor(...) method. 调用CreateViewAccessor(...)方法时,我有相同的行为。

Turns out, the error was only thrown when the size parameter exceeded the length of the file (it's not the same behaviour as we're used to with streams where size is a maximum value, instead it appears to take the parameter as a literal and the result is an attempt to read past the end of the file). 事实证明,只有当size参数超出文件的长度时才会抛出错误(这与我们习惯使用的大小是最大值的流的行为不同,相反它似乎将参数作为文字和结果是尝试读取文件的末尾)。

I fixed my problem by checking that the size doesn't exceed the size of the open file. 我通过检查大小是否超过打开文件的大小来解决我的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 FileOpenPicker PickSingleFileAsync抛出UnauthorizedAccessException - FileOpenPicker PickSingleFileAsync throws UnauthorizedAccessException NamedPipeClientStream 在 Connect 上抛出 UnauthorizedAccessException - NamedPipeClientStream throws UnauthorizedAccessException on Connect FileOpenPicker引发UnauthorizedAccessException - FileOpenPicker throws UnauthorizedAccessException 谷歌 API .NET 抛出 UnauthorizedAccessException - Google API .NET Throws UnauthorizedAccessException MemoryMappedFile CreateViewAccessor抛出“没有足够的存储空间来处理此命令。” - MemoryMappedFile CreateViewAccessor throws “Not enough storage is available to process this command.” .net 4.0使用全局上下文创建MemoryMappedFile会引发异常 - .net 4.0 creating a MemoryMappedFile with global context throws exception C# 注册表 SetValue 抛出 UnauthorizedAccessException - C# Registry SetValue throws UnauthorizedAccessException ContactManager.RequestStoreAsync()抛出System.UnauthorizedAccessException - ContactManager.RequestStoreAsync() throws System.UnauthorizedAccessException GetAccessControl抛出UnauthorizedAccessException访问SystemData目录 - GetAccessControl throws UnauthorizedAccessException accessing SystemData directory 下载文件引发 UnauthorizedAccessException net core 2 - Downloading a file throws UnauthorizedAccessException net core 2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM