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

            if (disposing)
            {
                GC.SuppressFinalize(this);
            }
        }
Ejemplo n.º 2
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 (!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);
        }