private FileMappingWrapper(SafeFileMappingHandle handle) { Debug.Assert(handle != null); Debug.Assert(!handle.IsInvalid, "handle is invalid"); FileMappingHandle = handle; }
/// <summary> /// /// </summary> /// <param name="fileStream"></param> /// <param name="mapName"></param> /// <param name="capacity"></param> /// <param name="access"></param> /// <param name="memoryMappedFileSecurity"></param> /// <param name="inheritability"></param> /// <param name="leaveOpen"></param> /// <returns></returns> public static FileMappingWrapper CreateFromFile( FileStream fileStream, string mapName, long capacity, FileMappingAccess access, FileMappingSecurity memoryMappedFileSecurity, HandleInheritability inheritability, bool leaveOpen ) { if (fileStream == null || fileStream.SafeFileHandle.IsInvalid || string.IsNullOrEmpty(mapName)) { throw new ArgumentNullException("fileStream or mapname is null"); } SafeFileMappingHandle handle = new SafeFileMappingHandle(Win32.CreateFileMapping( fileStream.SafeFileHandle, IntPtr.Zero, Win32.PAGE_READWRITE, 0, unchecked ((UInt32)capacity), mapName ) ); if (handle.IsInvalid) { throw new Exception("Could not create memory-mapped file."); } // throws // ArgumentException // ArgumentNullException // ArgumentOutOfRangeException // ObjectDisposedException // UnauthorizedAccessException // IOException var wrapper = new FileMappingWrapper(handle); wrapper.fileSize = fileStream.Length; return(wrapper); }
public static extern IntPtr MapViewOfFile(SafeFileMappingHandle hFileMappingObject, UInt32 dwDesiredAccess, UInt32 dwFileOffsetHigh, UInt32 dwFileOffsetLow, IntPtr dwNumberOfBytesToMap);