IntPtr _viewBaseAddr = IntPtr.Zero; // Pointer to the base address of the currently mapped view #endregion Fields #region Constructors /// <summary> /// Constructor used internally by MemoryMappedFile. /// </summary> /// <param name="backingFile">Preconstructed MemoryMappedFile</param> /// <param name="mapSize">Size of the view, in bytes.</param> /// <param name="isWriteable">True if Read/Write access is desired, False otherwise</param> internal MapViewStream(MemoryMappedFile backingFile, long mapSize, bool isWriteable) { if (backingFile == null) { throw new Exception("MapViewStream.MapViewStream - backingFile is null"); } if (!backingFile.IsOpen) { throw new Exception("MapViewStream.MapViewStream - backingFile is not open"); } if ((mapSize < 1) || (mapSize > backingFile.MaxSize)) { throw new Exception(string.Format("MapViewStream.MapViewStream - mapSize is invalid. mapSize == {0}, backingFile.MaxSize == {1}", mapSize, backingFile.MaxSize)); } _backingFile = backingFile; _isWriteable = isWriteable; _access = isWriteable ? MapAccess.FileMapWrite : MapAccess.FileMapRead; // Need a backingFile.SupportsAccess function that takes a MapAccess compares it against its stored MapProtection protection and returns bool _mapSize = mapSize; _isOpen = true; // Map the first view Seek(0, SeekOrigin.Begin); }
private static void ProcessCloseAction() { try { DotNetProcess32FileMap.Close(); DotNetProcess32FileMap = null; DotNetSpyEventWaitHandle.Close(); DotNetSpyEventWaitHandle = null; File.Delete(string.Format(@"{0}{1}", TmpDirName, @"DotNetSpyProxy32.RefDlls.DotNetSpyLib.dll")); } catch { } finally { Application.Exit(); } }
static void Main() { try { DotNetSpyEventWaitHandle = EventWaitHandle.OpenExisting(@"DotNetSpyEventWaitHandle"); DotNetProcess32FileMap = MemoryMappedFile.Open(MapAccess.FileMapAllAccess, @"DotNetProcess32FileMap"); } catch { return; } new Thread(new ThreadStart(ActionThreadProc)).Start(); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new SpyProxy32()); }
/// <summary> /// Open an existing named File Mapping object /// </summary> /// <param name="access">desired access to the map</param> /// <param name="name">name of object</param> /// <returns>The memory mapped file instance</returns> public static MemoryMappedFile Open(MapAccess access, String name) { MemoryMappedFile map = new MemoryMappedFile { _hMap = Win32MapApis.OpenFileMapping((int)access, false, name) }; if (map._hMap == NULL_HANDLE) throw new FileMapIOException(Marshal.GetHRForLastWin32Error()); map._maxSize = -1; // debug unknown return map; }
/// <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(); 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; }