Cannot open memory mapped file from login screen

I am trying to use memory mapped files to exchange data between windows (written in C #) and a splash screen written in C ++.

Although this works well if the screensaver works in the context of a registered user, it does not work if the screensaver works on the login screen. Instead, OpenFileMapping crashes with an unauthorized access error. It seems obvious that I don’t have an access rule (or maybe a system setup to do this), but I can’t figure out which one.

My C # code:

  public CreateMemMappedFile(string fileName) { this.rootName = fileName; var security = new MemoryMappedFileSecurity(); security.AddAccessRule(new System.Security.AccessControl.AccessRule<MemoryMappedFileRights>( new SecurityIdentifier(WellKnownSidType.WorldSid, null), MemoryMappedFileRights.FullControl, System.Security.AccessControl.AccessControlType.Allow)); security.AddAccessRule(new System.Security.AccessControl.AccessRule<MemoryMappedFileRights>( new SecurityIdentifier(WellKnownSidType.AnonymousSid, null), MemoryMappedFileRights.FullControl, System.Security.AccessControl.AccessControlType.Allow)); security.AddAccessRule(new System.Security.AccessControl.AccessRule<MemoryMappedFileRights>( new SecurityIdentifier(WellKnownSidType.NullSid, null), MemoryMappedFileRights.FullControl, System.Security.AccessControl.AccessControlType.Allow)); // printing the SID from the screensaver shows that we are running as Local Service security.AddAccessRule(new System.Security.AccessControl.AccessRule<MemoryMappedFileRights>( new SecurityIdentifier(WellKnownSidType.LocalServiceSid, null), MemoryMappedFileRights.FullControl, System.Security.AccessControl.AccessControlType.Allow)); this.handle = MemoryMappedFile.CreateOrOpen( "Global\\" + fileName, 1024 * 1024, MemoryMappedFileAccess.ReadWriteExecute ,MemoryMappedFileOptions.DelayAllocatePages, security, System.IO.HandleInheritability.Inheritable ); } 

My screensaver code:

  TCHAR* myFileName = _T("Global\\myfile"); ipcBufferHandle = OpenFileMapping(FILE_MAP_READ,FALSE,myFileName); if(ipcBufferHandle == NULL) { _tprintf(_T("couldn't open file \"%s\" with error code %d\n"),myFileName,GetLastError()); } else { _tprintf(_T("opened file %s\n"),myFileName); } 

which outputs couldn't open file "Global\myfile" with error code 5

+4
source share
1 answer

As indicated by msdn, OpenFileMapping is defined as

 HANDLE WINAPI OpenFileMapping( _In_ DWORD dwDesiredAccess, _In_ BOOL bInheritHandle, _In_ LPCTSTR lpName ); 

Therefore, when you call bInheritHandle with FALSE , the handle is not shared with other processes. Try TRUE , as in C # with System.IO.HandleInheritability.Inheritable .

 ipcBufferHandle = OpenFileMapping(FILE_MAP_READ, TRUE, myFileName); 
0
source

Source: https://habr.com/ru/post/1495407/


All Articles