Memory Mapped File Access Denied

2019-05-09 04:31发布

问题:

I'm attempting to refactor an old piece of code that was cobbled together in a hurry into something a little more elegant.

There are two pieces of the project, a windows service and a form application which monitors the services activity.

To allow this communication I have decided to use a non-persistent Memory Mapped File.

Below is working code in the old project:

var security = new MemoryMappedFileSecurity();
security.AddAccessRule(new AccessRule<MemoryMappedFileRights>("everyone", MemoryMappedFileRights.FullControl,AccessControlType.Allow));

file = MemoryMappedFile.CreateOrOpen(@"Global\" + instance, bufferSize,
                     MemoryMappedFileAccess.ReadWrite,
                     MemoryMappedFileOptions.DelayAllocatePages, security,
                     HandleInheritability.Inheritable);

The above code is in a ctor that is run by both the Service and the Form, they use the exact same code and it works regardless of which creates the mmf.

Now I have this same code in a new project, but if the Service creates the mmf first then the Form gets an Access to path denied error, and if the Form creates the mmf the Service 'opens' his side fine, but neither can see the information written, which makes me believe that they are not actually looking at the same thing.

At this point I don't know where to start to debug the issue, i'm using the security rule and the 'Global\' namespace due to Session 0 Isolation.

I just cannot wrap my head around why it works in one but not the other.

Any advice on where to go from here would be appreciated. Also if more code is needed just let me know.

回答1:

So after much grief I have found the answer to my issue.

Apparently Visual Studios didn't like how I added the file that contains that code into my project. I simply did an 'Add Existing'.

To solve the issue I created a new class, and copy/pasted the code from the other file:

Everything works fine now.