private SafeViewOfFileHandle GetView(bool writable) { SafeViewOfFileHandle handle = UnsafeNativeMethods.MapViewOfFile(this.fileMapping, writable ? 2 : 4, 0, 0, (IntPtr)sizeof(SharedMemoryContents)); if (handle.IsInvalid) { int error = Marshal.GetLastWin32Error(); handle.SetHandleAsInvalid(); throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreatePipeNameCannotBeAccessedException(error, this.pipeUri)); } return(handle); }
private static bool GetView(SafeFileMappingHandle fileMapping, bool writable, out SafeViewOfFileHandle handle) { handle = UnsafeNativeMethods.MapViewOfFile(fileMapping, writable ? 2 : 4, 0, 0, (IntPtr) sizeof(SharedMemoryContents)); int error = Marshal.GetLastWin32Error(); if (!handle.IsInvalid) { return true; } handle.SetHandleAsInvalid(); fileMapping.Close(); if (writable || (error != 2)) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new Win32Exception(error)); } return false; }
private unsafe void InitializeContents(Guid pipeGuid) { SafeViewOfFileHandle view = this.GetView(true); try { SharedMemoryContents *handle = (SharedMemoryContents *)view.DangerousGetHandle(); handle->pipeGuid = pipeGuid; Thread.MemoryBarrier(); handle->isInitialized = true; } finally { view.Close(); } }
static bool GetView(SafeFileMappingHandle fileMapping, bool writable, out SafeViewOfFileHandle handle) { handle = UnsafeNativeMethods.MapViewOfFile(fileMapping, writable ? UnsafeNativeMethods.FILE_MAP_WRITE : UnsafeNativeMethods.FILE_MAP_READ, 0, 0, (IntPtr)sizeof(SharedMemoryContents)); int errorCode = Marshal.GetLastWin32Error(); if (!handle.IsInvalid) { return true; } else { handle.SetHandleAsInvalid(); fileMapping.Close(); // Only return false when it's reading time. if (!writable && errorCode == UnsafeNativeMethods.ERROR_FILE_NOT_FOUND) { return false; } throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new Win32Exception(errorCode)); } }