Memory Mapped File Access Denied

情到浓时终转凉″ 提交于 2019-12-01 05:23:52

问题


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.



来源:https://stackoverflow.com/questions/14345274/memory-mapped-file-access-denied

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!