Search code examples
c#memory-mapped-filesfile-security

C# MemoryMappedFile - Different Processes for Read & Write


I have some problems with MemoryMappedFiles in C#. They are working perfect when I am using only 1 process, but as soon as I try to access the mmf-file from different processes I get errors that this is permitted.

I have a data-logger, which writes incoming data into the mmf file:

private void WriteRawToFile(.....)
    {
        MemoryMappedFileSecurity security = new MemoryMappedFileSecurity();
        security.AddAccessRule(new AccessRule<MemoryMappedFileRights>(("Everyone"), MemoryMappedFileRights.FullControl, AccessControlType.Allow));

        using (MemoryMappedFile mmf = MemoryMappedFile.CreateFromFile(new FileStream(@"data.mmf", FileMode.OpenOrCreate), "mmf", Int32.MaxValue / 100, MemoryMappedFileAccess.ReadWriteExecute, security, HandleInheritability.Inheritable, false))
        {
            using (MemoryMappedViewAccessor view = mmf.CreateViewAccessor(size*rawId, size, MemoryMappedFileAccess.CopyOnWrite))
            {
                    view.WriteArray(....);
            }
        }
    }

Other threads (and the mainthread) read from this file (from another class):

private float[] ReadRawFromFile(....)
        {
            MemoryMappedFileSecurity security = new MemoryMappedFileSecurity();
            security.AddAccessRule(new AccessRule<MemoryMappedFileRights>(("Everyone"), MemoryMappedFileRights.FullControl, AccessControlType.Allow));
            using (MemoryMappedFile mmf = MemoryMappedFile.CreateFromFile(new FileStream(@"data.mmf", FileMode.OpenOrCreate), "mmf", Int32.MaxValue / 100, MemoryMappedFileAccess.ReadWriteExecute, security, HandleInheritability.Inheritable, false))
            {
                using (MemoryMappedViewAccessor view = mmf.CreateViewAccessor(size*rawId, size, MemoryMappedFileAccess.Read))
                {
                    view.ReadArray(....);
                }
            }
            return res;
        }

With this code I get an exception while reading:

Some or all identity references could not be translated.

Initally I tried it without the MemoryMappedFileSecurity:

using (MemoryMappedFile mmf = MemoryMappedFile.CreateFromFile("data.mmf", FileMode.OpenOrCreate , "mmf", Int32.MaxValue / 100, MemoryMappedFileAccess.ReadWriteExecute))
using (MemoryMappedFile mmf = MemoryMappedFile.CreateFromFile("data.mmf", FileMode.Open, "mmf", Int32.MaxValue / 100, MemoryMappedFileAccess.Read))

but then I got another Exception:

The process cannot access the file 'data.mmf' because it is being used by another process.

What do I do wrong, I couldn't find a solution so far.


Solution

  • This error message has nothing to do with security. It's about file sharing access to the same file.

    The CreateFromFile overload that you used always uses FileShare.None according to reflector. That does not seem useful. Use a better overload. Here, you can create the FileStream yourself and use the proper sharing mode.