Esempio n. 1
0
        private void OpenMap(string filename)
        {
            IntPtr hmap;
            int    length;

            using (FileStream stream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read)){
                if (stream.Length > Int32.MaxValue)
                {
                    throw new FileLoadException(ExceptionStrings.FileTooBig, filename);
                }
                length = unchecked ((int)stream.Length);
#if WHIDBEY && !OldWhidbey
                hmap = CreateFileMapping(stream.SafeFileHandle.DangerousGetHandle(), IntPtr.Zero, PageAccess.PAGE_READONLY, 0, length, null);
#else
                hmap = CreateFileMapping(stream.Handle, IntPtr.Zero, PageAccess.PAGE_READONLY, 0, length, null);
#endif
                if (hmap == IntPtr.Zero)
                {
                    int rc = Marshal.GetLastWin32Error();
                    throw new FileLoadException(String.Format(CultureInfo.CurrentCulture,
                                                              ExceptionStrings.CreateFileMappingReturnedErrorCode, rc.ToString()), filename);
                }
            }
            this.buffer = (byte *)MapViewOfFile(hmap, FileMapAccess.FILE_MAP_READ, 0, 0, (IntPtr)length);
            MemoryMappedFile.CloseHandle(hmap);
            if (this.buffer == null)
            {
                int rc = Marshal.GetLastWin32Error();
                throw new FileLoadException(String.Format(CultureInfo.CurrentCulture,
                                                          ExceptionStrings.MapViewOfFileReturnedErrorCode, rc.ToString()), filename);
            }
            this.length = length;
        }