2

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.

thetemplar
  • 97
  • 2
  • 3
  • 9

1 Answers1

1

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.

usr
  • 168,620
  • 35
  • 240
  • 369
  • I changed the Filestreams to `new FileStream(@"data.mmf", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite)`, but now I get another exception: Access to the path is denied. – thetemplar Aug 11 '15 at 12:17
  • Post the full exception ToString. And delete your security code. – usr Aug 11 '15 at 12:18
  • An unhandled exception of type 'System.UnauthorizedAccessException' occurred in System.Core.dll Additional information: Access to the path is denied. – thetemplar Aug 11 '15 at 12:20
  • That means file security does not allow access. Fix that. Probably a remnant of a previous experiment of yours. Maybe you should try googling more for error messages. This is not hard to figure out. – usr Aug 11 '15 at 12:22
  • I already searched the internet for that and found solutions like stackoverflow.com/questions/12358568/ but they also return the access denied exception, and for that exception I dont find a solution. – thetemplar Aug 11 '15 at 12:43
  • OK, what happened after trying out my suggestions from the previous comment. Use a new file name to be sure that no old security is lingering. – usr Aug 11 '15 at 13:07
  • 2
    The problem was that I tried to manage read and write permissions. Streams FileAccess and FileShare, also the MemoryMappedFileAccess have to be set to ReadWrite (and nothing else), even when only reading data. I dont get why, but it works now. – thetemplar Aug 11 '15 at 13:29