Esempio n. 1
0
        internal static MemoryMappedView CreateView(SafeMemoryMappedFileHandle safeMemoryMappedFileHandle, MemoryMappedFileAccess access, long offset, long size)
        {
            // http://msdn.microsoft.com/en-us/library/windows/desktop/aa366548(v=vs.85).aspx
            UnsafeNative.SYSTEM_INFO info = new UnsafeNative.SYSTEM_INFO();
            UnsafeNative.GetSystemInfo(ref info);

            // To calculate where to start the file mapping, round down the
            // offset of the data into the memory-mapped file to the nearest multiple of the
            // system allocation granularity.
            long fileMapStart = (offset / info.dwAllocationGranularity) * info.dwAllocationGranularity;
            // How large will the file mapping object be?
            long mapViewSize = (offset % info.dwAllocationGranularity) + size;
            // The data of interest is not necessarily at the beginning of the
            // view, so determine how far into the view to set the pointer.
            long viewDelta = offset - fileMapStart;

            SafeMemoryMappedViewHandle safeHandle = UnsafeNative.MapViewOfFile(safeMemoryMappedFileHandle, access.ToMapViewFileAccess(), (ulong)fileMapStart, new UIntPtr((ulong)mapViewSize));
            var lastWin32Error = Marshal.GetLastWin32Error();

            if (safeHandle.IsInvalid)
            {
                if (lastWin32Error == UnsafeNative.ERROR_FILE_NOT_FOUND)
                {
                    throw new FileNotFoundException();
                }
                throw new System.IO.IOException(UnsafeNative.GetMessage(lastWin32Error));
            }

            return(new MemoryMappedView(safeHandle, viewDelta, size));
        }
 public static MemoryMappedFile OpenExisting(string mapName)
 {
     SafeMemoryMappedFileHandle safeMemoryMappedFileHandle = UnsafeNative.OpenFileMapping((uint)MemoryMappedFileRights.ReadWrite, false, mapName);
     int lastWin32Error = Marshal.GetLastWin32Error();
     if (safeMemoryMappedFileHandle.IsInvalid)
     {
         if (lastWin32Error == UnsafeNative.ERROR_FILE_NOT_FOUND)
             throw new FileNotFoundException();
         throw new System.IO.IOException(UnsafeNative.GetMessage(lastWin32Error));
     }
     return new MemoryMappedFile(safeMemoryMappedFileHandle);
 }
Esempio n. 3
0
        private static SafeMemoryMappedFileHandle DoCreate(string path, string mapName, long capacity, out bool exists, bool readCapacity = false)
        {
            exists = File.Exists(path);
            FileStream file = File.Open(path, FileMode.OpenOrCreate);

            if (exists && file.Length != capacity)
            {
                if (!readCapacity)
                {
                    file.Close();
                    File.Copy(path, path + "_old", true);
                    File.Delete(path);
                    file = File.Open(path, FileMode.OpenOrCreate);
                }
                else
                {
                    byte[] byt = new byte[8];
                    file.Read(byt, 0, 8);
                    capacity = BitConverter.ToInt32(byt, 0);
                }
            }
            SafeFileHandle             fileHandle = file.SafeFileHandle;
            SafeMemoryMappedFileHandle safeHandle = UnsafeNative.CreateFileMapping(fileHandle,
                                                                                   (UnsafeNative.FileMapProtection)MemoryMappedFileAccess.ReadWrite,
                                                                                   capacity, mapName);

            var lastWin32Error = Marshal.GetLastWin32Error();

            if (!safeHandle.IsInvalid && (lastWin32Error == UnsafeNative.ERROR_ALREADY_EXISTS))
            {
                throw new System.IO.IOException(UnsafeNative.GetMessage(lastWin32Error));
            }
            else if (safeHandle.IsInvalid && lastWin32Error > 0)
            {
                throw new System.IO.IOException(UnsafeNative.GetMessage(lastWin32Error));
            }

            if (safeHandle == null || safeHandle.IsInvalid)
            {
                throw new InvalidOperationException("Cannot create file mapping");
            }

            return(safeHandle);
        }