/// <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); }