public static unsafe bool Read(string name, out string content) { bool flag; content = null; SafeFileMappingHandle fileMapping = UnsafeNativeMethods.OpenFileMapping(4, false, @"Global\" + name); int error = Marshal.GetLastWin32Error(); if (fileMapping.IsInvalid) { fileMapping.SetHandleAsInvalid(); fileMapping.Close(); if (error != 2) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new Win32Exception(error)); } return(false); } try { SafeViewOfFileHandle handle2; if (!GetView(fileMapping, false, out handle2)) { flag = false; } else { try { SharedMemoryContents *handle = (SharedMemoryContents *)handle2.DangerousGetHandle(); content = handle->isInitialized ? handle->pipeGuid.ToString() : null; flag = true; } finally { handle2.Close(); } } } finally { fileMapping.Close(); } return(flag); }
public static bool Read(string name, out string content) { content = null; SafeFileMappingHandle fileMapping = UnsafeNativeMethods.OpenFileMapping(UnsafeNativeMethods.FILE_MAP_READ, false, ListenerConstants.GlobalPrefix + name); int errorCode = Marshal.GetLastWin32Error(); if (fileMapping.IsInvalid) { fileMapping.SetHandleAsInvalid(); fileMapping.Close(); if (errorCode == UnsafeNativeMethods.ERROR_FILE_NOT_FOUND) { return(false); } throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new Win32Exception(errorCode)); } try { SafeViewOfFileHandle view; if (!GetView(fileMapping, false, out view)) { return(false); } try { SharedMemoryContents *contents = (SharedMemoryContents *)view.DangerousGetHandle(); content = contents->isInitialized ? contents->pipeGuid.ToString() : null; return(true); } finally { view.Close(); } } finally { fileMapping.Close(); } }
public static unsafe SharedMemory Create(string name, Guid content, List <SecurityIdentifier> allowedSids) { int errorCode = UnsafeNativeMethods.ERROR_SUCCESS; byte[] binarySecurityDescriptor = SecurityDescriptorHelper.FromSecurityIdentifiers(allowedSids, UnsafeNativeMethods.GENERIC_READ); SafeFileMappingHandle fileMapping; UnsafeNativeMethods.SECURITY_ATTRIBUTES securityAttributes = new UnsafeNativeMethods.SECURITY_ATTRIBUTES(); fixed(byte *pinnedSecurityDescriptor = binarySecurityDescriptor) { securityAttributes.lpSecurityDescriptor = (IntPtr)pinnedSecurityDescriptor; fileMapping = UnsafeNativeMethods.CreateFileMapping((IntPtr)(-1), securityAttributes, UnsafeNativeMethods.PAGE_READWRITE, 0, sizeof(SharedMemoryContents), name); errorCode = Marshal.GetLastWin32Error(); } if (fileMapping.IsInvalid) { fileMapping.SetHandleAsInvalid(); fileMapping.Close(); throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new Win32Exception(errorCode)); } SharedMemory sharedMemory = new SharedMemory(fileMapping); SafeViewOfFileHandle view; // Ignore return value. GetView(fileMapping, true, out view); try { SharedMemoryContents *contents = (SharedMemoryContents *)view.DangerousGetHandle(); contents->pipeGuid = content; Thread.MemoryBarrier(); contents->isInitialized = true; return(sharedMemory); } finally { view.Close(); } }