Exemple #1
0
        public static SafeFileHandle CreateSafeFileHandle(
            this IStorageFile windowsRuntimeFile,
            FileAccess access   = FileAccess.ReadWrite,
            FileShare share     = FileShare.Read,
            FileOptions options = FileOptions.None)
        {
            if (windowsRuntimeFile == null)
            {
                throw new ArgumentNullException(nameof(windowsRuntimeFile));
            }

            HANDLE_ACCESS_OPTIONS  accessOptions  = FileAccessToHandleAccessOptions(access);
            HANDLE_SHARING_OPTIONS sharingOptions = FileShareToHandleSharingOptions(share);
            HANDLE_OPTIONS         handleOptions  = FileOptionsToHandleOptions(options);

            IStorageItemHandleAccess handleAccess = windowsRuntimeFile.As <IStorageItemHandleAccess>();

            if (handleAccess == null)
            {
                return(null);
            }

            return(handleAccess.Create(
                       accessOptions,
                       sharingOptions,
                       handleOptions,
                       IntPtr.Zero));
        }
Exemple #2
0
        public static SafeFileHandle GetSafeFileHandle(this IStorageItem Item)
        {
            IntPtr ComInterface = Marshal.GetComInterfaceForObject(Item, typeof(IStorageItemHandleAccess));
            IStorageItemHandleAccess StorageHandleAccess = (IStorageItemHandleAccess)Marshal.GetObjectForIUnknown(ComInterface);

            const uint READ_FLAG       = 0x120089;
            const uint WRITE_FLAG      = 0x120116;
            const uint SHARE_READ_FLAG = 0x1;

            StorageHandleAccess.Create(READ_FLAG | WRITE_FLAG, SHARE_READ_FLAG, 0, IntPtr.Zero, out IntPtr handle);

            return(new SafeFileHandle(handle, true));
        }
Exemple #3
0
        public static SafeFileHandle CreateSafeFileHandle(
            this IStorageFile windowsRuntimeFile,
            FileAccess access   = FileAccess.ReadWrite,
            FileShare share     = FileShare.Read,
            FileOptions options = FileOptions.None)
        {
            if (windowsRuntimeFile == null)
            {
                throw new ArgumentNullException(nameof(windowsRuntimeFile));
            }

            HANDLE_ACCESS_OPTIONS  accessOptions  = FileAccessToHandleAccessOptions(access);
            HANDLE_SHARING_OPTIONS sharingOptions = FileShareToHandleSharingOptions(share);
            HANDLE_OPTIONS         handleOptions  = FileOptionsToHandleOptions(options);

            IStorageItemHandleAccess handleAccess = ((object)windowsRuntimeFile) as IStorageItemHandleAccess;

            if (handleAccess == null)
            {
                return(null);
            }

            SafeFileHandle handle;

            int result = handleAccess.Create(
                accessOptions,
                sharingOptions,
                handleOptions,
                IntPtr.Zero,
                out handle);

            if (result != 0)
            {
                throw Win32Marshal.GetExceptionForWin32Error(Win32Marshal.TryMakeWin32ErrorCodeFromHR(result), windowsRuntimeFile.Name);
            }

            return(handle);
        }