internal MapViewStream(IntPtr baseAddress, long length, MapProtection protection) { m_base = baseAddress; m_length = length; m_protection = protection; m_position = 0; m_isOpen = (baseAddress.ToInt64() > 0); }
/// <summary> /// Constructor used internally by MemoryMappedFile. /// </summary> /// <param name="baseAddress">base address where the view starts</param> /// <param name="length">Length of view, in bytes</param> /// <param name="protection"></param> internal MapViewStream(IntPtr baseAddress, long length, MapProtection protection) { _base = baseAddress; _length = length; _protection = protection; _position = 0; _isOpen = (baseAddress != IntPtr.Zero); }
//! are we open? #endregion // variables /// <summary> /// Constructor used internally by MemoryMappedFile. /// </summary> /// <param name="baseAddress">base address where the view starts</param> /// <param name="length">Length of view, in bytes</param> /// <param name="protection"></param> internal MapViewStream(IntPtr baseAddress, long length, MapProtection protection) { _base = baseAddress; _length = length; _protection = protection; _position = 0; IsOpen = (baseAddress != IntPtr.Zero); }
/// <summary> /// /// </summary> /// <param name="fileName"></param> /// <param name="protection"></param> /// <param name="maxSize"></param> /// <param name="name"></param> public void Create(String fileName, MapProtection protection, long maxSize, String name) { // open file first IntPtr hFile = INVALID_HANDLE_VALUE; try { if (fileName != null) { // determine file access needed // we'll always need generic read access int desiredAccess = GENERIC_READ; if ((protection == MapProtection.PageReadWrite) || (protection == MapProtection.PageWriteCopy)) { desiredAccess |= GENERIC_WRITE; } // open or create the file // if it doesn't exist, it gets created hFile = Win32MapApis.CreateFile( GetMMFDir() + fileName, desiredAccess, 0, IntPtr.Zero, OPEN_ALWAYS, 0, IntPtr.Zero ); if (hFile != NULL_HANDLE) { m_hMap = Win32MapApis.CreateFileMapping( hFile, IntPtr.Zero, (int)protection, (int)((maxSize >> 32) & 0xFFFFFFFF), (int)(maxSize & 0xFFFFFFFF), name ); } else { throw new FileMapIOException(Marshal.GetHRForLastWin32Error()); } if (m_hMap == NULL_HANDLE) { throw new FileMapIOException(Marshal.GetHRForLastWin32Error()); } } } catch (Exception Err) { throw Err; } finally { if (!(hFile == NULL_HANDLE)) { Win32MapApis.CloseHandle(hFile); } } }
/// <summary> /// /// </summary> /// <param name="fileName"></param> /// <param name="protection"></param> /// <param name="access"></param> /// <param name="maxSize"></param> /// <param name="name"></param> public MemoryMappedFile(String fileName, MapProtection protection, MapAccess access, long maxSize, String name) { //try to open the mmf by name. if failed create the file and mmf. IntPtr hFile = IntPtr.Zero; try { m_hMap = Win32MapApis.OpenFileMapping((int)access, false, name); if (m_hMap == NULL_HANDLE) { int desiredAccess = GENERIC_READ; if ((protection == MapProtection.PageReadWrite) || (protection == MapProtection.PageWriteCopy)) { desiredAccess |= GENERIC_WRITE; } hFile = Win32MapApis.CreateFile( GetMMFDir() + fileName, desiredAccess, 0, IntPtr.Zero, OPEN_ALWAYS, 0, IntPtr.Zero ); if (hFile != NULL_HANDLE) { m_hMap = Win32MapApis.CreateFileMapping( hFile, IntPtr.Zero, (int)protection, 0, (int)(maxSize & 0xFFFFFFFF), name ); if (m_hMap != NULL_HANDLE) { m_maxSize = maxSize; } else { throw new FileMapIOException(Marshal.GetHRForLastWin32Error()); } } else { throw new FileMapIOException(Marshal.GetHRForLastWin32Error()); } } } catch (Exception Err) { throw Err; } finally { if (!(hFile == NULL_HANDLE)) { Win32MapApis.CloseHandle(hFile); } } }
/// <summary> /// /// </summary> /// <param name="FileName"></param> /// <param name="protection"></param> /// <param name="name"></param> /// <param name="access"></param> /// <returns></returns> public bool OpenEx(string FileName, MapProtection protection, string name, MapAccess access) { bool RV = false; m_hMap = Win32MapApis.OpenFileMapping((int)access, false, name); if (m_hMap == NULL_HANDLE) { if (System.IO.File.Exists(GetMMFDir() + FileName)) { long maxSize; System.IO.FileStream stream = System.IO.File.Open(GetMMFDir() + FileName, System.IO.FileMode.Open); maxSize = stream.Length; stream.Close(); IntPtr hFile = INVALID_HANDLE_VALUE; OFSTRUCT ipStruct = new OFSTRUCT(); string MMFName = GetMMFDir() + FileName; hFile = Win32MapApis.OpenFile(MMFName, ipStruct , 2); // determine file access needed // we'll always need generic read access int desiredAccess = GENERIC_READ; if ((protection == MapProtection.PageReadWrite) || (protection == MapProtection.PageWriteCopy)) { desiredAccess |= GENERIC_WRITE; } // open or create the file // if it doesn't exist, it gets created m_hMap = Win32MapApis.CreateFileMapping( hFile, IntPtr.Zero, (int)protection, (int)((maxSize >> 32) & 0xFFFFFFFF), (int)(maxSize & 0xFFFFFFFF), name ); // close file handle, we don't need it if (!(hFile == NULL_HANDLE)) { Win32MapApis.CloseHandle(hFile); } RV = true; } } else { RV = true; } return(RV); }
/// <summary> /// Constructor used internally by MemoryMappedFile. /// </summary> /// <param name="baseAddress">base address where the view starts</param> /// <param name="length">Length of view, in bytes</param> /// <param name="protection"></param> internal MapViewStream(IntPtr baseAddress, long length, MapProtection protection) { _base = baseAddress; _length = length; _protection = protection; _position = 0; //UnityEngine.Debug.Log("base address " + baseAddress); //UnityEngine.Debug.Log(baseAddress != IntPtr.Zero); _isOpen = (baseAddress != IntPtr.Zero); // _isOpen = true; //UnityEngine.Debug.Log("_isOpen" + _isOpen); }
Create(string fileName, MapProtection protection, long maxSize, String name) { MemoryMappedFile map = new MemoryMappedFile(); // open file first IntPtr hFile = INVALID_HANDLE_VALUE; if (fileName != null) { // determine file access needed // we'll always need generic read access int desiredAccess = GENERIC_READ; if ((protection == MapProtection.PageReadWrite) || (protection == MapProtection.PageWriteCopy)) { desiredAccess |= GENERIC_WRITE; } // open or create the file // if it doesn't exist, it gets created hFile = Win32MapApis.CreateFile( fileName, desiredAccess, 0, IntPtr.Zero, OPEN_ALWAYS, 0, IntPtr.Zero ); if (hFile == INVALID_HANDLE_VALUE) { throw new FileMapIOException(Marshal.GetHRForLastWin32Error()); } // FIX: Is support neede for zero-length files!?! } map._hMap = Win32MapApis.CreateFileMapping( hFile, IntPtr.Zero, (int)protection, (int)((maxSize >> 32) & 0xFFFFFFFF), (int)(maxSize & 0xFFFFFFFF), name ); // close file handle, we don't need it if (hFile != INVALID_HANDLE_VALUE) { Win32MapApis.CloseHandle(hFile); } if (map._hMap == NULL_HANDLE) { throw new FileMapIOException(Marshal.GetHRForLastWin32Error()); } return(map); }
public static IMemoryMappedFile Create(string fileName, long maxSize, MapProtection mapProtection) { #if NETFX_BUILD // You can either be windows, linux or f****d. if (Environment.OSVersion.Platform == PlatformID.Win32NT) return new Win32MemoryMappedFile(fileName, maxSize, mapProtection); #endif // NETFX_BUILD #if MONO_BUILD // Assume we are running in Unix under Mono, // so we should have Mono.Posix assembly available to us if (Environment.OSVersion.Platform == PlatformID.Unix) return new PosixMemoryMappedFile(fileName, maxSize, mapProtection); #endif // MONO_BUILD throw new PlatformNotSupportedException("Only Unix and Windows platforms are supported at this time"); }
public override IntPtr MapView(MapProtection protection, long offset, long size, IntPtr desiredAddress) { var unixProt = MmapProts.PROT_NONE; if (protection == MapProtection.PageRead) unixProt = MmapProts.PROT_READ; if (protection == MapProtection.PageReadWrite) unixProt = MmapProts.PROT_READ | MmapProts.PROT_WRITE; var flags = MmapFlags.MAP_FILE | MmapFlags.MAP_SHARED; System.Console.WriteLine("Attempting to map fd={0} @ size={1}", _fd, size); var baseAddress = Syscall.mmap(desiredAddress, (ulong) size, unixProt, flags, _fd, offset); if (baseAddress.ToInt64() <= 0) throw new MemoryMapException(String.Format("mmap() failed reason={0}", Mono.Unix.Native.Stdlib.GetLastError())); _mappings.Add(baseAddress, size); return baseAddress; }
Create(MapProtection protection, long maxSize, string name) { return Create(null, protection, maxSize, name); }
Create(string fileName, MapProtection protection, long maxSize) { return(Create(fileName, protection, maxSize, null)); }
Create(string fileName, MapProtection protection) { return(Create(fileName, protection, -1, null)); }
Create(MapProtection protection, long maxSize, string name) { // Debug.Log("Create mem file " + name + " " + maxSize); return(Create(null, protection, maxSize, name)); }
/// <summary> /// Constructor used internally by MemoryMappedFile. /// </summary> /// <param name="backingFile">Preconstructed MemoryMappedFile</param> /// <param name="mapStartIdx">Index in the backingFile at which the view starts</param> /// <param name="mapSize">Size of the view, in bytes.</param> /// <param name="isWriteable">True if Read/Write access is desired, False otherwise</param> /// <param name="viewSize">The desired view size</param> public MemoryMappedStream(IMemoryMappedFile backingFile, long mapStartIdx, long mapSize, bool isWriteable, long viewSize) { if (backingFile == null) { throw new ArgumentException("backingFile is null"); } if ((mapStartIdx < 0) || (mapStartIdx > (long)backingFile.MaxSize)) throw new ArgumentException( String.Format("mapStartIdx is invalid. mapStartIdx=={0}, backingFile.MaxSize=={1}", mapStartIdx, backingFile.MaxSize)); if ((mapSize < 1) || (((mapStartIdx) + mapSize) > (long)backingFile.MaxSize)) throw new ArgumentException( String.Format("mapSize is invalid. mapStartIdx=={0}, mapSize=={1}, backingFile.MaxSize=={2}", mapStartIdx, mapSize, backingFile.MaxSize)); if ((viewSize < MIN_VIEW_SIZE) || (viewSize > MAX_VIEW_SIZE)) throw new ArgumentException( String.Format("viewSize is invalid. viewSize=={0}", viewSize)); _backingFile = backingFile; _isWriteable = isWriteable; _access = isWriteable ? MapProtection.PageReadWrite : MapProtection.PageRead; _desiredViewSize = viewSize; _mapStartIdx = mapStartIdx; _mapSize = (long)mapSize; _isOpen = true; // Map the first view Seek(0, SeekOrigin.Begin); }
Create(string fileName, MapProtection protection, long maxSize) { return Create(fileName, protection, maxSize, null); }
/// <summary> /// Create an named map object with no file backing /// </summary> /// <param name="protection">desired access to the /// mapping object</param> /// <param name="maxSize">maximum size of filemap object</param> /// <returns>The memory mapped file instance</returns> public static MemoryMappedFile Create(MapProtection protection, long maxSize) { return Create ( null, protection, maxSize, null ); }
Create(string fileName, MapProtection protection, long maxSize, String name) { MemoryMappedFile map = new MemoryMappedFile(); if (!map.Is64bit && maxSize > uint.MaxValue) { throw new ConstraintException("32bit systems support max size of 4gb."); } // open file first IntPtr hFile = INVALID_HANDLE_VALUE; if (!string.IsNullOrEmpty(fileName)) { if (!File.Exists(fileName)) { throw new MMException(string.Format("MemoryMappedFile.Create - \"{0}\" does not exist ==> Unable to map file", fileName)); } FileInfo backingFileInfo = new FileInfo(fileName); // Get the file size and store it for later use. map._fileSize = backingFileInfo.Length; // Set max size if not set. Otherwise cap it. if (maxSize == 0) { maxSize = backingFileInfo.Length; } else { maxSize = Math.Min(maxSize, backingFileInfo.Length); } if (maxSize == 0) { throw new MMException(string.Format("Create - \"{0}\" is zero bytes ==> Unable to map file", fileName)); } int desiredAccess = GENERIC_READ; int desiredShareMode = FILE_SHARE_READ; if ((protection == MapProtection.PageReadWrite) || (protection == MapProtection.PageWriteCopy)) { desiredAccess |= GENERIC_WRITE; desiredShareMode |= FILE_SHARE_WRITE; } hFile = Win32MapApis.CreateFile( fileName, desiredAccess, desiredShareMode, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero ); if (hFile == INVALID_HANDLE_VALUE) { throw new MMException(Marshal.GetLastWin32Error()); } map._fileName = fileName; } // Map the file. map._hMap = Win32MapApis.CreateFileMapping( hFile, IntPtr.Zero, (int)protection, (int)((maxSize >> 32) & 0xFFFFFFFF), (int)(maxSize & 0xFFFFFFFF), name ); if (hFile != INVALID_HANDLE_VALUE) { Win32MapApis.CloseHandle(hFile); } if (map._hMap == NULL_HANDLE) { throw new MMException(Marshal.GetLastWin32Error()); } map._protection = protection; map._maxSize = maxSize; return(map); }
public Win32MemoryMappedFile(string fileName, long maxSize, MapProtection mapProtection) : base(fileName, maxSize) { // open file first var fileHandle = Win32Native.INVALID_HANDLE_VALUE; FileInfo backingFileInfo = null; if (!String.IsNullOrEmpty(fileName)) backingFileInfo = new FileInfo(fileName); // Anonymous file mapping? _isAnonymous = fileName == null; if (String.Empty == fileName) throw new ArgumentException("Filename must be specified"); if (maxSize == 0) { if (_isAnonymous || !File.Exists(fileName)) throw new ArgumentException( String.Format("Win32MemoryMappedFile.Create - \"{0}\" does not exist ==> Unable to map entire file", fileName)); maxSize = backingFileInfo.Length; if (maxSize == 0) throw new ArgumentException( string.Format("Win32MemoryMappedFile.Create - \"{0}\" is zero bytes ==> Unable to map entire file", fileName)); } _maxSize = maxSize; // determine file access needed // we'll always need generic read access var desiredAccess = Win32FileAccess.None; switch (mapProtection) { case MapProtection.PageRead: desiredAccess = Win32FileAccess.GenericRead; break; case MapProtection.PageReadWrite: desiredAccess = Win32FileAccess.GenericRead | Win32FileAccess.GenericWrite; break; } //string mapName; // open or create the file // if it doesn't exist, it gets created if (!_isAnonymous) { fileHandle = Win32Native.CreateFile( fileName, (uint)desiredAccess, (uint)(Wind32FileShare.Write | Wind32FileShare.Read), IntPtr.Zero, (uint)Win32CreationDisposition.OpenAlways, 0, IntPtr.Zero); if (fileHandle == Win32Native.INVALID_HANDLE_VALUE) throw new Win32Exception(Marshal.GetLastWin32Error()); //mapName = backingFileInfo.Name; } else ; //mapName = "Anonymous"; // We always create a read-write mapping object // the individual map obtained through MapView will be able to restrict // the access var win32MapProtection = Win32MapProtection.PageReadOnly; switch (mapProtection) { case MapProtection.PageRead: win32MapProtection = Win32MapProtection.PageReadOnly; break; case MapProtection.PageReadWrite: win32MapProtection = Win32MapProtection.PageReadWrite; break; case MapProtection.PageWriteCopy: win32MapProtection = Win32MapProtection.PageWriteCopy; break; } _mapHandle = Win32Native.CreateFileMapping( fileHandle, IntPtr.Zero, (int) win32MapProtection, (int) ((maxSize >> 32) & 0xFFFFFFFF), (int) (maxSize & 0xFFFFFFFF), null); // close file handle, we don't need it if (fileHandle != Win32Native.INVALID_HANDLE_VALUE) Win32Native.CloseHandle(fileHandle); if (_mapHandle == Win32Native.NULL_HANDLE) throw new Win32Exception(Marshal.GetLastWin32Error()); }
public override IntPtr MapView(MapProtection protection, long offset, long size, IntPtr desiredAddress) { if (!IsOpen) throw new ObjectDisposedException("Win32MemoryMappedFile already closed!"); var mapSize = new IntPtr(size); var win32Access = Win32FileMapAccessType.Unspecified; switch (protection) { case MapProtection.PageRead: win32Access = Win32FileMapAccessType.Read; break; case MapProtection.PageReadWrite: win32Access = Win32FileMapAccessType.Write; break; case MapProtection.PageWriteCopy: win32Access = Win32FileMapAccessType.Copy; break; } var baseAddress = Win32Native.MapViewOfFileEx( _mapHandle, win32Access, (uint)((offset >> 32) & 0xFFFFFFFF), (uint)(offset & 0xFFFFFFFF), new UIntPtr((ulong) mapSize), desiredAddress); if (baseAddress == IntPtr.Zero) throw new Win32Exception(Marshal.GetLastWin32Error()); _mappings.Add((ulong) baseAddress, size); return baseAddress; }
public abstract IntPtr MapView(MapProtection protection, long offset, long size, IntPtr desiredAddress);
Create(MapProtection protection, long maxSize) { return Create(null, protection, maxSize, null); }
Create(string fileName, MapProtection protection) { return Create(fileName, protection, 0, null); }
/// <summary> /// Create an unnamed map object /// </summary> /// <param name="fileName">name of backing file</param> /// <param name="protection">desired access to the /// mapping object</param> /// <param name="maxSize">maximum size of filemap /// object, or -1 for size of file</param> /// <returns>The memory mapped file instance</returns> public static MemoryMappedFile Create( string fileName, MapProtection protection, long maxSize) { return Create ( fileName, protection, maxSize, null ); }
Create(string fileName, MapProtection protection, long maxSize, String name) { MemoryMappedFile map = new MemoryMappedFile(); if (!map.Is64bit && maxSize > uint.MaxValue) throw new ConstraintException("32bit systems support max size of 4gb."); // open file first IntPtr hFile = INVALID_HANDLE_VALUE; if (!string.IsNullOrEmpty(fileName)) { if (maxSize == 0) { if (!File.Exists(fileName)) { throw new Exception(string.Format("Winterdom.IO.FileMap.MemoryMappedFile.Create - \"{0}\" does not exist ==> Unable to map entire file", fileName)); } FileInfo backingFileInfo = new FileInfo(fileName); maxSize = backingFileInfo.Length; if (maxSize == 0) { throw new Exception(string.Format("Winterdom.IO.FileMap.MemoryMappedFile.Create - \"{0}\" is zero bytes ==> Unable to map entire file", fileName)); } } // determine file access needed // we'll always need generic read access int desiredAccess = GENERIC_READ; if ((protection == MapProtection.PageReadWrite) || (protection == MapProtection.PageWriteCopy)) { desiredAccess |= GENERIC_WRITE; } // open or create the file // if it doesn't exist, it gets created hFile = Win32MapApis.CreateFile( fileName, desiredAccess, 0, IntPtr.Zero, OPEN_ALWAYS, 0, IntPtr.Zero ); if (hFile == INVALID_HANDLE_VALUE) throw new FileMapIOException(Marshal.GetHRForLastWin32Error()); //SafeFileHandle handle = new SafeFileHandle(hFile,true); //NTFS.Sparse.SparseFile.MarkSparse(handle); map._fileName = fileName; } map._hMap = Win32MapApis.CreateFileMapping( hFile, IntPtr.Zero, (int)protection, (int)((maxSize >> 32) & 0xFFFFFFFF), (int)(maxSize & 0xFFFFFFFF), name ); // close file handle, we don't need it if (hFile != INVALID_HANDLE_VALUE) Win32MapApis.CloseHandle(hFile); if (map._hMap == NULL_HANDLE) throw new FileMapIOException(Marshal.GetHRForLastWin32Error()); map._protection = protection; map._maxSize = maxSize; return map; }
Create(MapProtection protection, long maxSize) { return(Create(null, protection, maxSize, null)); }
Create(string fileName, MapProtection protection, long maxSize, String name) { MemoryMappedFile map = new MemoryMappedFile(); if (!map.Is64bit && maxSize > uint.MaxValue) { throw new ConstraintException("32bit systems support max size of 4gb."); } // open file first IntPtr hFile = INVALID_HANDLE_VALUE; if (!string.IsNullOrEmpty(fileName)) { if (maxSize == 0) { if (!File.Exists(fileName)) { throw new Exception(string.Format("Winterdom.IO.FileMap.MemoryMappedFile.Create - \"{0}\" does not exist ==> Unable to map entire file", fileName)); } FileInfo backingFileInfo = new FileInfo(fileName); maxSize = backingFileInfo.Length; if (maxSize == 0) { throw new Exception(string.Format("Winterdom.IO.FileMap.MemoryMappedFile.Create - \"{0}\" is zero bytes ==> Unable to map entire file", fileName)); } } // determine file access needed // we'll always need generic read access int desiredAccess = GENERIC_READ; if ((protection == MapProtection.PageReadWrite) || (protection == MapProtection.PageWriteCopy)) { desiredAccess |= GENERIC_WRITE; } // open or create the file // if it doesn't exist, it gets created hFile = Win32MapApis.CreateFile( fileName, desiredAccess, 0, IntPtr.Zero, OPEN_ALWAYS, 0, IntPtr.Zero ); if (hFile == INVALID_HANDLE_VALUE) { throw new FileMapIOException(Marshal.GetHRForLastWin32Error()); } //SafeFileHandle handle = new SafeFileHandle(hFile,true); //NTFS.Sparse.SparseFile.MarkSparse(handle); map._fileName = fileName; } map._hMap = Win32MapApis.CreateFileMapping( hFile, IntPtr.Zero, (int)protection, (int)((maxSize >> 32) & 0xFFFFFFFF), (int)(maxSize & 0xFFFFFFFF), name ); // close file handle, we don't need it if (hFile != INVALID_HANDLE_VALUE) { Win32MapApis.CloseHandle(hFile); } if (map._hMap == NULL_HANDLE) { throw new FileMapIOException(Marshal.GetHRForLastWin32Error()); } map._protection = protection; map._maxSize = maxSize; return(map); }
public static extern SafeFileHandle CreateFileMapping(SafeFileHandle hFile, IntPtr lpAttributes, MapProtection flProtect, int dwMaximumSizeHigh, int dwMaximumSizeLow, string lpName);
/// <summary> /// Create an unnamed map object with a maximum size /// equal to that of the file /// </summary> /// <param name="fileName">name of backing file</param> /// <param name="protection">desired access to the /// mapping object</param> /// <returns>The memory mapped file instance</returns> public static MemoryMappedFile Create(string fileName, MapProtection protection) { return Create ( fileName, protection, -1, null ); }
Create(MapProtection protection, long maxSize, string name) { return(Create(null, protection, maxSize, name)); }
/// <summary> /// Create a named map object /// </summary> /// <param name="fileName">name of backing file, or null /// for a pagefile-backed map</param> /// <param name="protection">desired access to the mapping /// object</param> /// <param name="maxSize">maximum size of filemap object, or 0 /// for size of file</param> /// <param name="name">name of file mapping object</param> /// <returns>The memory mapped file instance</returns> public static MemoryMappedFile Create( string fileName, MapProtection protection, long maxSize, String name) { MemoryMappedFile map = new MemoryMappedFile(); // open file first IntPtr hFile = INVALID_HANDLE_VALUE; if ( fileName != null ) { // determine file access needed // we'll always need generic read access int desiredAccess = GENERIC_READ; if ( (protection == MapProtection.PageReadWrite) || (protection == MapProtection.PageWriteCopy) ) { desiredAccess |= GENERIC_WRITE; } // open or create the file // if it doesn't exist, it gets created hFile = Win32MapApis.CreateFile ( fileName, desiredAccess, 0, IntPtr.Zero, OPEN_ALWAYS, 0, IntPtr.Zero ); if ( hFile == INVALID_HANDLE_VALUE ) throw new FileMapIOException ( Marshal.GetHRForLastWin32Error() ); // FIX: Is support neede for zero-length files!?! } map._hMap = Win32MapApis.CreateFileMapping ( hFile, IntPtr.Zero, (int)protection, (int)((maxSize >> 32) & 0xFFFFFFFF), (int)(maxSize & 0xFFFFFFFF), name ); // close file handle, we don't need it if ( hFile != INVALID_HANDLE_VALUE ) Win32MapApis.CloseHandle(hFile); if ( map._hMap == NULL_HANDLE ) throw new FileMapIOException ( Marshal.GetHRForLastWin32Error() ); return map; }
Create(string fileName, MapProtection protection, long maxSize, String name) { MemoryMappedFile map = new MemoryMappedFile(); if (!map.Is64bit && maxSize > uint.MaxValue) { throw new ConstraintException("32bit systems support max size of 4gb."); } // open file first IntPtr hFile = INVALID_HANDLE_VALUE; if (!string.IsNullOrEmpty(fileName)) { if (maxSize == 0) { if (!File.Exists(fileName)) { throw new MMException(string.Format("MemoryMappedFile.Create - \"{0}\" does not exist ==> Unable to map entire file", fileName)); } FileInfo backingFileInfo = new FileInfo(fileName); maxSize = backingFileInfo.Length; if (maxSize == 0) { throw new MMException(string.Format("Create - \"{0}\" is zero bytes ==> Unable to map entire file", fileName)); } } int desiredAccess = GENERIC_READ; if ((protection == MapProtection.PageReadWrite) || (protection == MapProtection.PageWriteCopy)) { desiredAccess |= GENERIC_WRITE; } hFile = Win32MapApis.CreateFile( fileName, desiredAccess, 0, IntPtr.Zero, OPEN_ALWAYS, 0, IntPtr.Zero ); if (hFile == INVALID_HANDLE_VALUE) { throw new MMException(Marshal.GetHRForLastWin32Error()); } map._fileName = fileName; } map._hMap = Win32MapApis.CreateFileMapping( hFile, IntPtr.Zero, (int)protection, (int)((maxSize >> 32) & 0xFFFFFFFF), (int)(maxSize & 0xFFFFFFFF), name ); if (hFile != INVALID_HANDLE_VALUE) { Win32MapApis.CloseHandle(hFile); } if (map._hMap == NULL_HANDLE) { throw new MMException(Marshal.GetHRForLastWin32Error()); } map._protection = protection; map._maxSize = maxSize; return(map); }