Example #1
0
        protected virtual void Dispose(bool disposing)
        {
            if (IsOpen)
            {
                Win32MapApis.CloseHandle(_hMap);
            }
            _hMap = NULL_HANDLE;

            if (disposing)
            {
                GC.SuppressFinalize(this);
            }
        }
Example #2
0
        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 MMException(Marshal.GetHRForLastWin32Error());
            }
            map._maxSize = -1;
            return(map);
        }
Example #3
0
        public IntPtr MapView(MapAccess access, long offset, long size)
        {
            if (!IsOpen)
            {
                throw new ObjectDisposedException("Memmoryfile Already closed");
            }


            IntPtr mapSize = new IntPtr(size);

            IntPtr baseAddress = Win32MapApis.MapViewOfFile(
                _hMap, (int)access,
                (int)((offset >> 32) & 0xFFFFFFFF),
                (int)(offset & 0xFFFFFFFF), mapSize
                );

            if (baseAddress == IntPtr.Zero)
            {
                throw new MMException(Marshal.GetHRForLastWin32Error());
            }

            return(baseAddress);
        }
Example #4
0
        public void Flush(IntPtr viewBaseAddr)
        {
            IntPtr flushLength = new IntPtr(MaxSize);

            Win32MapApis.FlushViewOfFile(viewBaseAddr, flushLength);
        }
Example #5
0
 public void UnMapView(IntPtr mapBaseAddr)
 {
     Win32MapApis.UnmapViewOfFile(mapBaseAddr);
 }
Example #6
0
        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);
        }