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

            if (disposing)
            {
                GC.SuppressFinalize(this);
            }
        }
Esempio n. 2
0
        Create(string fileName, MapProtection protection,
               long maxSize, String name)
        {
            MemoryMappedFile map = new MemoryMappedFile();

            // open file first
            IntPtr hFile = INVALID_HANDLE_VALUE;

            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(
                    fileName, desiredAccess, 0,
                    IntPtr.Zero, OPEN_ALWAYS, 0, IntPtr.Zero
                    );
                if (hFile == INVALID_HANDLE_VALUE)
                {
                    throw new FileMapIOException(Marshal.GetHRForLastWin32Error());
                }

                // FIX: Is support neede for zero-length files!?!
            }

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

            return(map);
        }