Пример #1
0
        /// <summary>
        /// Opens a virtual hard disk (VHD) using the V2 of OpenVirtualDisk Win32 API for use, allowing you to explicitly specify OpenVirtualDiskFlags,
        /// Read/Write depth, and Access Mask information.
        /// </summary>
        /// <param name="filePath">The path and name of the Virtual Hard Disk file to open.</param>
        /// <param name="accessMask">Contains the bit mask for specifying access rights to a virtual hard disk (VHD).  Default is All.</param>
        /// <param name="flags">An OpenVirtualDiskFlags object to modify the way the Virtual Hard Disk is opened.  Default is Unknown.</param>
        /// <param name="virtualStorageDeviceType">VHD Format Version (VHD1 or VHD2)</param>
        /// <returns>VirtualHardDisk object</returns>
        public static VirtualHardDisk Open(
            string filePath,
            VIRTUAL_STORAGE_TYPE_DEVICE virtualStorageDeviceType,
            VIRTUAL_DISK_ACCESS_MASK accessMask = VIRTUAL_DISK_ACCESS_MASK.VIRTUAL_DISK_ACCESS_ALL,
            OPEN_VIRTUAL_DISK_FLAG flags        = OPEN_VIRTUAL_DISK_FLAG.OPEN_VIRTUAL_DISK_FLAG_NONE)
        {
            if (filePath == null)
            {
                throw new ArgumentNullException(nameof(filePath));
            }

            if (!File.Exists(filePath))
            {
                throw new FileNotFoundException("The specified VHD was not found.  Please check your path and try again.", filePath);
            }

            var openParams = new NativeMethods.OPEN_VIRTUAL_DISK_PARAMETERS();

            openParams.Version     = virtualStorageDeviceType == VIRTUAL_STORAGE_TYPE_DEVICE.VIRTUAL_STORAGE_TYPE_DEVICE_VHD ? NativeMethods.OPEN_VIRTUAL_DISK_VERSION.OPEN_VIRTUAL_DISK_VERSION_1 : NativeMethods.OPEN_VIRTUAL_DISK_VERSION.OPEN_VIRTUAL_DISK_VERSION_2;
            openParams.GetInfoOnly = false;

            var virtualStorageType = new NativeMethods.VIRTUAL_STORAGE_TYPE();

            virtualStorageType.DeviceId = virtualStorageDeviceType;
            virtualStorageType.VendorId = virtualStorageDeviceType == VIRTUAL_STORAGE_TYPE_DEVICE.VIRTUAL_STORAGE_TYPE_DEVICE_UNKNOWN ? NativeMethods.VirtualStorageTypeVendorUnknown : NativeMethods.VirtualStorageTypeVendorMicrosoft;
            int ret = NativeMethods.OpenVirtualDisk(ref virtualStorageType, filePath, accessMask, flags, ref openParams, out var handle);

            if (NativeMethods.ERROR_SUCCESS != ret)
            {
                throw new Win32Exception(ret);
            }

            return(new VirtualHardDisk(handle, filePath));
        }
Пример #2
0
 public static extern int OpenVirtualDisk(
     ref VIRTUAL_STORAGE_TYPE VirtualStorageType,
     [MarshalAs(UnmanagedType.LPWStr)] string Path,
     VIRTUAL_DISK_ACCESS_MASK VirtualDiskAccessMask,
     OPEN_VIRTUAL_DISK_FLAG Flags,
     ref OPEN_VIRTUAL_DISK_PARAMETERS Parameters,
     out IntPtr Handle);
Пример #3
0
 public static extern UInt32 OpenVirtualDisk(
     ref VIRTUAL_STORAGE_TYPE VirtualStorageType,
     string Path,
     VIRTUAL_DISK_ACCESS_MASK VirtualDiskAccessMask,
     OPEN_VIRTUAL_DISK_FLAG Flags,
     IntPtr Parameters,   // to make it optional (pass IntPtr.Zero)
     ref VirtualDiskSafeHandle Handle);
Пример #4
0
 public static extern UInt32 OpenVirtualDisk(
     ref VIRTUAL_STORAGE_TYPE VirtualStorageType,
     string Path,
     VIRTUAL_DISK_ACCESS_MASK VirtualDiskAccessMask,
     OPEN_VIRTUAL_DISK_FLAG Flags,
     ref OPEN_VIRTUAL_DISK_PARAMETERS Parameters,
     ref VirtualDiskSafeHandle Handle);
Пример #5
0
 public static extern Int32 OpenVirtualDisk(ref VIRTUAL_STORAGE_TYPE VirtualStorageType, String Path, VIRTUAL_DISK_ACCESS_MASK VirtualDiskAccessMask, OPEN_VIRTUAL_DISK_FLAG Flags, ref OPEN_VIRTUAL_DISK_PARAMETERS Parameters, ref VirtualDiskSafeHandle Handle);
Пример #6
0
 public static extern uint OpenVirtualDisk(VIRTUAL_STORAGE_TYPE VirtualStorageType, IntPtr Path, VIRTUAL_DISK_ACCESS_MASK VirtualDiskAccessMask, OPEN_VIRTUAL_DISK_FLAG Flags, OPEN_VIRTUAL_DISK_PARAMETERS_V1 Paramaters, ref IntPtr Handle);