Esempio n. 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));
        }
Esempio n. 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);
Esempio n. 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);
Esempio n. 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);
Esempio n. 5
0
 public static extern int CreateVirtualDisk(
     ref VIRTUAL_STORAGE_TYPE VirtualStorageType,
     [MarshalAs(UnmanagedType.LPWStr)] string Path,
     VIRTUAL_DISK_ACCESS_MASK VirtualDiskAccessMask,
     ref SECURITY_DESCRIPTOR SecurityDescriptor,
     CREATE_VIRTUAL_DISK_FLAG Flags,
     int ProviderSpecificFlags,
     ref CREATE_VIRTUAL_DISK_PARAMETERS_VERSION2 Parameters,
     IntPtr Overlapped,
     out IntPtr Handle);
Esempio n. 6
0
        public static IntPtr OpenVHD(FileInfo file, VHDOpeMode openMode)
        {
            IntPtr handle = IntPtr.Zero;

            //Storage type initialization
            VIRTUAL_STORAGE_TYPE storageType = new VIRTUAL_STORAGE_TYPE();

            storageType.DeviceId = Win32.VIRTUAL_STORAGE_TYPE_DEVICE_VHD;
            storageType.VendorId = Win32.VIRTUAL_STORAGE_TYPE_VENDOR_MICROSOFT;

            //Open disk parameters
            OPEN_VIRTUAL_DISK_PARAMETERS openVdisk = new OPEN_VIRTUAL_DISK_PARAMETERS();

            openVdisk.Version          = OPEN_VIRTUAL_DISK_VERSION.OPEN_VIRTUAL_DISK_VERSION_1;
            openVdisk.Version1.RWDepth = Win32.OPEN_VIRTUAL_DISK_RW_DEPTH_DEFAULT;

            VIRTUAL_DISK_ACCESS_MASK accessMask = VIRTUAL_DISK_ACCESS_MASK.VIRTUAL_DISK_ACCESS_ALL;

            if (openMode == VHDOpeMode.Attach)
            {
                accessMask = VIRTUAL_DISK_ACCESS_MASK.VIRTUAL_DISK_ACCESS_ATTACH_RW;
            }
            else if (openMode == VHDOpeMode.Detach)
            {
                accessMask = VIRTUAL_DISK_ACCESS_MASK.VIRTUAL_DISK_ACCESS_DETACH;
            }

            //Open the virtual disk for read write using API
            int nRet = Win32.OpenVirtualDisk(ref storageType,
                                             file.FullName,
                                             accessMask,
                                             OPEN_VIRTUAL_DISK_FLAG.OPEN_VIRTUAL_DISK_FLAG_NONE,
                                             ref openVdisk,
                                             ref handle);

            if (nRet != Win32.ERROR_SUCCESS)
            {
                throw new Win32Exception(nRet);
            }



            return(handle);
        }
Esempio n. 7
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);
Esempio n. 8
0
 public static extern Int32 CreateVirtualDisk(ref VIRTUAL_STORAGE_TYPE VirtualStorageType, String Path, VIRTUAL_DISK_ACCESS_MASK VirtualDiskAccessMask, IntPtr SecurityDescriptor, CREATE_VIRTUAL_DISK_FLAG Flags, Int32 ProviderSpecificFlags, ref CREATE_VIRTUAL_DISK_PARAMETERS Parameters, ref NativeOverlapped Overlapped, ref VirtualDiskSafeHandle Handle);
Esempio n. 9
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);
Esempio n. 10
0
 public static extern uint CreateVirtualDisk(VIRTUAL_STORAGE_TYPE VirtualStorageType, [MarshalAs(UnmanagedType.LPWStr)] string Path, VIRTUAL_DISK_ACCESS_MASK VirtualDiskAccessMask, IntPtr SecurityDescriptor, CREATE_VIRTUAL_DISK_FLAGS Flags, uint ProviderSpecificFlags, CREATE_VIRTUAL_DISK_PARAMETERS_V2 Parameters, IntPtr Overlapped, ref IntPtr Handle);