Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why do I get an access denied exception when accessing a memory mapped file from c# but not from c++

I have a WCF service that uses named pipes. Apparently doesn't use the url for the name. It generates a GUID from the url, then stores it in a memory mapped file. So I wrote a C++ app that gets the name of the pipe from the memory mapped file, and that works great. Now I'm trying to make a c# app that gets the name of the pipe from the memory mapped file. here is the code that I'm using.

    private static string GetPipeName(string mappedFileName)
    {
        var mappedFile = MemoryMappedFile.OpenExisting(mappedFileName, MemoryMappedFileRights.Read);
        var bytes = new List<byte>();
        using (MemoryMappedViewStream s = mappedFile.CreateViewStream())
        {
            using (var b = new BinaryReader(s))
            {
                bytes = b.ReadBytes((int)b.BaseStream.Length).ToList();
            }

        }
        var sb = new StringBuilder();
        foreach (var b in bytes)
        {
            sb.Append(b.ToString("x2"));
            sb.Append(" ");
        }
        Console.WriteLine(sb.ToString());
        return sb.ToString();
    }

At the first using statement I get an UnauthorizedAccessException, that says "Access to the path is denied."

I was able to step through and confirm that the filename is the same that I use in the c++ app and that shows when I use the SysInternals tool Handle.exe

Why would the C# app get an access denied? As far as I can tell both are running as the same user.

like image 457
scott Avatar asked Aug 03 '12 18:08

scott


1 Answers

you need to use the following

CreateViewAccessor(Int64, Int64, MemoryMappedFileAccess);
like image 173
MethodMan Avatar answered Nov 13 '22 17:11

MethodMan