/// <summary> /// 创建文件映射对象,并映射(内存)视图。 /// </summary> /// <param name="mappingName">文件映射对象的名称,可为null/empty。</param> /// <param name="pageAccessRights">内存页面访问权限。</param> /// <param name="mappingAccessRights">文件映射的访问权限。</param> /// <param name="mappingOffset">文件映射的偏移量(起始位置)。</param> /// <param name="viewSize">文件映射的视图大小,单位:字节。</param> protected void InternalCreate(string mappingName, WIN32_API.PageAccess pageAccessRights, WIN32_API.FileMappingAccess mappingAccessRights, ulong mappingOffset, uint viewSize) { SafeFileMappingHandle handle = null; SafeViewOfFileHandle view = null; handle = CreateMapping(mappingName, pageAccessRights, mappingAccessRights, mappingOffset, ref viewSize); view = WIN32_API.MapViewOfFile(handle, mappingAccessRights, (uint)(mappingOffset >> 32), (uint)mappingOffset, viewSize); if (view.IsInvalid) { handle.Dispose(); handle = null; throw new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error()); } m_mappingName = mappingName; m_mappingHandle = handle; m_mappingView = view; m_mappingOffset = mappingOffset; m_viewSize = viewSize; }
/// <summary> /// 创建文件映射对象,并映射视图。 /// </summary> /// <param name="mappingName">文件映射对象的名称,可为null/empty。</param> /// <param name="pageAccessRights">内存页面访问权限。</param> /// <param name="mappingAccessRights">文件映射的访问权限。</param> /// <param name="viewSize">文件映射的视图大小,单位:字节。</param> public void Create(string mappingName, WIN32_API.PageAccess pageAccessRights, WIN32_API.FileMappingAccess mappingAccessRights, uint viewSize) { if (m_mappingView != null && !m_mappingView.IsInvalid) { throw new InvalidOperationException("SharedMemory already created."); } InternalCreate(mappingName, pageAccessRights, mappingAccessRights, 0, viewSize); }
/// <summary> /// 创建文件映射。 /// </summary> /// <param name="mappingName">文件映射对象的名称。</param> /// <param name="pageAccessRights">页面访问权限。</param> /// <param name="mappingAccessRights">文件映射的访问权限。</param> /// <param name="mappingOffset">文件映射的偏移量(起始位置)。</param> /// <param name="viewSize">文件映射的视图大小。</param> /// <exception cref="System.ComponentModel.Win32Exception">创建文件映射失败时。</exception> protected virtual SafeFileMappingHandle CreateMapping(string mappingName, WIN32_API.PageAccess pageAccessRights, WIN32_API.FileMappingAccess mappingAccessRights, ulong mappingOffset, ref uint viewSize) { SafeFileMappingHandle result = null; result = WIN32_API.CreateFileMapping(new SafeFileHandle(WIN32_API.InvalidHandleValue, true), IntPtr.Zero, pageAccessRights, 0, viewSize, mappingName); if (result.IsInvalid) { throw new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error()); } return(result); }
/// <summary> /// 创建文件映射。 /// </summary> /// <param name="mappingName">文件映射对象的名称。</param> /// <param name="pageAccessRights">页面访问权限。</param> /// <param name="mappingAccessRights">文件映射的访问权限。</param> /// <param name="mappingOffset">文件映射的偏移量(起始位置)。</param> /// <param name="viewSize">[in, out]文件映射的视图大小,等于0时表示按文件实际大小取值。</param> /// <exception cref="System.ComponentModel.Win32Exception">创建文件映射失败时。</exception> protected override SafeFileMappingHandle CreateMapping(string mappingName, WIN32_API.PageAccess pageAccessRights, WIN32_API.FileMappingAccess mappingAccessRights, ulong mappingOffset, ref uint viewSize) { SafeFileMappingHandle result = null; WIN32_API.GenericRight genericRights = 0; if ((m_accessRights & FileAccess.Read) == FileAccess.Read) { genericRights |= WIN32_API.GenericRight.GENERIC_READ; } if ((m_accessRights & FileAccess.Write) == FileAccess.Write) { genericRights |= WIN32_API.GenericRight.GENERIC_WRITE; } SafeFileHandle handle = null; try { Debug.Assert(!String.IsNullOrEmpty(m_filePath)); handle = WIN32_API.CreateFile(m_filePath, genericRights, m_shareMode, IntPtr.Zero, m_creationMode, FileAttributes.Normal, IntPtr.Zero); if (handle.IsInvalid) { throw new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error()); } ulong actualFileSize = 0; { uint fileSizeHigh = 0; uint fileSizeLow = WIN32_API.GetFileSize(handle, out fileSizeHigh); if (fileSizeLow == uint.MaxValue) { throw new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error()); } actualFileSize = (((ulong)fileSizeHigh) << 32) + fileSizeLow; } ulong expectedFileSize = 0; if (viewSize == 0) { if (actualFileSize == 0) { throw new ArgumentException("The actual file size is zero, Can't set the value of MappingSize."); } if (actualFileSize <= mappingOffset) { throw new ArgumentOutOfRangeException("MappingOffset is greater than the actual file size, Can't set the value of MappingSize."); } if ((actualFileSize - mappingOffset) > uint.MaxValue) { throw new ArgumentException("The actual file size is too large to set the value of MappingSize."); } viewSize = (uint)(actualFileSize - mappingOffset); expectedFileSize = actualFileSize; } else { Debug.Assert(ulong.MaxValue - mappingOffset >= viewSize); expectedFileSize = mappingOffset + viewSize; } // 不需处理,创建FileMapping时,会自动扩大 //if (expectedFileSize < actualFileSize) //{ // // Resize File //} result = WIN32_API.CreateFileMapping(handle, IntPtr.Zero, pageAccessRights, (uint)(expectedFileSize >> 32), (uint)expectedFileSize, mappingName); if (result.IsInvalid) { throw new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error()); } } catch (Exception) { if (handle != null && !handle.IsInvalid) { handle.Dispose(); handle = null; } throw; } Debug.Assert(m_fileHandle == null); m_fileHandle = handle; return(result); }