示例#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));
        }
示例#2
0
        internal static string GetMessage(int errorCode)
        {
            StringBuilder stringBuilder = new StringBuilder(512);

            if (UnsafeNative.FormatMessage(12800, IntPtr.Zero, errorCode, 0, stringBuilder, stringBuilder.Capacity, IntPtr.Zero) != 0)
            {
                return(stringBuilder.ToString());
            }
            return(string.Concat("UnknownError_Num ", errorCode));
        }
 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);
 }
示例#4
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);
        }