public override void Flush() { if (!IsOpen) { throw new FileMapIOException("Stream is closed!"); } Win32MapApis.FlushViewOfFile(m_base, (int)m_length); }
/// <summary> /// /// </summary> public void Close() { if (m_hMap != NULL_HANDLE) { Win32MapApis.CloseHandle(m_hMap); } m_hMap = NULL_HANDLE; }
public override void Close() { if (IsOpen) { Flush(); Win32MapApis.UnmapViewOfFile(m_base); IsOpen = false; } }
/// <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> /// /// </summary> /// <param name="access"></param> /// <param name="name"></param> public bool Open(MapAccess access, String name) { bool RV = true; try { m_hMap = Win32MapApis.OpenFileMapping((int)access, false, name); if (m_hMap == NULL_HANDLE) { RV = false; } return(RV); } catch { return(RV); } }
/// <summary> /// /// </summary> /// <param name="access"></param> /// <param name="offset"></param> /// <param name="size"></param> /// <param name="path"></param> /// <returns></returns> public MapViewStream MapView(MapAccess access, long offset, int size, string path) { IntPtr baseAddress = IntPtr.Zero; try { baseAddress = Win32MapApis.MapViewOfFile( m_hMap, (int)access, (int)((offset >> 32) & 0xFFFFFFFF), (int)(offset & 0xFFFFFFFF), size ); if (baseAddress != IntPtr.Zero) { MapProtection protection; if (access == MapAccess.FileMapRead) { protection = MapProtection.PageReadOnly; } else { protection = MapProtection.PageReadWrite; } if (path != "") { System.IO.FileInfo oFi = new System.IO.FileInfo(GetMMFDir() + path); m_maxSize = (int)oFi.Length; } return(new MapViewStream(baseAddress, m_maxSize, protection)); } return(null); } catch { throw new FileMapIOException(Marshal.GetHRForLastWin32Error()); } }
/// <summary> /// this function will /// 1) open exsisting (if not create) MMF that hold all the MMf names for each object /// 2) look if aname allready exist /// 3) if exist /// -Delete the MMF /// -create new MMF /// -Enter the onject into /// if not /// -create new MMF /// -Enter the onject into /// -enter the new name and MMF name into MMF of object and MMF name /// </summary> /// <param name="objName"></param> /// <param name="inObject"></param> public void AddObject(string objName, object inObject, bool UpdateDomain) { MemoryMappedFile map = new MemoryMappedFile(); System.Collections.Hashtable oFilesMap; System.IntPtr oAtom = System.IntPtr.Zero; string strIps = ""; try { if (!map.OpenEx(ObjectNamesMMF + ".nat", MapProtection.PageReadWrite, ObjectNamesMMF, MapAccess.FileMapAllAccess)) { //Create MMF for the object and serialize it WriteObjectToMMF(inObject, objName, 0); //create hashtable oFilesMap = new System.Collections.Hashtable(); //add object name and mmf name to hash oFilesMap.Add(objName, objName); //create main MMF WriteObjectToMMF(oFilesMap, ObjectNamesMMF, 0); } else { BinaryFormatter bf = new BinaryFormatter(); Stream mmfStream = map.MapView(MapAccess.FileMapRead, 0, 0, ""); mmfStream.Position = 0; oFilesMap = bf.Deserialize(mmfStream) as Hashtable; long StartPosition = mmfStream.Position; if (oFilesMap.ContainsKey(objName)) { //name exist so we need to // open the MMF of the existing and update it MemoryMappedFile MemberMap = new MemoryMappedFile(); oMutex.WaitOne(); MemberMap.OpenEx(objName + ".nat", MapProtection.PageReadWrite, objName, MapAccess.FileMapAllAccess); //(MapAccess.FileMapAllAccess ,objName); MapViewStream stream = MemberMap.MapView(MapAccess.FileMapAllAccess, 0, (int)0, ""); bf = new BinaryFormatter(); MemoryStream ms = new MemoryStream(); bf.Serialize(ms, inObject); stream.Position = 0; stream.Write(ms.GetBuffer(), 0, (int)ms.Length); stream.Flush(); stream.Close(); oMutex.ReleaseMutex(); } else { //name not apear so we nedd to // craete new MMF file and serialize WriteObjectToMMF(inObject, objName, 0); oMutex.WaitOne(); MapViewStream stream = map.MapView(MapAccess.FileMapAllAccess, 0, (int)0, ""); // update the main HashTable oFilesMap.Add(objName, objName); // serialize new Hash bf = new BinaryFormatter(); MemoryStream ms = new MemoryStream(); bf.Serialize(ms, oFilesMap); stream.Position = 0; stream.Write(ms.GetBuffer(), 0, (int)ms.Length); stream.Flush(); stream.Close(); oMutex.ReleaseMutex(); } } } catch (Exception e) { throw new Exception("Cannot Open File " + objName, e); } finally { Win32MapApis.GlobalDeleteAtom(oAtom); } }