/// <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> /// 初始化SharedMemory类的新实例。 /// </summary> public SharedMemory() { m_mappingName = null; m_mappingHandle = null; m_mappingView = null; m_mappingOffset = 0; m_viewSize = 0; }
/// <summary> /// 释放SharedMemory所使用的资源。 /// </summary> /// <param name="disposing">资源释放标志,True: 释放所有资源;False: 仅释放非受控资源。</param> protected virtual void Dispose(bool disposing) { if (m_mappingView != null && !m_mappingView.IsInvalid) { m_mappingView.Dispose(); m_mappingView = null; } if (m_mappingHandle != null && !m_mappingHandle.IsInvalid) { m_mappingHandle.Dispose(); m_mappingHandle = null; } m_mappingOffset = 0; m_viewSize = 0; m_mappingName = null; }