using System; using System.Runtime.InteropServices; using System.Text; using System.Threading; using Vanara.Extensions; using Vanara.InteropServices; using static Vanara.PInvoke.Kernel32; namespace Vanara.PInvoke { /// Platform invokable enumerated types, constants and functions from VirtDisk.h public static partial class VirtDisk { /// The virtual storage type vendor Microsoft public static readonly Guid VIRTUAL_STORAGE_TYPE_VENDOR_MICROSOFT = new(0xec984aec, 0xa0f9, 0x47e9, 0x90, 0x1f, 0x71, 0x41, 0x5a, 0x66, 0x34, 0x5b); /// The virtual storage type vendor unknown. public static readonly Guid VIRTUAL_STORAGE_TYPE_VENDOR_UNKNOWN = new(0x00000000, 0x0000, 0x0000, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00); /// Contains flags affecting the behavior of the ApplySnapshotVhdSet function. [PInvokeData("VirtDisk.h", MSDNShortId = "mt638035")] [Flags] public enum APPLY_SNAPSHOT_VHDSET_FLAG { /// No flag specified. APPLY_SNAPSHOT_VHDSET_FLAG_NONE = 0x00000000, /// Indicates that the snapshot to be applied was created as a writeable snapshot type. APPLY_SNAPSHOT_VHDSET_FLAG_WRITEABLE = 0x00000001 } /// Enumerates the possible versions for parameters for the ApplySnapshotVhdSet function. [PInvokeData("VirtDisk.h")] public enum APPLY_SNAPSHOT_VHDSET_VERSION { /// Not Supported. APPLY_SNAPSHOT_VHDSET_VERSION_UNSPECIFIED = 0, /// The Version1 member structure will be used. APPLY_SNAPSHOT_VHDSET_VERSION_1 = 1, } /// Contains virtual disk attach request flags. [PInvokeData("VirtDisk.h")] [Flags] public enum ATTACH_VIRTUAL_DISK_FLAG { /// No flags. Use system defaults. ATTACH_VIRTUAL_DISK_FLAG_NONE = 0x00000000, /// /// Attach the virtual disk as read-only. /// /// Windows 7 and Windows Server 2008 R2: This flag is not supported for opening ISO virtual disks until Windows 8 and /// Windows Server 2012. /// /// ATTACH_VIRTUAL_DISK_FLAG_READ_ONLY = 0x00000001, /// /// No drive letters are assigned to the disk's volumes. /// /// Windows 7 and Windows Server 2008 R2: This flag is not supported for opening ISO virtual disks until Windows 8 and /// Windows Server 2012. /// /// ATTACH_VIRTUAL_DISK_FLAG_NO_DRIVE_LETTER = 0x00000002, /// /// Will decouple the virtual disk lifetime from that of the VirtualDiskHandle. The virtual disk will be attached until the /// DetachVirtualDisk function is called, even if all open handles to the virtual disk are closed. /// /// Windows 7 and Windows Server 2008 R2: This flag is not supported for opening ISO virtual disks until Windows 8 and /// Windows Server 2012. /// /// ATTACH_VIRTUAL_DISK_FLAG_PERMANENT_LIFETIME = 0x00000004, /// Reserved. This flag is not supported for ISO virtual disks. ATTACH_VIRTUAL_DISK_FLAG_NO_LOCAL_HOST = 0x00000008, /// Do not assign a custom security descriptor to the disk; use the system default. ATTACH_VIRTUAL_DISK_FLAG_NO_SECURITY_DESCRIPTOR = 0x00000010, /// Default volume encryption policies should not be applied to the disk when attached to the local system. ATTACH_VIRTUAL_DISK_FLAG_BYPASS_DEFAULT_ENCRYPTION_POLICY = 0x00000020, /// Attach the disk as a non-PnP device. ATTACH_VIRTUAL_DISK_FLAG_NON_PNP = 0x00000040, /// Restrict the disk's view to the specified offset and length. ATTACH_VIRTUAL_DISK_FLAG_RESTRICTED_RANGE = 0x00000080, /// Restrict the disk's view to the unique data partition. ATTACH_VIRTUAL_DISK_FLAG_SINGLE_PARTITION = 0x00000100, /// Register the non-PnP disk as a volume with mount manager. ATTACH_VIRTUAL_DISK_FLAG_REGISTER_VOLUME = 0x00000200, } /// /// Contains the version of the virtual hard disk (VHD) ATTACH_VIRTUAL_DISK_PARAMETERS structure to use in calls to VHD functions. /// [PInvokeData("VirtDisk.h")] public enum ATTACH_VIRTUAL_DISK_VERSION { /// Unspecified version. ATTACH_VIRTUAL_DISK_VERSION_UNSPECIFIED = 0, /// Version 1. ATTACH_VIRTUAL_DISK_VERSION_1 = 1, /// Version 2. ATTACH_VIRTUAL_DISK_VERSION_2 = 2 } /// Contains virtual disk compact request flags. [PInvokeData("VirtDisk.h", MSDNShortId = "dd323656")] [Flags] public enum COMPACT_VIRTUAL_DISK_FLAG { /// No flags are specified. COMPACT_VIRTUAL_DISK_FLAG_NONE = 0, /// COMPACT_VIRTUAL_DISK_FLAG_NO_ZERO_SCAN = 1, /// COMPACT_VIRTUAL_DISK_FLAG_NO_BLOCK_MOVES = 2, } /// /// Contains the version of the virtual hard disk (VHD) COMPACT_VIRTUAL_DISK_PARAMETERS structure to use in calls to VHD functions. /// [PInvokeData("VirtDisk.h")] public enum COMPACT_VIRTUAL_DISK_VERSION { /// Unspecified. COMPACT_VIRTUAL_DISK_VERSION_UNSPECIFIED = 0, /// Version 1. COMPACT_VIRTUAL_DISK_VERSION_1 = 1 } /// Contains virtual disk creation flags. [PInvokeData("VirtDisk.h")] [Flags] public enum CREATE_VIRTUAL_DISK_FLAG { /// No special creation conditions; system defaults are used. CREATE_VIRTUAL_DISK_FLAG_NONE = 0x0, /// Pre-allocate all physical space necessary for the virtual size of the disk (e.g. a fixed VHD). CREATE_VIRTUAL_DISK_FLAG_FULL_PHYSICAL_ALLOCATION = 0x1, /// /// Take ownership of the source disk during create from source disk, to insure the source disk does not change during the create /// operation. The source disk must also already be offline or read-only (or both). Ownership is released when create is done. /// This also has a side-effect of disallowing concurrent create from same source disk. Create will fail if ownership cannot be /// obtained or if the source disk is not already offline or read-only. This flag is optional, but highly recommended for creates /// from source disk. No effect for other types of create (no effect for create from source VHD; no effect for create without SourcePath). /// CREATE_VIRTUAL_DISK_FLAG_PREVENT_WRITES_TO_SOURCE_DISK = 0x2, /// /// Do not copy initial virtual disk metadata or block states from the parent VHD; this is useful if the parent VHD is a stand-in /// file and the real parent will be explicitly set later. /// CREATE_VIRTUAL_DISK_FLAG_DO_NOT_COPY_METADATA_FROM_PARENT = 0x4, /// Create the backing storage disk. CREATE_VIRTUAL_DISK_FLAG_CREATE_BACKING_STORAGE = 0x8, /// /// If set, the SourceLimitPath is an change tracking ID, and all data that has changed since that change tracking ID will be /// copied from the source. If clear, the SourceLimitPath is a VHD file path in the source VHD's chain, and all data that is /// present in the children of that VHD in the chain will be copied from the source. /// CREATE_VIRTUAL_DISK_FLAG_USE_CHANGE_TRACKING_SOURCE_LIMIT = 0x10, /// /// If set and the parent VHD has change tracking enabled, the child will have change tracking enabled and will recognize all /// change tracking IDs that currently exist in the parent. If clear or if the parent VHD does not have change tracking /// available, then change tracking will not be enabled in the new VHD. /// CREATE_VIRTUAL_DISK_FLAG_PRESERVE_PARENT_CHANGE_TRACKING_STATE = 0x20, /// /// When creating a VHD Set from source, don't copy the data in the original backing store, but instead use the file as is. If /// this flag is not specified and a source file is passed to CreateVirtualDisk for a VHDSet file, the data in the source file is /// copied. If this flag is set the data is moved. The name of the file may change. /// CREATE_VIRTUAL_DISK_FLAG_VHD_SET_USE_ORIGINAL_BACKING_STORAGE = 0x40, /// /// When creating a fixed virtual disk, take advantage of an underlying sparse file. Only supported on file systems that support /// sparse VDLs. /// CREATE_VIRTUAL_DISK_FLAG_SPARSE_FILE = 0x80, /// Creates a VHD suitable as the backing store for a virtual persistent memory device. CREATE_VIRTUAL_DISK_FLAG_PMEM_COMPATIBLE = 0x100, /// Allow a VHD to be created on a compressed volume. CREATE_VIRTUAL_DISK_FLAG_SUPPORT_COMPRESSED_VOLUMES = 0x200, /// /// Allow a VHD to be created when it may be marked as a sparse file. This flag is a companion to /// CREATE_VIRTUAL_DISK_FLAG_SPARSE_FILE, and overrides the behavior that only allows sparse files on file systems that support /// sparse VDLs. /// CREATE_VIRTUAL_DISK_FLAG_SUPPORT_SPARSE_FILES_ANY_FS = 0x400, } /// /// Contains the version of the virtual hard disk (VHD) CREATE_VIRTUAL_DISK_PARAMETERS structure to use in calls to VHD functions. /// [PInvokeData("VirtDisk.h")] public enum CREATE_VIRTUAL_DISK_VERSION { /// Unsupported CREATE_VIRTUAL_DISK_VERSION_UNSPECIFIED = 0, /// Version 1 CREATE_VIRTUAL_DISK_VERSION_1 = 1, /// Version 2 CREATE_VIRTUAL_DISK_VERSION_2 = 2, /// Version 3 CREATE_VIRTUAL_DISK_VERSION_3 = 3, /// Version 4 CREATE_VIRTUAL_DISK_VERSION_4 = 4, } /// Contains flags affecting the behavior of the DeleteSnapshotVhdSet function. [PInvokeData("VirtDisk.h")] [Flags] public enum DELETE_SNAPSHOT_VHDSET_FLAG { /// No flag specified. DELETE_SNAPSHOT_VHDSET_FLAG_NONE = 0x00000000, /// A reference point should be persisted in the VHD Set after the snapshot is deleted. DELETE_SNAPSHOT_VHDSET_FLAG_PERSIST_RCT = 0x00000001 } /// Contains the version of the DELETE_SNAPHSOT_VHDSET_PARAMETERS structure to use in calls to virtual disk functions. [PInvokeData("VirtDisk.h")] public enum DELETE_SNAPSHOT_VHDSET_VERSION { /// Not supported. DELETE_SNAPSHOT_VHDSET_VERSION_UNSPECIFIED = 0x00000000, /// The Version1 member structure will be used. DELETE_SNAPSHOT_VHDSET_VERSION_1 = 0x00000001 } /// Contains virtual disk dependency information flags. [PInvokeData("VirtDisk.h")] [Flags] public enum DEPENDENT_DISK_FLAG { /// No flags specified. Use system defaults. DEPENDENT_DISK_FLAG_NONE = 0x00000000, /// Multiple files backing the virtual disk. DEPENDENT_DISK_FLAG_MULT_BACKING_FILES = 0x00000001, /// Fully allocated virtual disk. DEPENDENT_DISK_FLAG_FULLY_ALLOCATED = 0x00000002, /// Read-only virtual disk. DEPENDENT_DISK_FLAG_READ_ONLY = 0x00000004, /// The backing file of the virtual disk is not on a local physical disk. DEPENDENT_DISK_FLAG_REMOTE = 0x00000008, /// Reserved. DEPENDENT_DISK_FLAG_SYSTEM_VOLUME = 0x00000010, /// The backing file of the virtual disk is on the system volume. DEPENDENT_DISK_FLAG_SYSTEM_VOLUME_PARENT = 0x00000020, /// The backing file of the virtual disk is on a removable physical disk. DEPENDENT_DISK_FLAG_REMOVABLE = 0x00000040, /// Drive letters are not automatically assigned to the volumes on the virtual disk. DEPENDENT_DISK_FLAG_NO_DRIVE_LETTER = 0x00000080, /// The virtual disk is a parent of a differencing chain. DEPENDENT_DISK_FLAG_PARENT = 0x00000100, /// /// The virtual disk is not surfaced on (attached to) the local host. For example, it is attached to a guest virtual machine. /// DEPENDENT_DISK_FLAG_NO_HOST_DISK = 0x00000200, /// The lifetime of the virtual disk is not tied to any application or process. DEPENDENT_DISK_FLAG_PERMANENT_LIFETIME = 0x00000400, /// Volume backing the virtual storage device file can be a compressed volume. DEPENDENT_DISK_FLAG_SUPPORT_COMPRESSED_VOLUMES = 0x00000800, /// /// Allow the file to be mounted, even if it is sparse and the volume on which it sits does not support a sparse VDL. /// DEPENDENT_DISK_FLAG_ALWAYS_ALLOW_SPARSE = 0x00001000, /// Allow the file to be mounted, even if it is encrypted. DEPENDENT_DISK_FLAG_SUPPORT_ENCRYPTED_FILES = 0x00002000, } /// Contains virtual disk detach request flags. [PInvokeData("VirtDisk.h")] [Flags] public enum DETACH_VIRTUAL_DISK_FLAG { /// No flags. Use system defaults. DETACH_VIRTUAL_DISK_FLAG_NONE = 0 } /// Contains virtual hard disk (VHD) expand request flags. [PInvokeData("VirtDisk.h")] [Flags] public enum EXPAND_VIRTUAL_DISK_FLAG { /// No flags. Use system defaults. EXPAND_VIRTUAL_DISK_FLAG_NONE = 0x00000000, /// EXPAND_VIRTUAL_DISK_FLAG_NOTIFY_CHANGE = 0x00000001, } /// /// Contains the version of the virtual hard disk (VHD) EXPAND_VIRTUAL_DISK_PARAMETERS structure to use in calls to VHD functions. /// [PInvokeData("VirtDisk.h")] public enum EXPAND_VIRTUAL_DISK_VERSION { /// Unspecified. EXPAND_VIRTUAL_DISK_VERSION_UNSPECIFIED = 0, /// Version 1. EXPAND_VIRTUAL_DISK_VERSION_1 = 1 } /// Contains virtual hard disk (VHD) storage dependency request flags. [PInvokeData("VirtDisk.h")] [Flags] public enum GET_STORAGE_DEPENDENCY_FLAG { /// No flags specified. GET_STORAGE_DEPENDENCY_FLAG_NONE = 0x00000000, /// Return information for volumes or disks hosting the volume specified. GET_STORAGE_DEPENDENCY_FLAG_HOST_VOLUMES = 0x00000001, /// The handle provided is to a disk, not a volume or file. GET_STORAGE_DEPENDENCY_FLAG_DISK_HANDLE = 0x00000002 } /// Contains virtual hard disk (VHD) information retrieval identifiers. [PInvokeData("VirtDisk.h")] public enum GET_VIRTUAL_DISK_INFO_VERSION { /// Reserved. This value should not be used. [CorrespondingType(null)] GET_VIRTUAL_DISK_INFO_UNSPECIFIED = 0, /// /// Information related to the virtual disk size, including total size, physical allocation used, block size, and sector size. /// [CorrespondingType(typeof(GET_VIRTUAL_DISK_INFO.GET_VIRTUAL_DISK_INFO_Size))] GET_VIRTUAL_DISK_INFO_SIZE = 1, /// /// The unique identifier. This identifier is persistently stored in the virtual disk and will not change even if the virtual /// disk file is copied to another file. /// [CorrespondingType(typeof(Guid))] GET_VIRTUAL_DISK_INFO_IDENTIFIER = 2, /// The paths to parent virtual disks. Valid only for differencing virtual disks. [CorrespondingType(typeof(GET_VIRTUAL_DISK_INFO.GET_VIRTUAL_DISK_INFO_ParentLocation))] GET_VIRTUAL_DISK_INFO_PARENT_LOCATION = 3, /// The unique identifier of the parent virtual disk. Valid only for differencing virtual disks. [CorrespondingType(typeof(Guid))] GET_VIRTUAL_DISK_INFO_PARENT_IDENTIFIER = 4, /// The time stamp of the parent when the child virtual disk was created. Valid only for differencing virtual disks. [CorrespondingType(typeof(uint))] GET_VIRTUAL_DISK_INFO_PARENT_TIMESTAMP = 5, /// The device identifier and vendor identifier that identify the type of virtual disk. [CorrespondingType(typeof(VIRTUAL_STORAGE_TYPE))] GET_VIRTUAL_DISK_INFO_VIRTUAL_STORAGE_TYPE = 6, /// The type of virtual disk. [CorrespondingType(typeof(VIRTUAL_DISK_INFO_PROVIDER_SUBTYPE))] GET_VIRTUAL_DISK_INFO_PROVIDER_SUBTYPE = 7, /// /// Indicates whether the virtual disk is 4 KB aligned. /// Windows 7 and Windows Server 2008 R2: This value is not supported before Windows 8 and Windows Server 2012. /// [CorrespondingType(typeof(bool))] GET_VIRTUAL_DISK_INFO_IS_4K_ALIGNED = 8, /// /// Details about the physical disk on which the virtual disk resides. /// Windows 7 and Windows Server 2008 R2: This value is not supported before Windows 8 and Windows Server 2012. /// [CorrespondingType(typeof(GET_VIRTUAL_DISK_INFO.GET_VIRTUAL_DISK_INFO_PhysicalDisk))] GET_VIRTUAL_DISK_INFO_PHYSICAL_DISK = 9, /// /// The physical sector size of the virtual disk. /// Windows 7 and Windows Server 2008 R2: This value is not supported before Windows 8 and Windows Server 2012. /// [CorrespondingType(typeof(uint))] GET_VIRTUAL_DISK_INFO_VHD_PHYSICAL_SECTOR_SIZE = 10, /// /// The smallest safe minimum size of the virtual disk. /// Windows 7 and Windows Server 2008 R2: This value is not supported before Windows 8 and Windows Server 2012. /// [CorrespondingType(typeof(ulong))] GET_VIRTUAL_DISK_INFO_SMALLEST_SAFE_VIRTUAL_SIZE = 11, /// /// The fragmentation level of the virtual disk. /// Windows 7 and Windows Server 2008 R2: This value is not supported before Windows 8 and Windows Server 2012. /// [CorrespondingType(typeof(uint))] GET_VIRTUAL_DISK_INFO_FRAGMENTATION = 12, /// /// Whether the virtual disk is currently mounted and in use. /// Windows 8 and Windows Server 2012: This value is not supported before Windows 8.1 and Windows Server 2012 R2. /// [CorrespondingType(typeof(bool))] GET_VIRTUAL_DISK_INFO_IS_LOADED = 13, /// /// The identifier that is uniquely created when a user first creates the virtual disk to attempt to uniquely identify that /// virtual disk. /// Windows 8 and Windows Server 2012: This value is not supported before Windows 8.1 and Windows Server 2012 R2. /// [CorrespondingType(typeof(Guid))] GET_VIRTUAL_DISK_INFO_VIRTUAL_DISK_ID = 14, /// /// The state of resilient change tracking (RCT) for the virtual disk. /// Windows 8.1 and Windows Server 2012 R2: This value is not supported before Windows 10 and Windows Server 2016. /// [CorrespondingType(typeof(GET_VIRTUAL_DISK_INFO.GET_VIRTUAL_DISK_INFO_ChangeTrackingState))] GET_VIRTUAL_DISK_INFO_CHANGE_TRACKING_STATE = 15, } /// Contains virtual hard disk (VHD) merge request flags. [PInvokeData("VirtDisk.h")] [Flags] public enum MERGE_VIRTUAL_DISK_FLAG { /// None. MERGE_VIRTUAL_DISK_FLAG_NONE = 0x00000000 } /// /// Contains the version of the virtual hard disk (VHD) MERGE_VIRTUAL_DISK_PARAMETERS structure to use in calls to VHD functions. /// [PInvokeData("VirtDisk.h")] public enum MERGE_VIRTUAL_DISK_VERSION { /// Not supported. MERGE_VIRTUAL_DISK_VERSION_UNSPECIFIED = 0, /// The Version1 member structure will be used. MERGE_VIRTUAL_DISK_VERSION_1 = 1, /// /// The Version2 member structure will be used. /// Windows 7 and Windows Server 2008 R2: This value is not supported before Windows 8 and Windows Server 2012. /// MERGE_VIRTUAL_DISK_VERSION_2 = 2, } /// Contains virtual hard disk (VHD) mirror request flags. [PInvokeData("VirtDisk.h", MSDNShortId = "hh448679")] [Flags] public enum MIRROR_VIRTUAL_DISK_FLAG { /// The mirror virtual disk file does not exist, and needs to be created. MIRROR_VIRTUAL_DISK_FLAG_NONE = 0x00000000, /// Create the mirror using an existing file. MIRROR_VIRTUAL_DISK_FLAG_EXISTING_FILE = 0x00000001, /// MIRROR_VIRTUAL_DISK_FLAG_SKIP_MIRROR_ACTIVATION = 0x00000002, /// MIRROR_VIRTUAL_DISK_FLAG_ENABLE_SMB_COMPRESSION = 0x00000004, /// MIRROR_VIRTUAL_DISK_FLAG_IS_LIVE_MIGRATION = 0x00000008 } /// /// Contains the version of the virtual disk MIRROR_VIRTUAL_DISK_PARAMETERS structure used by the MirrorVirtualDisk function. /// [PInvokeData("VirtDisk.h", MSDNShortId = "hh448681")] public enum MIRROR_VIRTUAL_DISK_VERSION { /// Unsupported. MIRROR_VIRTUAL_DISK_VERSION_UNSPECIFIED = 0, /// Use the Version1 member. MIRROR_VIRTUAL_DISK_VERSION_1 = 1, } /// Contains flags affecting the behavior of the ModifyVhdSet function. [PInvokeData("VirtDisk.h")] [Flags] public enum MODIFY_VHDSET_FLAG { /// No flag specified. MODIFY_VHDSET_FLAG_NONE = 0x00000000, /// MODIFY_VHDSET_FLAG_WRITEABLE_SNAPSHOT = 0x00000001, } /// Contains the version of the MODIFY_VHDSET_PARAMETERS structure to use in calls to virtual disk functions. [PInvokeData("VirtDisk.h")] public enum MODIFY_VHDSET_VERSION { /// Not Supported. MODIFY_VHDSET_UNSPECIFIED = 0, /// The SnapshotPath member structure will be used. MODIFY_VHDSET_SNAPSHOT_PATH = 1, /// The SnapshotId member structure will be used. MODIFY_VHDSET_REMOVE_SNAPSHOT = 2, /// The DefaultFilePath member structure will be used MODIFY_VHDSET_DEFAULT_SNAPSHOT_PATH = 3, } /// Contains virtual hard disk (VHD) or CD or DVD image file (ISO) open request flags. [PInvokeData("VirtDisk.h", MSDNShortId = "dd323681")] [Flags] public enum OPEN_VIRTUAL_DISK_FLAG { /// No flag specified. OPEN_VIRTUAL_DISK_FLAG_NONE = 0x00000000, /// /// Open the VHD file (backing store) without opening any differencing-chain parents. Used to correct broken parent links. This /// flag is not supported for ISO virtual disks. /// OPEN_VIRTUAL_DISK_FLAG_NO_PARENTS = 0x00000001, /// Reserved. This flag is not supported for ISO virtual disks. OPEN_VIRTUAL_DISK_FLAG_BLANK_FILE = 0x00000002, /// Reserved. This flag is not supported for ISO virtual disks. OPEN_VIRTUAL_DISK_FLAG_BOOT_DRIVE = 0x00000004, /// /// Indicates that the virtual disk should be opened in cached mode. By default the virtual disks are opened using /// FILE_FLAG_NO_BUFFERING and FILE_FLAG_WRITE_THROUGH. /// Windows 7 and Windows Server 2008 R2: This value is not supported before Windows 8 and Windows Server 2012. /// OPEN_VIRTUAL_DISK_FLAG_CACHED_IO = 0x00000008, /// /// Indicates the VHD file is to be opened without opening any differencing-chain parents and the parent chain is to be created /// manually using the AddVirtualDiskParent function. /// Windows 7 and Windows Server 2008 R2: This value is not supported before Windows 8 and Windows Server 2012. /// OPEN_VIRTUAL_DISK_FLAG_CUSTOM_DIFF_CHAIN = 0x00000010, /// This flag causes all backing stores except the leaf backing store to be opened in cached mode. OPEN_VIRTUAL_DISK_FLAG_PARENT_CACHED_IO = 0x00000020, /// This flag causes a Vhd Set file to be opened without any virtual disk. OPEN_VIRTUAL_DISK_FLAG_VHDSET_FILE_ONLY = 0x00000040, /// For differencing disks, relative parent locators are not used when determining the path of a parent VHD. OPEN_VIRTUAL_DISK_FLAG_IGNORE_RELATIVE_PARENT_LOCATOR = 0x00000080, /// /// Disable flushing and FUA (both for payload data and for metadata) for backing files associated with this virtual disk. /// OPEN_VIRTUAL_DISK_FLAG_NO_WRITE_HARDENING = 0x00000100, /// Open the backing store even if it is a compressed file. OPEN_VIRTUAL_DISK_FLAG_SUPPORT_COMPRESSED_VOLUMES = 0x00000200, /// Open the backing store even if it is a sparse file, on any file system. OPEN_VIRTUAL_DISK_FLAG_SUPPORT_SPARSE_FILES_ANY_FS = 0x00000400, /// Open the backing store even if it is an encrypted file. OPEN_VIRTUAL_DISK_FLAG_SUPPORT_ENCRYPTED_FILES = 0x00000800, } /// /// Contains the version of the virtual disk OPEN_VIRTUAL_DISK_PARAMETERS structure to use in calls to virtual disk functions. /// [PInvokeData("VirtDisk.h", MSDNShortId = "dd323683")] public enum OPEN_VIRTUAL_DISK_VERSION { /// Unspecified version. OPEN_VIRTUAL_DISK_VERSION_UNSPECIFIED = 0, /// Use the Version1 member of this structure. OPEN_VIRTUAL_DISK_VERSION_1 = 1, /// /// Use the Version2 member of this structure. /// Windows 7 and Windows Server 2008 R2: This value is not supported before Windows 8 and Windows Server 2012. /// OPEN_VIRTUAL_DISK_VERSION_2 = 2, /// Use the Version3 member of this structure. OPEN_VIRTUAL_DISK_VERSION_3 = 3, } /// Used by . [PInvokeData("VirtDisk.h")] [Flags] public enum QUERY_CHANGES_VIRTUAL_DISK_FLAG { /// Default value. QUERY_CHANGES_VIRTUAL_DISK_FLAG_NONE = 0x00000000, } /// Contains flags affecting the behavior of the RawSCSIVirtualDisk function. [PInvokeData("VirtDisk.h")] [Flags] public enum RAW_SCSI_VIRTUAL_DISK_FLAG { /// No flag specified. RAW_SCSI_VIRTUAL_DISK_FLAG_NONE = 0X00000000 } /// Contains the version of the RAW_SCSI_VIRTUAL_DISK_PARAMETERS structure to use in calls to virtual disk functions. [PInvokeData("VirtDisk.h")] public enum RAW_SCSI_VIRTUAL_DISK_VERSION { /// Unspecified version. RAW_SCSI_VIRTUAL_DISK_VERSION_UNSPECIFIED = 0, /// Use the Version1 member of this structure. RAW_SCSI_VIRTUAL_DISK_VERSION_1 = 1 } /// Enumerates the available flags for the ResizeVirtualDisk function. [PInvokeData("VirtDisk.h", MSDNShortId = "dd323683")] [Flags] public enum RESIZE_VIRTUAL_DISK_FLAG { /// No flags are specified. RESIZE_VIRTUAL_DISK_FLAG_NONE = 0x0, /// /// If this flag is set, skip checking the virtual disk's partition table to ensure that this truncation is safe. Setting this /// flag can cause unrecoverable data loss; use with care. /// RESIZE_VIRTUAL_DISK_FLAG_ALLOW_UNSAFE_VIRTUAL_SIZE = 0x1, /// /// If this flag is set, resize the disk to the smallest virtual size possible without truncating past any existing partitions. /// If this is set, NewSize in RESIZE_VIRTUAL_DISK_PARAMETERS must be zero. /// RESIZE_VIRTUAL_DISK_FLAG_RESIZE_TO_SMALLEST_SAFE_VIRTUAL_SIZE = 0x2, } /// Enumerates the possible versions for parameters for the ResizeVirtualDisk function. [PInvokeData("VirtDisk.h", MSDNShortId = "hh832161")] public enum RESIZE_VIRTUAL_DISK_VERSION { /// The version is not valid. RESIZE_VIRTUAL_DISK_VERSION_UNSPECIFIED = 0, /// Version one of the parameters is used. This is the only supported value. RESIZE_VIRTUAL_DISK_VERSION_1 = 1, } /// Contains the version of the virtual hard disk (VHD) SET_VIRTUAL_DISK_INFO structure to use in calls to VHD functions. [PInvokeData("VirtDisk.h", MSDNShortId = "dd323687")] public enum SET_VIRTUAL_DISK_INFO_VERSION { /// Not used. Will fail the operation. SET_VIRTUAL_DISK_INFO_UNSPECIFIED = 0, /// Parent information is being set. SET_VIRTUAL_DISK_INFO_PARENT_PATH = 1, /// A unique identifier is being set. SET_VIRTUAL_DISK_INFO_IDENTIFIER = 2, /// Sets the parent file path and the child depth. SET_VIRTUAL_DISK_INFO_PARENT_PATH_WITH_DEPTH = 3, /// Sets the physical sector size reported by the VHD. SET_VIRTUAL_DISK_INFO_PHYSICAL_SECTOR_SIZE = 4, /// /// The identifier that is uniquely created when a user first creates the virtual disk to attempt to uniquely identify that /// virtual disk. /// SET_VIRTUAL_DISK_INFO_VIRTUAL_DISK_ID = 5, /// Whether resilient change tracking (RCT) is turned on for the virtual disk. SET_VIRTUAL_DISK_INFO_CHANGE_TRACKING_STATE = 6, /// /// The parent linkage information that differencing VHDs store. Parent linkage information is metadata used to locate and /// correctly identify the next parent in the virtual disk chain. /// SET_VIRTUAL_DISK_INFO_PARENT_LOCATOR = 7, } /// Contains the version of the virtual hard disk (VHD) STORAGE_DEPENDENCY_INFO structure to use in calls to VHD functions. [PInvokeData("VirtDisk.h", MSDNShortId = "dd323691")] public enum STORAGE_DEPENDENCY_INFO_VERSION { /// The version is not specified. STORAGE_DEPENDENCY_INFO_VERSION_UNSPECIFIED = 0, /// Specifies STORAGE_DEPENDENCY_INFO_TYPE_1. STORAGE_DEPENDENCY_INFO_VERSION_1 = 1, /// Specifies STORAGE_DEPENDENCY_INFO_TYPE_2. STORAGE_DEPENDENCY_INFO_VERSION_2 = 2 } /// Contains flags affecting the behavior of the TakeSnapshotVhdSet function. [PInvokeData("VirtDisk.h", MSDNShortId = "mt414221")] [Flags] public enum TAKE_SNAPSHOT_VHDSET_FLAG { /// No flag specified. TAKE_SNAPSHOT_VHDSET_FLAG_NONE = 0x00000000, /// TAKE_SNAPSHOT_VHDSET_FLAG_WRITEABLE = 0x00000001, } /// Enumerates the possible versions for parameters for the TakeSnapshotVhdSet function. [PInvokeData("VirtDisk.h", MSDNShortId = "mt414224")] public enum TAKE_SNAPSHOT_VHDSET_VERSION { /// Not Supported. TAKE_SNAPSHOT_VHDSET_VERSION_UNSPECIFIED = 0, /// The Version1 member structure will be used. TAKE_SNAPSHOT_VHDSET_VERSION_1 = 1 } /// Contains the bitmask for specifying access rights to a virtual hard disk (VHD) or CD or DVD image file (ISO). [PInvokeData("VirtDisk.h", MSDNShortId = "dd323702")] [Flags] public enum VIRTUAL_DISK_ACCESS_MASK { /// /// Open the virtual disk with no access. This is the only supported value when calling CreateVirtualDisk and specifying /// CREATE_VIRTUAL_DISK_VERSION_2 in the VirtualDiskAccessMask parameter. /// VIRTUAL_DISK_ACCESS_NONE = 0x00000000, /// /// Open the virtual disk for read-only attach access. The caller must have READ access to the virtual disk image file. /// /// If used in a request to open a virtual disk that is already open, the other handles must be limited to either /// VIRTUAL_DISK_ACCESS_DETACH or VIRTUAL_DISK_ACCESS_GET_INFO access, otherwise the open request with this flag will fail. /// /// /// Windows 7 and Windows Server 2008 R2: This access right is not supported for opening ISO virtual disks until Windows 8 /// and Windows Server 2012. /// /// VIRTUAL_DISK_ACCESS_ATTACH_RO = 0x00010000, /// /// Open the virtual disk for read/write attaching access. The caller must have (READ | WRITE) access to the virtual disk image file. /// /// If used in a request to open a virtual disk that is already open, the other handles must be limited to either /// VIRTUAL_DISK_ACCESS_DETACH or VIRTUAL_DISK_ACCESS_GET_INFO access, otherwise the open request with this flag will fail. /// /// /// If the virtual disk is part of a differencing chain, the disk for this request cannot be less than the RWDepth specified /// during the prior open request for that differencing chain. /// /// This flag is not supported for ISO virtual disks. /// VIRTUAL_DISK_ACCESS_ATTACH_RW = 0x00020000, /// /// Open the virtual disk to allow detaching of an attached virtual disk. The caller must have (FILE_READ_ATTRIBUTES | /// FILE_READ_DATA) access to the virtual disk image file. /// /// Windows 7 and Windows Server 2008 R2: This access right is not supported for opening ISO virtual disks until Windows 8 /// and Windows Server 2012. /// /// VIRTUAL_DISK_ACCESS_DETACH = 0x00040000, /// /// Information retrieval access to the virtual disk. The caller must have READ access to the virtual disk image file. /// /// Windows 7 and Windows Server 2008 R2: This access right is not supported for opening ISO virtual disks until Windows 8 /// and Windows Server 2012. /// /// VIRTUAL_DISK_ACCESS_GET_INFO = 0x00080000, /// /// Virtual disk creation access. /// This flag is not supported for ISO virtual disks. /// VIRTUAL_DISK_ACCESS_CREATE = 0x00100000, /// /// Open the virtual disk to perform offline meta-operations. The caller must have (READ | WRITE) access to the virtual disk /// image file, up to RWDepth if working with a differencing chain. /// /// If the virtual disk is part of a differencing chain, the backing store (host volume) is opened in RW exclusive mode up to RWDepth. /// /// This flag is not supported for ISO virtual disks. /// VIRTUAL_DISK_ACCESS_METAOPS = 0x00200000, /// Reserved. VIRTUAL_DISK_ACCESS_READ = 0x000d0000, /// /// Allows unrestricted access to the virtual disk. The caller must have unrestricted access rights to the virtual disk image file. /// This flag is not supported for ISO virtual disks. /// VIRTUAL_DISK_ACCESS_ALL = 0x003f0000, /// /// Reserved. /// This flag is not supported for ISO virtual disks. /// VIRTUAL_DISK_ACCESS_WRITABLE = 0x00320000 } /// Provider-specific subtype. Set the Version member to GET_VIRTUAL_DISK_INFO_PROVIDER_SUBTYPE. [PInvokeData("VirtDisk.h")] public enum VIRTUAL_DISK_INFO_PROVIDER_SUBTYPE : uint { /// Fixed. Fixed = 2, /// Dynamically expandable (sparse). DynamicallyExpandable = 3, /// Differencing. Differencing = 4 } /// Device type identifier. [PInvokeData("VirtDisk.h")] public enum VIRTUAL_STORAGE_TYPE_DEVICE_TYPE : uint { /// Device type is unknown or not valid. VIRTUAL_STORAGE_TYPE_DEVICE_UNKNOWN = 0, /// /// CD or DVD image file device type. (.iso file) /// Windows 7 and Windows Server 2008 R2: This value is not supported before Windows 8 and Windows Server 2012. /// VIRTUAL_STORAGE_TYPE_DEVICE_ISO = 1, /// Virtual hard disk device type. (.vhd file) VIRTUAL_STORAGE_TYPE_DEVICE_VHD = 2, /// /// VHDX format virtual hard disk device type. (.vhdx file) /// Windows 7 and Windows Server 2008 R2: This value is not supported before Windows 8 and Windows Server 2012. /// VIRTUAL_STORAGE_TYPE_DEVICE_VHDX = 3, /// /// VHD Set files (.vhds file) are a new shared Virtual Disk model for guest clusters in Windows Server 2016. VHD Set files /// support online resizing of shared virtual disks, support Hyper-V Replica, and can be included in application-consistent checkpoints. /// VIRTUAL_STORAGE_TYPE_DEVICE_VHDSET = 4 } /// Attaches a parent to a virtual disk opened with the OPEN_VIRTUAL_DISK_FLAG_CUSTOM_DIFF_CHAIN flag. /// Handle to a virtual disk. /// Address of a string containing a valid path to the virtual hard disk image to add as a parent. /// /// Status of the request. /// If the function succeeds, the return value is ERROR_SUCCESS. /// If the function fails, the return value is an error code. For more information, see System Error Codes. /// [DllImport(Lib.VirtDisk, ExactSpelling = true)] [PInvokeData("VirtDisk.h")] public static extern Win32Error AddVirtualDiskParent([In] VIRTUAL_DISK_HANDLE VirtualDiskHandle, [MarshalAs(UnmanagedType.LPWStr)] string ParentPath); /// Applies a snapshot of the current virtual disk for VHD Set files. /// /// A handle to an open virtual disk. For information on how to open a virtual disk, see the OpenVirtualDisk function. /// /// A pointer to a valid APPLY_SNAPSHOT_VHDSET_PARAMETERS structure that contains snapshot data. /// A valid combination of values of the APPLY_SNAPSHOT_VHDSET_FLAG enumeration. /// /// Status of the request. /// If the function succeeds, the return value is ERROR_SUCCESS. /// If the function fails, the return value is an error code. For more information, see System Error Codes. /// [DllImport(Lib.VirtDisk, ExactSpelling = true)] [PInvokeData("VirtDisk.h")] public static extern Win32Error ApplySnapshotVhdSet([In] VIRTUAL_DISK_HANDLE VirtualDiskHandle, in APPLY_SNAPSHOT_VHDSET_PARAMETERS Parameters, APPLY_SNAPSHOT_VHDSET_FLAG Flags); /// /// Attaches a virtual hard disk (VHD) or CD or DVD image file (ISO) by locating an appropriate VHD provider to accomplish the attachment. /// /// /// A handle to an open virtual disk. For information on how to open a virtual disk, see the OpenVirtualDisk function. /// /// /// An optional pointer to a SECURITY_DESCRIPTOR to apply to the attached virtual disk. If this parameter is NULL, the security /// descriptor of the virtual disk image file is used. /// /// Ensure that the security descriptor that AttachVirtualDisk applies to the attached virtual disk grants the write attributes /// permission for the user, or that the security descriptor of the virtual disk image file grants the write attributes permission /// for the user if you specify NULL for this parameter. If the security descriptor does not grant write attributes permission for a /// user, Shell displays the following error when the user accesses the attached virtual disk: The Recycle Bin is corrupted. Do you /// want to empty the Recycle Bin for this drive? /// /// /// A valid combination of values of the ATTACH_VIRTUAL_DISK_FLAG enumeration. /// Flags specific to the type of virtual disk being attached. May be zero if none are required. /// A pointer to a valid ATTACH_VIRTUAL_DISK_PARAMETERS structure that contains attachment parameter data. /// An optional pointer to a valid OVERLAPPED structure if asynchronous operation is desired. /// /// Status of the request. /// If the function succeeds, the return value is ERROR_SUCCESS. /// If the function fails, the return value is an error code. For more information, see System Error Codes. /// [DllImport(Lib.VirtDisk, ExactSpelling = true)] [PInvokeData("VirtDisk.h")] public static extern Win32Error AttachVirtualDisk([In] VIRTUAL_DISK_HANDLE VirtualDiskHandle, [In, Optional] PSECURITY_DESCRIPTOR SecurityDescriptor, ATTACH_VIRTUAL_DISK_FLAG Flags, uint ProviderSpecificFlags, in ATTACH_VIRTUAL_DISK_PARAMETERS Parameters, [In, Optional] IntPtr Overlapped); /// /// Attaches a virtual hard disk (VHD) or CD or DVD image file (ISO) by locating an appropriate VHD provider to accomplish the attachment. /// /// /// A handle to an open virtual disk. For information on how to open a virtual disk, see the OpenVirtualDisk function. /// /// /// An optional pointer to a SECURITY_DESCRIPTOR to apply to the attached virtual disk. If this parameter is NULL, the security /// descriptor of the virtual disk image file is used. /// /// Ensure that the security descriptor that AttachVirtualDisk applies to the attached virtual disk grants the write attributes /// permission for the user, or that the security descriptor of the virtual disk image file grants the write attributes permission /// for the user if you specify NULL for this parameter. If the security descriptor does not grant write attributes permission for a /// user, Shell displays the following error when the user accesses the attached virtual disk: The Recycle Bin is corrupted. Do you /// want to empty the Recycle Bin for this drive? /// /// /// A valid combination of values of the ATTACH_VIRTUAL_DISK_FLAG enumeration. /// Flags specific to the type of virtual disk being attached. May be zero if none are required. /// A pointer to a valid ATTACH_VIRTUAL_DISK_PARAMETERS structure that contains attachment parameter data. /// An optional pointer to a valid OVERLAPPED structure if asynchronous operation is desired. /// /// Status of the request. /// If the function succeeds, the return value is ERROR_SUCCESS. /// If the function fails, the return value is an error code. For more information, see System Error Codes. /// [DllImport(Lib.VirtDisk, ExactSpelling = true)] [PInvokeData("VirtDisk.h")] public static extern Win32Error AttachVirtualDisk([In] VIRTUAL_DISK_HANDLE VirtualDiskHandle, [In, Optional] PSECURITY_DESCRIPTOR SecurityDescriptor, ATTACH_VIRTUAL_DISK_FLAG Flags, uint ProviderSpecificFlags, [In, Optional] IntPtr Parameters, [In, Optional] IntPtr Overlapped); /// /// Attaches a virtual hard disk (VHD) or CD or DVD image file (ISO) by locating an appropriate VHD provider to accomplish the attachment. /// /// /// A handle to an open virtual disk. For information on how to open a virtual disk, see the OpenVirtualDisk function. /// /// /// An optional pointer to a SECURITY_DESCRIPTOR to apply to the attached virtual disk. If this parameter is NULL, the security /// descriptor of the virtual disk image file is used. /// /// Ensure that the security descriptor that AttachVirtualDisk applies to the attached virtual disk grants the write attributes /// permission for the user, or that the security descriptor of the virtual disk image file grants the write attributes permission /// for the user if you specify NULL for this parameter. If the security descriptor does not grant write attributes permission for a /// user, Shell displays the following error when the user accesses the attached virtual disk: The Recycle Bin is corrupted. Do you /// want to empty the Recycle Bin for this drive? /// /// /// A valid combination of values of the ATTACH_VIRTUAL_DISK_FLAG enumeration. /// Flags specific to the type of virtual disk being attached. May be zero if none are required. /// A pointer to a valid ATTACH_VIRTUAL_DISK_PARAMETERS structure that contains attachment parameter data. /// An optional pointer to a valid OVERLAPPED structure if asynchronous operation is desired. /// /// Status of the request. /// If the function succeeds, the return value is ERROR_SUCCESS. /// If the function fails, the return value is an error code. For more information, see System Error Codes. /// [DllImport(Lib.VirtDisk, ExactSpelling = true)] [PInvokeData("VirtDisk.h")] public static extern Win32Error AttachVirtualDisk([In] VIRTUAL_DISK_HANDLE VirtualDiskHandle, [In, Optional] PSECURITY_DESCRIPTOR SecurityDescriptor, ATTACH_VIRTUAL_DISK_FLAG Flags, uint ProviderSpecificFlags, in ATTACH_VIRTUAL_DISK_PARAMETERS Parameters, in NativeOverlapped Overlapped); /// Breaks a previously initiated mirror operation and sets the mirror to be the active virtual disk. /// /// A handle to the open mirrored virtual disk. For information on how to open a virtual disk, see the OpenVirtualDisk function. For /// information on how to mirror a virtual disk, see the MirrorVirtualDisk function. /// /// /// Status of the request. /// If the function succeeds, the return value is ERROR_SUCCESS. /// If the function fails, the return value is an error code. For more information, see System Error Codes. /// [PInvokeData("VirtDisk.h", MSDNShortId = "hh448676")] [DllImport(Lib.VirtDisk, ExactSpelling = true)] public static extern Win32Error BreakMirrorVirtualDisk([In] VIRTUAL_DISK_HANDLE VirtualDiskHandle); /// Reduces the size of a virtual hard disk (VHD) backing store file. /// /// A handle to the open virtual disk, which must have been opened using the VIRTUAL_DISK_ACCESS_METAOPS flag in the /// VirtualDiskAccessMask parameter passed to OpenVirtualDisk. For information on how to open a virtual disk, see the OpenVirtualDisk function. /// /// Must be the COMPACT_VIRTUAL_DISK_FLAG_NONE value (0) of the COMPACT_VIRTUAL_DISK_FLAG enumeration. /// /// A optional pointer to a valid COMPACT_VIRTUAL_DISK_PARAMETERS structure that contains compaction parameter data. /// /// An optional pointer to a valid OVERLAPPED structure if asynchronous operation is desired. /// /// If the function succeeds, the return value is ERROR_SUCCESS. If the function fails, the return value is an error code. For more /// information, see System Error Codes. /// /// /// Compaction can be run only on a virtual disk that is dynamically expandable or differencing. /// There are two different types of compaction. /// /// /// /// The first type, file-system-aware compaction, uses the NTFS file system to determine free space. This is done by attaching the /// VHD as a read-only device by including the VIRTUAL_DISK_ACCESS_METAOPS and VIRTUAL_DISK_ACCESS_ATTACH_RO flags in the /// VirtualDiskAccessMask parameter passed to OpenVirtualDisk, attaching the VHD by calling AttachVirtualDisk, and while the VHD is /// attached calling CompactVirtualDisk. Detaching the VHD before compaction is done can cause compaction to return failure before it /// is done (similar to cancellation of compaction). /// /// /// /// /// The second type, file-system-agnostic compaction, does not involve the file system but only looks for VHD blocks filled entirely /// with zeros (0). This is done by including the VIRTUAL_DISK_ACCESS_METAOPS flag in the VirtualDiskAccessMask parameter passed to /// OpenVirtualDisk, and calling CompactVirtualDisk. /// /// /// /// /// File-system-aware compaction is the most efficient compaction type but using first the file-system-aware compaction followed by /// the file-system-agnostic compaction will produce the smallest VHD. /// /// /// A compaction operation on a virtual disk can be safely interrupted and re-run later. Re-opening a virtual disk file that has been /// interrupted may result in the reduction of a virtual disk file's size at the time of opening. /// /// /// Compaction can be CPU-intensive and/or I/O-intensive, depending on how large the virtual disk is and how many blocks require movement. /// /// The CompactVirtualDisk function runs on the virtual disk in the same security context as the caller. /// [PInvokeData("VirtDisk.h")] [DllImport(Lib.VirtDisk, ExactSpelling = true)] public static extern Win32Error CompactVirtualDisk([In] VIRTUAL_DISK_HANDLE VirtualDiskHandle, COMPACT_VIRTUAL_DISK_FLAG Flags, in COMPACT_VIRTUAL_DISK_PARAMETERS Parameters, in NativeOverlapped Overlapped); /// Reduces the size of a virtual hard disk (VHD) backing store file. /// /// A handle to the open virtual disk, which must have been opened using the VIRTUAL_DISK_ACCESS_METAOPS flag in the /// VirtualDiskAccessMask parameter passed to OpenVirtualDisk. For information on how to open a virtual disk, see the OpenVirtualDisk function. /// /// Must be the COMPACT_VIRTUAL_DISK_FLAG_NONE value (0) of the COMPACT_VIRTUAL_DISK_FLAG enumeration. /// /// A optional pointer to a valid COMPACT_VIRTUAL_DISK_PARAMETERS structure that contains compaction parameter data. /// /// An optional pointer to a valid OVERLAPPED structure if asynchronous operation is desired. /// /// If the function succeeds, the return value is ERROR_SUCCESS. If the function fails, the return value is an error code. For more /// information, see System Error Codes. /// /// /// Compaction can be run only on a virtual disk that is dynamically expandable or differencing. /// There are two different types of compaction. /// /// /// /// The first type, file-system-aware compaction, uses the NTFS file system to determine free space. This is done by attaching the /// VHD as a read-only device by including the VIRTUAL_DISK_ACCESS_METAOPS and VIRTUAL_DISK_ACCESS_ATTACH_RO flags in the /// VirtualDiskAccessMask parameter passed to OpenVirtualDisk, attaching the VHD by calling AttachVirtualDisk, and while the VHD is /// attached calling CompactVirtualDisk. Detaching the VHD before compaction is done can cause compaction to return failure before it /// is done (similar to cancellation of compaction). /// /// /// /// /// The second type, file-system-agnostic compaction, does not involve the file system but only looks for VHD blocks filled entirely /// with zeros (0). This is done by including the VIRTUAL_DISK_ACCESS_METAOPS flag in the VirtualDiskAccessMask parameter passed to /// OpenVirtualDisk, and calling CompactVirtualDisk. /// /// /// /// /// File-system-aware compaction is the most efficient compaction type but using first the file-system-aware compaction followed by /// the file-system-agnostic compaction will produce the smallest VHD. /// /// /// A compaction operation on a virtual disk can be safely interrupted and re-run later. Re-opening a virtual disk file that has been /// interrupted may result in the reduction of a virtual disk file's size at the time of opening. /// /// /// Compaction can be CPU-intensive and/or I/O-intensive, depending on how large the virtual disk is and how many blocks require movement. /// /// The CompactVirtualDisk function runs on the virtual disk in the same security context as the caller. /// [PInvokeData("VirtDisk.h")] [DllImport(Lib.VirtDisk, ExactSpelling = true)] public static extern Win32Error CompactVirtualDisk([In] VIRTUAL_DISK_HANDLE VirtualDiskHandle, COMPACT_VIRTUAL_DISK_FLAG Flags, in COMPACT_VIRTUAL_DISK_PARAMETERS Parameters, [In, Optional] IntPtr Overlapped); /// Reduces the size of a virtual hard disk (VHD) backing store file. /// /// A handle to the open virtual disk, which must have been opened using the VIRTUAL_DISK_ACCESS_METAOPS flag in the /// VirtualDiskAccessMask parameter passed to OpenVirtualDisk. For information on how to open a virtual disk, see the OpenVirtualDisk function. /// /// Must be the COMPACT_VIRTUAL_DISK_FLAG_NONE value (0) of the COMPACT_VIRTUAL_DISK_FLAG enumeration. /// /// A optional pointer to a valid COMPACT_VIRTUAL_DISK_PARAMETERS structure that contains compaction parameter data. /// /// An optional pointer to a valid OVERLAPPED structure if asynchronous operation is desired. /// /// If the function succeeds, the return value is ERROR_SUCCESS. If the function fails, the return value is an error code. For more /// information, see System Error Codes. /// /// /// Compaction can be run only on a virtual disk that is dynamically expandable or differencing. /// There are two different types of compaction. /// /// /// /// The first type, file-system-aware compaction, uses the NTFS file system to determine free space. This is done by attaching the /// VHD as a read-only device by including the VIRTUAL_DISK_ACCESS_METAOPS and VIRTUAL_DISK_ACCESS_ATTACH_RO flags in the /// VirtualDiskAccessMask parameter passed to OpenVirtualDisk, attaching the VHD by calling AttachVirtualDisk, and while the VHD is /// attached calling CompactVirtualDisk. Detaching the VHD before compaction is done can cause compaction to return failure before it /// is done (similar to cancellation of compaction). /// /// /// /// /// The second type, file-system-agnostic compaction, does not involve the file system but only looks for VHD blocks filled entirely /// with zeros (0). This is done by including the VIRTUAL_DISK_ACCESS_METAOPS flag in the VirtualDiskAccessMask parameter passed to /// OpenVirtualDisk, and calling CompactVirtualDisk. /// /// /// /// /// File-system-aware compaction is the most efficient compaction type but using first the file-system-aware compaction followed by /// the file-system-agnostic compaction will produce the smallest VHD. /// /// /// A compaction operation on a virtual disk can be safely interrupted and re-run later. Re-opening a virtual disk file that has been /// interrupted may result in the reduction of a virtual disk file's size at the time of opening. /// /// /// Compaction can be CPU-intensive and/or I/O-intensive, depending on how large the virtual disk is and how many blocks require movement. /// /// The CompactVirtualDisk function runs on the virtual disk in the same security context as the caller. /// [PInvokeData("VirtDisk.h")] [DllImport(Lib.VirtDisk, ExactSpelling = true)] public static extern Win32Error CompactVirtualDisk([In] VIRTUAL_DISK_HANDLE VirtualDiskHandle, COMPACT_VIRTUAL_DISK_FLAG Flags, [In, Optional] IntPtr Parameters, [In, Optional] IntPtr Overlapped); /// /// Creates a virtual hard disk (VHD) image file, either using default parameters or using an existing VHD or physical disk. /// /// /// A pointer to a VIRTUAL_STORAGE_TYPE structure that contains the desired disk type and vendor information. /// /// A pointer to a valid string that represents the path to the new virtual disk image file. /// The VIRTUAL_DISK_ACCESS_MASK value to use when opening the newly created virtual disk file. /// /// An optional pointer to a SECURITY_DESCRIPTOR to apply to the virtual disk image file. If this parameter is NULL, the parent /// directory's security descriptor will be used. /// /// Creation flags, which must be a valid combination of the CREATE_VIRTUAL_DISK_FLAG enumeration. /// Flags specific to the type of virtual disk being created. May be zero if none are required. /// A pointer to a valid CREATE_VIRTUAL_DISK_PARAMETERS structure that contains creation parameter data. /// An optional pointer to a valid OVERLAPPED structure if asynchronous operation is desired. /// A pointer to the handle object that represents the newly created virtual disk. /// /// If the function succeeds, the return value is ERROR_SUCCESS and the Handle parameter contains a valid pointer to the new virtual /// disk object. If the function fails, the return value is an error code and the value of the Handle parameter is undefined. /// [PInvokeData("VirtDisk.h")] [DllImport(Lib.VirtDisk, CharSet = CharSet.Unicode, ExactSpelling = true)] public static extern Win32Error CreateVirtualDisk(in VIRTUAL_STORAGE_TYPE VirtualStorageType, string Path, VIRTUAL_DISK_ACCESS_MASK VirtualDiskAccessMask, [In, Optional] PSECURITY_DESCRIPTOR SecurityDescriptor, CREATE_VIRTUAL_DISK_FLAG Flags, int ProviderSpecificFlags, in CREATE_VIRTUAL_DISK_PARAMETERS Parameters, [In, Optional] IntPtr Overlapped, out SafeVIRTUAL_DISK_HANDLE Handle); /// /// Creates a virtual hard disk (VHD) image file, either using default parameters or using an existing VHD or physical disk. /// /// /// A pointer to a VIRTUAL_STORAGE_TYPE structure that contains the desired disk type and vendor information. /// /// A pointer to a valid string that represents the path to the new virtual disk image file. /// The VIRTUAL_DISK_ACCESS_MASK value to use when opening the newly created virtual disk file. /// /// An optional pointer to a SECURITY_DESCRIPTOR to apply to the virtual disk image file. If this parameter is NULL, the parent /// directory's security descriptor will be used. /// /// Creation flags, which must be a valid combination of the CREATE_VIRTUAL_DISK_FLAG enumeration. /// Flags specific to the type of virtual disk being created. May be zero if none are required. /// A pointer to a valid CREATE_VIRTUAL_DISK_PARAMETERS structure that contains creation parameter data. /// An optional pointer to a valid OVERLAPPED structure if asynchronous operation is desired. /// A pointer to the handle object that represents the newly created virtual disk. /// /// If the function succeeds, the return value is ERROR_SUCCESS and the Handle parameter contains a valid pointer to the new virtual /// disk object. If the function fails, the return value is an error code and the value of the Handle parameter is undefined. /// [PInvokeData("VirtDisk.h")] [DllImport(Lib.VirtDisk, CharSet = CharSet.Unicode, ExactSpelling = true)] public static extern Win32Error CreateVirtualDisk(in VIRTUAL_STORAGE_TYPE VirtualStorageType, string Path, VIRTUAL_DISK_ACCESS_MASK VirtualDiskAccessMask, [In, Optional] PSECURITY_DESCRIPTOR SecurityDescriptor, CREATE_VIRTUAL_DISK_FLAG Flags, int ProviderSpecificFlags, in CREATE_VIRTUAL_DISK_PARAMETERS Parameters, in NativeOverlapped Overlapped, out SafeVIRTUAL_DISK_HANDLE Handle); /// Deletes a snapshot from a VHD Set file. /// A handle to the open virtual disk. /// A pointer to a valid DELETE_SNAPSHOT_VHDSET_PARAMETERS structure that contains snapshot deletion data. /// Snapshot deletion flags, which must be a valid combination of the DELETE_SNAPSHOT_VHDSET_FLAG enumeration. /// /// If the function succeeds, the return value is ERROR_SUCCESS and the Handle parameter contains a valid pointer to the new virtual /// disk object. If the function fails, the return value is an error code and the value of the Handle parameter is undefined. /// [PInvokeData("VirtDisk.h")] [DllImport(Lib.VirtDisk, ExactSpelling = true)] public static extern Win32Error DeleteSnapshotVhdSet([In] VIRTUAL_DISK_HANDLE VirtualDiskHandle, in DELETE_SNAPSHOT_VHDSET_PARAMETERS Parameters, DELETE_SNAPSHOT_VHDSET_FLAG Flags); /// Deletes metadata from a virtual disk. /// A handle to the open virtual disk. /// The item to be deleted. /// /// If the function succeeds, the return value is ERROR_SUCCESS and the Handle parameter contains a valid pointer to the new virtual /// disk object. If the function fails, the return value is an error code and the value of the Handle parameter is undefined. /// [PInvokeData("VirtDisk.h")] [DllImport(Lib.VirtDisk, ExactSpelling = true)] public static extern Win32Error DeleteVirtualDiskMetadata([In] VIRTUAL_DISK_HANDLE VirtualDiskHandle, in Guid Item); /// /// Detaches a virtual hard disk (VHD) or CD or DVD image file (ISO) by locating an appropriate virtual disk provider to accomplish /// the operation. /// /// /// A handle to an open virtual disk, which must have been opened using the VIRTUAL_DISK_ACCESS_DETACH flag set in the /// VirtualDiskAccessMask parameter to the OpenVirtualDisk function. For information on how to open a virtual disk, see the /// OpenVirtualDisk function. /// /// A valid combination of values of the DETACH_VIRTUAL_DISK_FLAG enumeration. /// Flags specific to the type of virtual disk being detached. May be zero if none are required. /// /// If the function succeeds, the return value is ERROR_SUCCESS. If the function fails, the return value is an error code. For more /// information, see System Error Codes. /// [PInvokeData("VirtDisk.h")] [DllImport(Lib.VirtDisk, ExactSpelling = true)] public static extern Win32Error DetachVirtualDisk([In] VIRTUAL_DISK_HANDLE VirtualDiskHandle, DETACH_VIRTUAL_DISK_FLAG Flags, int ProviderSpecificFlags); /// Enumerates the metadata associated with a virtual disk. /// Handle to an open virtual disk. /// /// Address of a ULONG. On input, the value indicates the number of elements in the buffer pointed to by the Items parameter. On /// output, the value contains the number of items retrieved. If the buffer was too small, the API will fail and return /// ERROR_INSUFFICIENT_BUFFER and the ULONG will contain the required buffer size. /// /// /// Address of a buffer to be filled with the GUIDs representing the metadata. The GetVirtualDiskMetadata function can be used to /// retrieve the data represented by each GUID. /// /// /// If the function succeeds, the return value is ERROR_SUCCESS. If the function fails, the return value is an error code. For more /// information, see System Error Codes. /// [PInvokeData("VirtDisk.h")] [DllImport(Lib.VirtDisk, ExactSpelling = true)] public static extern Win32Error EnumerateVirtualDiskMetadata([In] VIRTUAL_DISK_HANDLE VirtualDiskHandle, ref uint NumberOfItems, [Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)] Guid[] Items); /// Increases the size of a fixed or dynamic virtual hard disk (VHD). /// /// A handle to the open VHD, which must have been opened using the VIRTUAL_DISK_ACCESS_METAOPS flag. /// /// Must be the EXPAND_VIRTUAL_DISK_FLAG_NONE value of the EXPAND_VIRTUAL_DISK_FLAG enumeration. /// A pointer to a valid EXPAND_VIRTUAL_DISK_PARAMETERS structure that contains expansion parameter data. /// An optional pointer to a valid OVERLAPPED structure if asynchronous operation is desired. /// /// If the function succeeds, the return value is ERROR_SUCCESS and the Handle parameter contains a valid pointer to the new virtual /// disk object. If the function fails, the return value is an error code and the value of the Handle parameter is undefined. /// [PInvokeData("VirtDisk.h")] [DllImport(Lib.VirtDisk, ExactSpelling = true)] public static extern Win32Error ExpandVirtualDisk([In] VIRTUAL_DISK_HANDLE VirtualDiskHandle, EXPAND_VIRTUAL_DISK_FLAG Flags, in EXPAND_VIRTUAL_DISK_PARAMETERS Parameters, [In, Optional] IntPtr Overlapped); /// Increases the size of a fixed or dynamic virtual hard disk (VHD). /// /// A handle to the open VHD, which must have been opened using the VIRTUAL_DISK_ACCESS_METAOPS flag. /// /// Must be the EXPAND_VIRTUAL_DISK_FLAG_NONE value of the EXPAND_VIRTUAL_DISK_FLAG enumeration. /// A pointer to a valid EXPAND_VIRTUAL_DISK_PARAMETERS structure that contains expansion parameter data. /// An optional pointer to a valid OVERLAPPED structure if asynchronous operation is desired. /// /// If the function succeeds, the return value is ERROR_SUCCESS and the Handle parameter contains a valid pointer to the new virtual /// disk object. If the function fails, the return value is an error code and the value of the Handle parameter is undefined. /// [PInvokeData("VirtDisk.h")] [DllImport(Lib.VirtDisk, ExactSpelling = true)] public static extern Win32Error ExpandVirtualDisk([In] VIRTUAL_DISK_HANDLE VirtualDiskHandle, EXPAND_VIRTUAL_DISK_FLAG Flags, in EXPAND_VIRTUAL_DISK_PARAMETERS Parameters, in NativeOverlapped Overlapped); /// Get the paths of all attached virtual disks. /// Size of the buffer supplied in . /// Buffer of sufficient size to hold all returned paths. /// /// If the function succeeds, the return value is ERROR_SUCCESS and the Handle parameter contains a valid pointer to the new virtual /// disk object. If the function fails, the return value is an error code and the value of the Handle parameter is undefined. /// [PInvokeData("VirtDisk.h")] [DllImport(Lib.VirtDisk, ExactSpelling = true)] public static extern Win32Error GetAllAttachedVirtualDiskPhysicalPaths(ref uint PathsBufferSizeInBytes, [Out] IntPtr PathsBuffer); /// /// Returns the relationships between virtual hard disks (VHDs) or the volumes contained within those disks and their parent disk or volume. /// /// A handle to an open VHD. /// A valid combination of GET_STORAGE_DEPENDENCY_FLAG values. /// /// Size, in bytes, of the STORAGE_DEPENDENCY_INFO structure that the StorageDependencyInfo parameter refers to. /// /// /// A pointer to a valid structure, which is a variable-length structure. /// /// An optional pointer to a ULONG that receives the size used. /// /// If the function succeeds, the return value is ERROR_SUCCESS and the Handle parameter contains a valid pointer to the new virtual /// disk object. If the function fails, the return value is an error code and the value of the Handle parameter is undefined. /// [PInvokeData("VirtDisk.h")] [DllImport(Lib.VirtDisk, ExactSpelling = true)] public static extern Win32Error GetStorageDependencyInformation([In] IntPtr ObjectHandle, GET_STORAGE_DEPENDENCY_FLAG Flags, int StorageDependencyInfoSize, [In, Out] IntPtr StorageDependencyInfo, out int SizeUsed); /// Retrieves information about a virtual hard disk (VHD). /// /// A handle to the open VHD, which must have been opened using the VIRTUAL_DISK_ACCESS_GET_INFO flag. /// /// A pointer to a ULONG that contains the size of the VirtualDiskInfo parameter. /// /// A pointer to a valid structure. The format of the data returned is dependent on the value /// passed in the Version member by the caller. /// /// A pointer to a ULONG that contains the size used. /// /// If the function succeeds, the return value is ERROR_SUCCESS and the Handle parameter contains a valid pointer to the new virtual /// disk object. If the function fails, the return value is an error code and the value of the Handle parameter is undefined. /// [PInvokeData("VirtDisk.h")] [DllImport(Lib.VirtDisk, ExactSpelling = true)] public static extern Win32Error GetVirtualDiskInformation([In] VIRTUAL_DISK_HANDLE VirtualDiskHandle, ref uint VirtualDiskInfoSize, ref GET_VIRTUAL_DISK_INFO VirtualDiskInfo, out uint SizeUsed); /// Retrieves information about a virtual hard disk (VHD). /// /// A handle to the open VHD, which must have been opened using the VIRTUAL_DISK_ACCESS_GET_INFO flag. /// /// A pointer to a ULONG that contains the size of the VirtualDiskInfo parameter. /// /// A pointer to a valid structure. The format of the data returned is dependent on the value /// passed in the Version member by the caller. /// /// A pointer to a ULONG that contains the size used. /// /// If the function succeeds, the return value is ERROR_SUCCESS and the Handle parameter contains a valid pointer to the new virtual /// disk object. If the function fails, the return value is an error code and the value of the Handle parameter is undefined. /// [PInvokeData("VirtDisk.h")] [DllImport(Lib.VirtDisk, ExactSpelling = true)] public static extern Win32Error GetVirtualDiskInformation([In] VIRTUAL_DISK_HANDLE VirtualDiskHandle, ref uint VirtualDiskInfoSize, [In, Out] IntPtr VirtualDiskInfo, out uint SizeUsed); /// Retrieves the specified metadata from the virtual disk. /// Handle to an open virtual disk. /// Address of a GUID identifying the metadata to retrieve. /// /// Address of a ULONG. On input, the value indicates the size, in bytes, of the buffer pointed to by the MetaData parameter. On /// output, the value contains size, in bytes, of the retrieved metadata. If the buffer was too small, the API will fail and return /// ERROR_INSUFFICIENT_BUFFER, putting the required size in the ULONG and the buffer will contain the start of the metadata. /// /// Address of the buffer where the metadata is to be stored. /// /// If the function succeeds, the return value is ERROR_SUCCESS. If the buffer pointed to by the Items parameter was too small, the /// return value is ERROR_INSUFFICIENT_BUFFER. If the function fails, the return value is an error code.For more information, see /// System Error Codes. /// [PInvokeData("VirtDisk.h")] [DllImport(Lib.VirtDisk, ExactSpelling = true)] public static extern Win32Error GetVirtualDiskMetadata([In] VIRTUAL_DISK_HANDLE VirtualDiskHandle, in Guid Item, ref uint MetaDataSize, [Out] IntPtr MetaData); /// Checks the progress of an asynchronous virtual hard disk (VHD) operation. /// A valid handle to a virtual disk with a pending asynchronous operation. /// /// A pointer to a valid OVERLAPPED structure. This parameter must reference the same structure previously sent to the virtual disk /// operation being checked for progress. /// /// A pointer to a VIRTUAL_DISK_PROGRESS structure that receives the current virtual disk operation progress. /// /// If the function succeeds, the return value is ERROR_SUCCESS and the Progress parameter will be populated with the current virtual /// disk operation progress. If the function fails, the return value is an error code and the value of the Progress parameter is /// undefined. For more information, see System Error Codes. /// [PInvokeData("VirtDisk.h")] [DllImport(Lib.VirtDisk, ExactSpelling = true)] public static extern Win32Error GetVirtualDiskOperationProgress([In] VIRTUAL_DISK_HANDLE VirtualDiskHandle, in NativeOverlapped Overlapped, out VIRTUAL_DISK_PROGRESS Progress); /// Retrieves the path to the physical device object that contains a virtual hard disk (VHD). /// /// A handle to the open VHD, which must have been opened using the VIRTUAL_DISK_ACCESS_GET_INFO flag. /// /// The size, in bytes, of the buffer pointed to by the DiskPath parameter. /// A target buffer to receive the path of the physical disk device that contains the VHD. /// /// If the function succeeds, the return value is ERROR_SUCCESS and the Handle parameter contains a valid pointer to the new virtual /// disk object. If the function fails, the return value is an error code and the value of the Handle parameter is undefined. /// [PInvokeData("VirtDisk.h")] [DllImport(Lib.VirtDisk, CharSet = CharSet.Unicode, ExactSpelling = true)] public static extern Win32Error GetVirtualDiskPhysicalPath([In] VIRTUAL_DISK_HANDLE VirtualDiskHandle, ref int DiskPathSizeInBytes, [Out] StringBuilder DiskPath); /// Merges a child virtual hard disk (VHD) in a differencing chain with parent disks in the chain. /// /// A handle to the open VHD, which must have been opened using the VIRTUAL_DISK_ACCESS_METAOPS flag. /// /// Must be the MERGE_VIRTUAL_DISK_FLAG_NONE value of the MERGE_VIRTUAL_DISK_FLAG enumeration. /// A pointer to a valid MERGE_VIRTUAL_DISK_PARAMETERS structure that contains merge parameter data. /// An optional pointer to a valid OVERLAPPED structure if asynchronous operation is desired. /// /// If the function succeeds, the return value is ERROR_SUCCESS and the Handle parameter contains a valid pointer to the new virtual /// disk object. If the function fails, the return value is an error code and the value of the Handle parameter is undefined. /// [PInvokeData("VirtDisk.h")] [DllImport(Lib.VirtDisk, ExactSpelling = true)] public static extern Win32Error MergeVirtualDisk([In] VIRTUAL_DISK_HANDLE VirtualDiskHandle, MERGE_VIRTUAL_DISK_FLAG Flags, in MERGE_VIRTUAL_DISK_PARAMETERS Parameters, [In, Optional] IntPtr Overlapped); /// Merges a child virtual hard disk (VHD) in a differencing chain with parent disks in the chain. /// /// A handle to the open VHD, which must have been opened using the VIRTUAL_DISK_ACCESS_METAOPS flag. /// /// Must be the MERGE_VIRTUAL_DISK_FLAG_NONE value of the MERGE_VIRTUAL_DISK_FLAG enumeration. /// A pointer to a valid MERGE_VIRTUAL_DISK_PARAMETERS structure that contains merge parameter data. /// An optional pointer to a valid OVERLAPPED structure if asynchronous operation is desired. /// /// If the function succeeds, the return value is ERROR_SUCCESS and the Handle parameter contains a valid pointer to the new virtual /// disk object. If the function fails, the return value is an error code and the value of the Handle parameter is undefined. /// [PInvokeData("VirtDisk.h")] [DllImport(Lib.VirtDisk, ExactSpelling = true)] public static extern Win32Error MergeVirtualDisk([In] VIRTUAL_DISK_HANDLE VirtualDiskHandle, MERGE_VIRTUAL_DISK_FLAG Flags, in MERGE_VIRTUAL_DISK_PARAMETERS Parameters, in NativeOverlapped Overlapped); /// /// Initiates a mirror operation for a virtual disk. Once the mirroring operation is initiated it will not complete until either /// CancelIo or CancelIoEx is called to cancel all I/O on the VirtualDiskHandle, leaving the original file as the current or /// BreakMirrorVirtualDisk is called to stop using the original file and only use the mirror. GetVirtualDiskOperationProgress can be /// used to determine if the disks are fully mirrored and writes go to both virtual disks. /// /// /// A handle to the open virtual disk. For information on how to open a virtual disk, see the OpenVirtualDisk function. /// /// A valid combination of values from the MIRROR_VIRTUAL_DISK_FLAG enumeration. /// Address of a MIRROR_VIRTUAL_DISK_PARAMETERS structure containing mirror parameter data. /// Address of an OVERLAPPEDstructure. This parameter is required. /// /// If the function succeeds, the return value is ERROR_SUCCESS. If the function fails, the return value is an error code.For more /// information, see System Error Codes. /// [PInvokeData("VirtDisk.h")] [DllImport(Lib.VirtDisk, ExactSpelling = true)] public static extern Win32Error MirrorVirtualDisk([In] VIRTUAL_DISK_HANDLE VirtualDiskHandle, MIRROR_VIRTUAL_DISK_FLAG Flags, in MIRROR_VIRTUAL_DISK_PARAMETERS Parameters, in NativeOverlapped Overlapped); /// /// Modifies the internal contents of a virtual disk file. Can be used to set the active leaf, or to fix up snapshot entries. /// /// A handle to the open virtual disk. This must be a VHD Set file. /// A pointer to a valid MODIFY_VHDSET_PARAMETERS structure that contains modification data. /// Modification flags, which must be a valid combination of the MODIFY_VHDSET_FLAG enumeration. /// /// Status of the request. If the function succeeds, the return value is ERROR_SUCCESS. If the function fails, the return value is an /// error code.For more information, see System Error Codes. /// [PInvokeData("VirtDisk.h")] [DllImport(Lib.VirtDisk, ExactSpelling = true)] public static extern Win32Error ModifyVhdSet([In] VIRTUAL_DISK_HANDLE VirtualDiskHandle, in MODIFY_VHDSET_PARAMETERS Parameters, MODIFY_VHDSET_FLAG Flags); /// Opens a virtual hard disk (VHD) or CD or DVD image file (ISO) for use. /// A pointer to a valid VIRTUAL_STORAGE_TYPE structure. /// A pointer to a valid path to the virtual disk image to open. /// A valid value of the VIRTUAL_DISK_ACCESS_MASK enumeration. /// A valid combination of values of the OPEN_VIRTUAL_DISK_FLAG enumeration. /// An optional pointer to a valid OPEN_VIRTUAL_DISK_PARAMETERS structure. Can be NULL. /// A pointer to the handle object that represents the open virtual disk. /// /// If the function succeeds, the return value is ERROR_SUCCESS (0) and the Handle parameter contains a valid pointer to the new /// virtual disk object. /// /// If the function fails, the return value is an error code and the value of the Handle parameter is undefined. For more /// information, see System Error Codes. /// /// [PInvokeData("VirtDisk.h")] [DllImport(Lib.VirtDisk, ExactSpelling = true, ThrowOnUnmappableChar = true)] public static extern Win32Error OpenVirtualDisk(in VIRTUAL_STORAGE_TYPE VirtualStorageType, [MarshalAs(UnmanagedType.LPWStr)] string Path, VIRTUAL_DISK_ACCESS_MASK VirtualDiskAccessMask, OPEN_VIRTUAL_DISK_FLAG Flags, [In, Optional] OPEN_VIRTUAL_DISK_PARAMETERS Parameters, out SafeVIRTUAL_DISK_HANDLE Handle); /// /// Retrieves information about changes to the specified areas of a virtual hard disk (VHD) that are tracked by resilient change /// tracking (RCT). /// /// /// A handle to the open VHD, which must have been opened using the VIRTUAL_DISK_ACCESS_GET_INFO flag set in the /// VirtualDiskAccessMask parameter to the OpenVirtualDisk function. For information on how to open a VHD, see the OpenVirtualDisk function. /// /// /// A pointer to a string that specifies the change tracking identifier for the change that identifies the state of the virtual disk /// that you want to use as the basis of comparison to determine whether the specified area of the VHD has changed. /// /// /// An unsigned long integer that specifies the distance from the start of the VHD to the beginning of the area of the VHD that you /// want to check for changes, in bytes. /// /// /// An unsigned long integer that specifies the length of the area of the VHD that you want to check for changes, in bytes. /// /// Reserved. Set to QUERY_CHANGES_VIRTUAL_DISK_FLAG_NONE. /// /// An array of QUERY_CHANGES_VIRTUAL_DISK_RANGE structures that indicates the areas of the virtual disk within the area that the /// ByteOffset and ByteLength parameters specify that have changed since the change tracking identifier that the ChangeTrackingId /// parameter specifies was sealed. /// /// /// An address of an unsigned long integer. On input, the value indicates the number of QUERY_CHANGES_VIRTUAL_DISK_RANGE structures /// that the array that the Ranges parameter points to can hold. On output, the value contains the number of /// QUERY_CHANGES_VIRTUAL_DISK_RANGE structures that the method placed in the array. /// /// /// A pointer to an unsigned long integer that indicates the total number of bytes that the method processed, which indicates for how /// much of the area that the BytesLength parameter specifies that changes were captured in the available space of the array that the /// Ranges parameter specifies. /// /// /// The status of the request. If the function succeeds, the return value is ERROR_SUCCESS and the Ranges parameter contains the /// requested information. If the function fails, the return value is an error code.For more information, see System Error Codes. /// [PInvokeData("VirtDisk.h")] [DllImport(Lib.VirtDisk, ExactSpelling = true)] public static extern Win32Error QueryChangesVirtualDisk([In] VIRTUAL_DISK_HANDLE VirtualDiskHandle, [MarshalAs(UnmanagedType.LPWStr)] string ChangeTrackingId, ulong ByteOffset, ulong ByteLength, QUERY_CHANGES_VIRTUAL_DISK_FLAG Flags, [Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 6)] QUERY_CHANGES_VIRTUAL_DISK_RANGE[] Ranges, ref uint RangeCount, out ulong ProcessedLength); /// Issues an embedded SCSI request directly to a virtual hard disk. /// /// A handle to an open virtual disk. For information on how to open a virtual disk, see the OpenVirtualDisk function. This handle /// may also be a handle to a Remote Shared Virtual Disk. For information on how to open a Remote Shared Virtual Disk, see the Remote /// Shared Virtual Disk Protocol documentation. /// /// A pointer to a valid RAW_SCSI_VIRTUAL_DISK_PARAMETERS structure that contains snapshot deletion data. /// SCSI virtual disk flags, which must be a valid combination of the RAW_SCSI_VIRTUAL_DISK_FLAG enumeration. /// /// A pointer to a RAW_SCSI_VIRTUAL_DISK_RESPONSE structure that contains the results of processing the SCSI command. /// /// /// Status of the request. If the function succeeds, the return value is ERROR_SUCCESS. A return of ERROR_SUCCESS only means the /// request was received by the virtual disk.The SCSI command itself could have failed due to an invalid device state, an unsupported /// SCSI command, or another error. If the function fails, the return value is an error code.For more information, see System Error Codes. /// [PInvokeData("VirtDisk.h")] [DllImport(Lib.VirtDisk, ExactSpelling = true)] public static extern Win32Error RawSCSIVirtualDisk([In] VIRTUAL_DISK_HANDLE VirtualDiskHandle, in RAW_SCSI_VIRTUAL_DISK_PARAMETERS Parameters, RAW_SCSI_VIRTUAL_DISK_FLAG Flags, out RAW_SCSI_VIRTUAL_DISK_RESPONSE Response); /// Resizes a virtual disk. /// Handle to an open virtual disk. /// Zero or more flags enumerated from the RESIZE_VIRTUAL_DISK_FLAG enumeration. /// Address of a RESIZE_VIRTUAL_DISK_PARAMETERS structure containing the new size of the virtual disk. /// If this is to be an asynchronous operation, the address of a valid OVERLAPPED structure. /// /// If the function succeeds, the return value is ERROR_SUCCESS (0) and the Handle parameter contains a valid pointer to the new /// virtual disk object. /// /// If the function fails, the return value is an error code and the value of the Handle parameter is undefined. For more /// information, see System Error Codes. /// /// [PInvokeData("VirtDisk.h")] [DllImport(Lib.VirtDisk, ExactSpelling = true)] public static extern Win32Error ResizeVirtualDisk([In] VIRTUAL_DISK_HANDLE VirtualDiskHandle, RESIZE_VIRTUAL_DISK_FLAG Flags, in RESIZE_VIRTUAL_DISK_PARAMETERS Parameters, IntPtr Overlapped); /// Resizes a virtual disk. /// Handle to an open virtual disk. /// Zero or more flags enumerated from the RESIZE_VIRTUAL_DISK_FLAG enumeration. /// Address of a RESIZE_VIRTUAL_DISK_PARAMETERS structure containing the new size of the virtual disk. /// If this is to be an asynchronous operation, the address of a valid OVERLAPPED structure. /// /// If the function succeeds, the return value is ERROR_SUCCESS (0) and the Handle parameter contains a valid pointer to the new /// virtual disk object. /// /// If the function fails, the return value is an error code and the value of the Handle parameter is undefined. For more /// information, see System Error Codes. /// /// [PInvokeData("VirtDisk.h")] [DllImport(Lib.VirtDisk, ExactSpelling = true)] public static extern Win32Error ResizeVirtualDisk([In] VIRTUAL_DISK_HANDLE VirtualDiskHandle, RESIZE_VIRTUAL_DISK_FLAG Flags, in RESIZE_VIRTUAL_DISK_PARAMETERS Parameters, in NativeOverlapped Overlapped); /// Sets information about a virtual hard disk (VHD). /// /// A handle to the open VHD, which must have been opened using the VIRTUAL_DISK_ACCESS_METAOPS flag. /// /// A pointer to a valid SET_VIRTUAL_DISK_INFO structure. /// /// If the function succeeds, the return value is ERROR_SUCCESS and the Handle parameter contains a valid pointer to the new virtual /// disk object. If the function fails, the return value is an error code and the value of the Handle parameter is undefined. /// [PInvokeData("VirtDisk.h")] [DllImport(Lib.VirtDisk, ExactSpelling = true)] public static extern Win32Error SetVirtualDiskInformation([In, Out] VIRTUAL_DISK_HANDLE VirtualDiskHandle, in SET_VIRTUAL_DISK_INFO VirtualDiskInfo); /// Sets a metadata item for a virtual disk. /// Handle to an open virtual disk. /// A GUID identifying the metadata to retrieve. /// Address of a ULONG containing the size, in bytes, of the buffer pointed to by the MetaData parameter. /// Address of the buffer containing the metadata to be stored. /// /// Status of the request. If the function succeeds, the return value is ERROR_SUCCESS. If the function fails, the return value is an /// error code.For more information, see System Error Codes. /// [PInvokeData("VirtDisk.h", MinClient = PInvokeClient.Windows8)] [DllImport(Lib.VirtDisk, ExactSpelling = true)] public static extern Win32Error SetVirtualDiskMetadata([In, Out] VIRTUAL_DISK_HANDLE VirtualDiskHandle, in Guid Item, uint MetaDataSize, IntPtr MetaData); /// Creates a snapshot of the current virtual disk for VHD Set files. /// A handle to the open virtual disk. This must be a VHD Set file. /// A pointer to a valid TAKE_SNAPSHOT_VHDSET_PARAMETERS structure that contains snapshot data. /// Snapshot flags, which must be a valid combination of the TAKE_SNAPSHOT_VHDSET_FLAG enumeration. /// /// Status of the request. If the function succeeds, the return value is ERROR_SUCCESS. If the function fails, the return value is an /// error code.For more information, see System Error Codes. /// [PInvokeData("VirtDisk.h", MinClient = PInvokeClient.Windows10)] [DllImport(Lib.VirtDisk, ExactSpelling = true)] public static extern Win32Error TakeSnapshotVhdSet([In] VIRTUAL_DISK_HANDLE VirtualDiskHandle, in TAKE_SNAPSHOT_VHDSET_PARAMETERS Parameters, TAKE_SNAPSHOT_VHDSET_FLAG Flags); /// Contains snapshot parameters, indicating information about the new snapshot to be applied. [PInvokeData("VirtDisk.h", MinClient = PInvokeClient.Windows10)] [StructLayout(LayoutKind.Sequential)] public struct APPLY_SNAPSHOT_VHDSET_PARAMETERS { /// /// An APPLY_SNAPSHOT_VHDSET_VERSION enumeration that specifies the version of the APPLY_SNAPSHOT_VHDSET_PARAMETERS structure /// being passed to or from the VHD functions. /// public APPLY_SNAPSHOT_VHDSET_VERSION Version; /// A structure with the following member. public APPLY_SNAPSHOT_VHDSET_PARAMETERS_Version1 Version1; /// A structure with the following member. [PInvokeData("VirtDisk.h", MinClient = PInvokeClient.Windows10)] [StructLayout(LayoutKind.Sequential)] public struct APPLY_SNAPSHOT_VHDSET_PARAMETERS_Version1 { /// The ID of the new snapshot to be applied to the VHD set. public Guid SnapshotId; /// /// Indicates whether the current default leaf data should be retained as part of the apply operation. When a zero GUID is /// specified, the apply operation will discard the current default leaf data. When a non-zero GUID is specified, the apply /// operation will convert the default leaf data into a writeable snapshot with the specified ID. /// public Guid LeafSnapshotId; } } /// Contains virtual hard disk (VHD) attach request parameters. [PInvokeData("VirtDisk.h", MinClient = PInvokeClient.Windows7)] [StructLayout(LayoutKind.Sequential)] public struct ATTACH_VIRTUAL_DISK_PARAMETERS { /// /// A ATTACH_VIRTUAL_DISK_VERSION enumeration that specifies the version of the ATTACH_VIRTUAL_DISK_PARAMETERS structure being /// passed to or from the VHD functions. /// public ATTACH_VIRTUAL_DISK_VERSION Version; /// A structure with the following member. public ATTACH_VIRTUAL_DISK_PARAMETERS_Version1 Version1; /// Gets the default value for this structure. This is currently the only valid value for . public static ATTACH_VIRTUAL_DISK_PARAMETERS Default => new() { Version = ATTACH_VIRTUAL_DISK_VERSION.ATTACH_VIRTUAL_DISK_VERSION_1 }; /// A structure with the following member. [PInvokeData("VirtDisk.h", MinClient = PInvokeClient.Windows7)] [StructLayout(LayoutKind.Sequential)] public struct ATTACH_VIRTUAL_DISK_PARAMETERS_Version1 { /// Reserved. public uint Reserved; } } /// Contains virtual hard disk (VHD) compacting parameters. [PInvokeData("VirtDisk.h", MinClient = PInvokeClient.Windows7)] [StructLayout(LayoutKind.Sequential)] public struct COMPACT_VIRTUAL_DISK_PARAMETERS { /// /// A COMPACT_VIRTUAL_DISK_VERSION enumeration that specifies the version of the COMPACT_VIRTUAL_DISK_PARAMETERS structure being /// passed to or from the virtual hard disk (VHD) functions. /// public COMPACT_VIRTUAL_DISK_VERSION Version; /// A structure with the following member. public COMPACT_VIRTUAL_DISK_PARAMETERS_Version1 Version1; /// Gets the default value for this structure. This is currently the only valid value for . public static COMPACT_VIRTUAL_DISK_PARAMETERS Default => new() { Version = COMPACT_VIRTUAL_DISK_VERSION.COMPACT_VIRTUAL_DISK_VERSION_1 }; /// A structure with the following member. [PInvokeData("VirtDisk.h", MinClient = PInvokeClient.Windows7)] [StructLayout(LayoutKind.Sequential)] public struct COMPACT_VIRTUAL_DISK_PARAMETERS_Version1 { /// Reserved. Must be set to zero. public uint Reserved; } } /// /// Contains virtual disk creation parameters, providing control over, and information about, the newly created virtual disk. /// [PInvokeData("VirtDisk.h", MinClient = PInvokeClient.Windows7)] [StructLayout(LayoutKind.Explicit)] public struct CREATE_VIRTUAL_DISK_PARAMETERS { /// /// A CREATE_VIRTUAL_DISK_VERSION enumeration that specifies the version of the CREATE_VIRTUAL_DISK_PARAMETERS structure being /// passed to or from the virtual hard disk (VHD) functions. /// [FieldOffset(0)] public CREATE_VIRTUAL_DISK_VERSION Version; /// This structure is used if the Version member is CREATE_VIRTUAL_DISK_VERSION_1 (1). [FieldOffset(8)] public CREATE_VIRTUAL_DISK_PARAMETERS_Version1 Version1; /// This structure is used if the Version member is CREATE_VIRTUAL_DISK_VERSION_2 (2). [FieldOffset(8)] public CREATE_VIRTUAL_DISK_PARAMETERS_Version2 Version2; /// This structure is used if the Version member is CREATE_VIRTUAL_DISK_VERSION_3 (3). [FieldOffset(8)] public CREATE_VIRTUAL_DISK_PARAMETERS_Version3 Version3; /// Initializes a CREATE_VIRTUAL_DISK_PARAMETERS with a maximum size. /// /// The maximum virtual size of the virtual disk object. Must be a multiple of 512. If a ParentPath is specified, this value must /// be zero. If a SourcePath is specified, this value can be zero to specify the size of the source VHD to be used, otherwise the /// size specified must be greater than or equal to the size of the source disk. /// /// Set this number if you wish to force the version of this structure to something other than . /// /// Internal size of the virtual disk object blocks, in bytes. This must be set to one of the following values: 0 (default), /// 0x80000 (512K), or 0x200000 (2MB). /// /// /// Internal size of the virtual disk object sectors. For VHDX must be set to 512 (0x200) or 4096 (0x1000). For VHD 1 must be set /// to 512. /// public CREATE_VIRTUAL_DISK_PARAMETERS(ulong maxSize, uint version = 1, uint blockSize = 0, uint logicalSectorSize = 0) : this() { if (version < 1 || version > 3) throw new ArgumentOutOfRangeException(nameof(version)); Version = (CREATE_VIRTUAL_DISK_VERSION)version; Version1.MaximumSize = maxSize; Version1.BlockSizeInBytes = blockSize; Version1.SectorSizeInBytes = logicalSectorSize; } /// Initializes a CREATE_VIRTUAL_DISK_PARAMETERS with a maximum size. /// /// Optional path to a parent virtual disk object. Associates the new virtual disk with an existing virtual disk. If this /// parameter is not NULL, SourcePath must be NULL. /// /// /// Fully qualified path to pre-populate the new virtual disk object with block data from an existing disk. This path may refer /// to a virtual disk or a physical disk. If this parameter is not NULL, SourcePath must be NULL. /// public CREATE_VIRTUAL_DISK_PARAMETERS(IntPtr pParentPath, IntPtr pSourcePath) : this() { Version = Environment.OSVersion.Version < new Version(6, 2) ? CREATE_VIRTUAL_DISK_VERSION.CREATE_VIRTUAL_DISK_VERSION_1 : CREATE_VIRTUAL_DISK_VERSION.CREATE_VIRTUAL_DISK_VERSION_2; if (Version == CREATE_VIRTUAL_DISK_VERSION.CREATE_VIRTUAL_DISK_VERSION_1) { Version1.ParentPath = pParentPath; Version1.SourcePath = pSourcePath; } else { Version2.ParentPath = pParentPath; Version2.SourcePath = pSourcePath; } } /// /// Contains virtual hard disk (VHD) creation parameters, providing control over, and information about, the newly created /// virtual disk. /// [PInvokeData("VirtDisk.h", MinClient = PInvokeClient.Windows7)] [StructLayout(LayoutKind.Sequential)] public struct CREATE_VIRTUAL_DISK_PARAMETERS_Version1 { /// /// Unique identifier to assign to the virtual disk object. If this member is set to zero, a unique identifier is created by the system. /// public Guid UniqueId; /// /// The maximum virtual size of the virtual disk object. Must be a multiple of 512. If a ParentPath is specified, this value must /// be zero. If a SourcePath is specified, this value can be zero to specify the size of the source VHD to be used, otherwise the /// size specified must be greater than or equal to the size of the source disk. /// public ulong MaximumSize; /// /// Internal size of the virtual disk object blocks. If value is 0, block size will be automatically matched to the parent or /// source disk's setting if ParentPath or SourcePath is specified (otherwise a block size of 2MB will be used). /// public uint BlockSizeInBytes; /// Internal size of the virtual disk object sectors. Must be set to 512. public uint SectorSizeInBytes; /// /// Optional path to a parent virtual disk object. Associates the new virtual disk with an existing virtual disk. If this /// parameter is not NULL, SourcePath must be NULL. /// public StrPtrUni ParentPath; /// /// Optional fully qualified path to pre-populate the new virtual disk object with block data from an existing disk. This path /// may refer to a virtual disk or a physical disk. If this parameter is not NULL, ParentPath must be NULL. /// public StrPtrUni SourcePath; } /// /// Contains virtual hard disk (VHD) creation parameters, providing control over, and information about, the newly created /// virtual disk. /// [PInvokeData("VirtDisk.h", MinClient = PInvokeClient.Windows8)] [StructLayout(LayoutKind.Sequential)] public struct CREATE_VIRTUAL_DISK_PARAMETERS_Version2 { /// /// Unique identifier to assign to the virtual disk object. If this member is set to zero, a unique identifier is created by the system. /// public Guid UniqueId; /// /// The maximum virtual size of the virtual disk object. Must be a multiple of 512. If a ParentPath is specified, this value must /// be zero. If a SourcePath is specified, this value can be zero to specify the size of the source VHD to be used, otherwise the /// size specified must be greater than or equal to the size of the source disk. /// public ulong MaximumSize; /// /// Internal size of the virtual disk object blocks. If value is 0, block size will be automatically matched to the parent or /// source disk's setting if ParentPath or SourcePath is specified (otherwise a block size of 2MB will be used). /// public uint BlockSizeInBytes; /// Internal size of the virtual disk object sectors. Must be set to 512. public uint SectorSizeInBytes; /// Size of the physical disk object sectors. public uint PhysicalSectorSizeInBytes; /// /// Optional path to a parent virtual disk object. Associates the new virtual disk with an existing virtual disk. If this /// parameter is not NULL, SourcePath must be NULL. /// public StrPtrUni ParentPath; /// /// Optional fully qualified path to pre-populate the new virtual disk object with block data from an existing disk. This path /// may refer to a virtual disk or a physical disk. If this parameter is not NULL, ParentPath must be NULL. /// public StrPtrUni SourcePath; /// Zero or more flags from the OPEN_VIRTUAL_DISK_FLAG enumeration describing how the virtual disk is to be opened. public OPEN_VIRTUAL_DISK_FLAG OpenFlags; /// A VIRTUAL_STORAGE_TYPE structure describing the parent virtual disk specified in the ParentPath member. public VIRTUAL_STORAGE_TYPE ParentVirtualStorageType; /// A VIRTUAL_STORAGE_TYPE structure describing the source virtual disk specified in the SourcePath member. public VIRTUAL_STORAGE_TYPE SourceVirtualStorageType; /// Resiliency GUID for the file. public Guid ResiliencyGuid; } /// /// Contains virtual hard disk (VHD) creation parameters, providing control over, and information about, the newly created /// virtual disk. /// [PInvokeData("VirtDisk.h", MinClient = PInvokeClient.Windows10)] [StructLayout(LayoutKind.Sequential)] public struct CREATE_VIRTUAL_DISK_PARAMETERS_Version3 { /// /// Unique identifier to assign to the virtual disk object. If this member is set to zero, a unique identifier is created by the system. /// public Guid UniqueId; /// /// The maximum virtual size of the virtual disk object. Must be a multiple of 512. If a ParentPath is specified, this value must /// be zero. If a SourcePath is specified, this value can be zero to specify the size of the source VHD to be used, otherwise the /// size specified must be greater than or equal to the size of the source disk. /// public ulong MaximumSize; /// /// Internal size of the virtual disk object blocks. If value is 0, block size will be automatically matched to the parent or /// source disk's setting if ParentPath or SourcePath is specified (otherwise a block size of 2MB will be used). /// public uint BlockSizeInBytes; /// Internal size of the virtual disk object sectors. Must be set to 512. public uint SectorSizeInBytes; /// Size of the physical disk object sectors. public uint PhysicalSectorSizeInBytes; /// /// Optional path to a parent virtual disk object. Associates the new virtual disk with an existing virtual disk. If this /// parameter is not NULL, SourcePath must be NULL. /// public StrPtrUni ParentPath; /// /// Optional fully qualified path to pre-populate the new virtual disk object with block data from an existing disk. This path /// may refer to a virtual disk or a physical disk. If this parameter is not NULL, ParentPath must be NULL. /// public StrPtrUni SourcePath; /// Zero or more flags from the OPEN_VIRTUAL_DISK_FLAG enumeration describing how the virtual disk is to be opened. public OPEN_VIRTUAL_DISK_FLAG OpenFlags; /// A VIRTUAL_STORAGE_TYPE structure describing the parent virtual disk specified in the ParentPath member. public VIRTUAL_STORAGE_TYPE ParentVirtualStorageType; /// A VIRTUAL_STORAGE_TYPE structure describing the source virtual disk specified in the SourcePath member. public VIRTUAL_STORAGE_TYPE SourceVirtualStorageType; /// Resiliency GUID for the file. public Guid ResiliencyGuid; /// public StrPtrUni SourceLimitPath; /// public VIRTUAL_STORAGE_TYPE BackingStorageType; } } /// Contains snapshot deletion parameters, designating which snapshot to delete from the VHD Set. [PInvokeData("VirtDisk.h", MinClient = PInvokeClient.Windows10)] [StructLayout(LayoutKind.Sequential)] public struct DELETE_SNAPSHOT_VHDSET_PARAMETERS { /// A value from the DELETE_SNAPSHOT_VHDSET_VERSION enumeration that is the discriminant for the union. public DELETE_SNAPSHOT_VHDSET_VERSION Version; /// A structure with the following member. public DELETE_SNAPSHOT_VHDSET_PARAMETERS_Version1 Version1; /// A structure with the following member. [PInvokeData("VirtDisk.h", MinClient = PInvokeClient.Windows10)] [StructLayout(LayoutKind.Sequential)] public struct DELETE_SNAPSHOT_VHDSET_PARAMETERS_Version1 { /// The Snapshot Id in GUID format indicating which snapshot is to be deleted from the VHD Set. public Guid SnapshotId; } } /// Contains virtual disk expansion request parameters. [PInvokeData("VirtDisk.h", MinClient = PInvokeClient.Windows7)] [StructLayout(LayoutKind.Sequential)] public struct EXPAND_VIRTUAL_DISK_PARAMETERS { /// /// An EXPAND_VIRTUAL_DISK_VERSION enumeration that specifies the version of the EXPAND_VIRTUAL_DISK_PARAMETERS structure being /// passed to or from the virtual hard disk (VHD) functions. /// public EXPAND_VIRTUAL_DISK_VERSION Version; /// New size, in bytes, for the expansion request. public EXPAND_VIRTUAL_DISK_PARAMETERS_Version1 Version1; /// Initializes with default version and . /// New size, in bytes, for the expansion request. public EXPAND_VIRTUAL_DISK_PARAMETERS(ulong newSize) { Version = EXPAND_VIRTUAL_DISK_VERSION.EXPAND_VIRTUAL_DISK_VERSION_1; Version1.NewSize = newSize; } /// New size, in bytes, for the expansion request. [PInvokeData("VirtDisk.h", MinClient = PInvokeClient.Windows7)] [StructLayout(LayoutKind.Sequential)] public struct EXPAND_VIRTUAL_DISK_PARAMETERS_Version1 { /// New size, in bytes, for the expansion request. public ulong NewSize; } } /// Contains virtual hard disk (VHD) information. [PInvokeData("VirtDisk.h", MinClient = PInvokeClient.Windows7)] [StructLayout(LayoutKind.Explicit, CharSet = CharSet.Unicode)] public struct GET_VIRTUAL_DISK_INFO { /// /// A GET_VIRTUAL_DISK_INFO_VERSION enumeration that specifies the version of the GET_VIRTUAL_DISK_INFO structure being passed to /// or from the VHD functions. This determines what parts of this structure will be used. /// [FieldOffset(0)] public GET_VIRTUAL_DISK_INFO_VERSION Version; /// [FieldOffset(8), PInvokeData("VirtDisk.h", MinClient = PInvokeClient.Windows7)] public GET_VIRTUAL_DISK_INFO_Size Size; /// Unique identifier of the VHD. [FieldOffset(8), PInvokeData("VirtDisk.h", MinClient = PInvokeClient.Windows7)] public Guid Identifier; /// A structure with the following members [FieldOffset(8), PInvokeData("VirtDisk.h", MinClient = PInvokeClient.Windows7)] public GET_VIRTUAL_DISK_INFO_ParentLocation ParentLocation; /// Unique identifier of the parent disk backing store. [FieldOffset(8), PInvokeData("VirtDisk.h", MinClient = PInvokeClient.Windows7)] public Guid ParentIdentifier; /// Internal time stamp of the parent disk backing store. [FieldOffset(8), PInvokeData("VirtDisk.h", MinClient = PInvokeClient.Windows7)] public uint ParentTimestamp; /// VIRTUAL_STORAGE_TYPE structure containing information about the type of VHD. [FieldOffset(8), PInvokeData("VirtDisk.h", MinClient = PInvokeClient.Windows7)] public VIRTUAL_STORAGE_TYPE VirtualStorageType; /// Provider-specific subtype. [FieldOffset(8), PInvokeData("VirtDisk.h", MinClient = PInvokeClient.Windows7)] public VIRTUAL_DISK_INFO_PROVIDER_SUBTYPE ProviderSubtype; /// Indicates whether the virtual disk is 4 KB aligned. [FieldOffset(8), MarshalAs(UnmanagedType.Bool), PInvokeData("VirtDisk.h", MinClient = PInvokeClient.Windows8)] public bool Is4kAligned; /// /// Indicates whether the virtual disk is currently mounted and in use. TRUE if the virtual disk is currently mounted and in use; /// otherwise FALSE. /// [FieldOffset(8), MarshalAs(UnmanagedType.Bool), PInvokeData("VirtDisk.h", MinClient = PInvokeClient.Windows81)] public bool IsLoaded; /// Details about the physical disk on which the virtual disk resides. [FieldOffset(8), PInvokeData("VirtDisk.h", MinClient = PInvokeClient.Windows8)] public GET_VIRTUAL_DISK_INFO_PhysicalDisk PhysicalDisk; /// The physical sector size of the virtual disk. [FieldOffset(8), PInvokeData("VirtDisk.h", MinClient = PInvokeClient.Windows8)] public uint VhdPhysicalSectorSize; /// The smallest safe minimum size of the virtual disk. [FieldOffset(8), PInvokeData("VirtDisk.h", MinClient = PInvokeClient.Windows8)] public ulong SmallestSafeVirtualSize; /// The fragmentation level of the virtual disk. [FieldOffset(8), PInvokeData("VirtDisk.h", MinClient = PInvokeClient.Windows8)] public uint FragmentationPercentage; /// /// The identifier that is uniquely created when a user first creates the virtual disk to attempt to uniquely identify that /// virtual disk. /// [FieldOffset(8), PInvokeData("VirtDisk.h", MinClient = PInvokeClient.Windows81)] public Guid VirtualDiskId; /// The state of resilient change tracking (RCT) for the virtual disk. [FieldOffset(8), PInvokeData("VirtDisk.h", MinClient = PInvokeClient.Windows10)] public GET_VIRTUAL_DISK_INFO_ChangeTrackingState ChangeTrackingState; /// /// The state of resilient change tracking (RCT) for the virtual disk. While this structure will fill, the value /// of MostRecentId will the be first character of a truncated string. /// [PInvokeData("VirtDisk.h", MinClient = PInvokeClient.Windows10)] [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] public struct GET_VIRTUAL_DISK_INFO_ChangeTrackingState { /// Whether RCT is turned on. TRUE if RCT is turned on; otherwise FALSE. [MarshalAs(UnmanagedType.Bool)] public bool Enabled; /// /// Whether the virtual disk has changed since the change identified by the MostRecentId member occurred. TRUE if the virtual /// disk has changed since the change identified by the MostRecentId member occurred; otherwise FALSE. /// [MarshalAs(UnmanagedType.Bool)] public bool NewerChanges; /// /// The change tracking identifier for the change that identifies the state of the virtual disk that you want to use as the basis /// of comparison to determine whether the NewerChanges member reports new changes. /// public IntPtr MostRecentId; } /// /// A structure with the following members While this structure will fill, the value of ParentLocationBuffer /// will the be first character of a truncated string. /// [PInvokeData("VirtDisk.h", MinClient = PInvokeClient.Windows7)] [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] public struct GET_VIRTUAL_DISK_INFO_ParentLocation { /// Parent resolution. TRUE if the parent backing store was successfully resolved, FALSE if not. [MarshalAs(UnmanagedType.Bool)] public bool ParentResolved; /// /// If the ParentResolved member is TRUE, contains the path of the parent backing store. If the ParentResolved member is FALSE, /// contains all of the parent paths present in the search list. /// public IntPtr ParentLocationBuffer; } /// Details about the physical disk on which the virtual disk resides. [PInvokeData("VirtDisk.h", MinClient = PInvokeClient.Windows8)] [StructLayout(LayoutKind.Sequential, Pack = 4)] public struct GET_VIRTUAL_DISK_INFO_PhysicalDisk { /// The logical sector size of the physical disk. public uint LogicalSectorSize; /// The physical sector size of the physical disk. public uint PhysicalSectorSize; /// Indicates whether the physical disk is remote. [MarshalAs(UnmanagedType.Bool)] public bool IsRemote; } /// Sizes of the virtual disk. [PInvokeData("VirtDisk.h", MinClient = PInvokeClient.Windows7)] [StructLayout(LayoutKind.Sequential, Pack = 4)] public struct GET_VIRTUAL_DISK_INFO_Size { /// Virtual size of the VHD, in bytes. public ulong VirtualSize; /// Physical size of the VHD on disk, in bytes. public ulong PhysicalSize; /// Block size of the VHD, in bytes. public uint BlockSize; /// Sector size of the VHD, in bytes. public uint SectorSize; } } /// Contains virtual disk merge request parameters. [PInvokeData("VirtDisk.h", MinClient = PInvokeClient.Windows7)] [StructLayout(LayoutKind.Explicit)] public struct MERGE_VIRTUAL_DISK_PARAMETERS { /// /// A MERGE_VIRTUAL_DISK_VERSION enumeration that specifies the version of the MERGE_VIRTUAL_DISK_PARAMETERS structure being /// passed to or from the VHD functions. /// [FieldOffset(0)] public MERGE_VIRTUAL_DISK_VERSION Version; /// This structure is used when the Version member is MERGE_VIRTUAL_DISK_VERSION_1 (1). [FieldOffset(4)] public MERGE_VIRTUAL_DISK_PARAMETERS_V1 Version1; /// This structure is used when the Version member is MERGE_VIRTUAL_DISK_VERSION_2 (2). [FieldOffset(4)] public MERGE_VIRTUAL_DISK_PARAMETERS_V2 Version2; /// /// Initializes a new instance of the struct. /// /// The merge depth. public MERGE_VIRTUAL_DISK_PARAMETERS(uint mergeDepth) : this() { Version = MERGE_VIRTUAL_DISK_VERSION.MERGE_VIRTUAL_DISK_VERSION_1; Version1.MergeDepth = mergeDepth; } /// /// Initializes a new instance of the struct. /// /// The merge source depth. /// The merge target depth. public MERGE_VIRTUAL_DISK_PARAMETERS(uint mergeSourceDepth, uint mergeTargetDepth) : this() { Version = MERGE_VIRTUAL_DISK_VERSION.MERGE_VIRTUAL_DISK_VERSION_2; Version2.MergeSourceDepth = mergeSourceDepth; Version2.MergeTargetDepth = mergeTargetDepth; } /// This structure is used when the Version member is MERGE_VIRTUAL_DISK_VERSION_1 (1). [PInvokeData("VirtDisk.h", MinClient = PInvokeClient.Windows7)] [StructLayout(LayoutKind.Sequential)] public struct MERGE_VIRTUAL_DISK_PARAMETERS_V1 { /// /// Depth of the merge request. This is the number of parent disks in the differencing chain to merge together. The RWDepth of the virtual disk must be greater than MergeDepth. For more information, see OPEN_VIRTUAL_DISK_PARAMETERS. /// public uint MergeDepth; } /// This structure is used when the Version member is MERGE_VIRTUAL_DISK_VERSION_2 (2). [PInvokeData("VirtDisk.h", MinClient = PInvokeClient.Windows8)] [StructLayout(LayoutKind.Sequential)] public struct MERGE_VIRTUAL_DISK_PARAMETERS_V2 { /// Depth from the leaf from which to begin the merge. The leaf is at depth 1. public uint MergeSourceDepth; /// Depth from the leaf to target the merge. The leaf is at depth 1. public uint MergeTargetDepth; } } /// Contains virtual hard disk (VHD) mirror request parameters. [PInvokeData("VirtDisk.h", MSDNShortId = "hh448680", MinClient = PInvokeClient.Windows8)] [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] public struct MIRROR_VIRTUAL_DISK_PARAMETERS { /// Indicates the version of this structure to use. Set this to MIRROR_VIRTUAL_DISK_VERSION_1 (1). public MIRROR_VIRTUAL_DISK_VERSION Version; /// This structure is used if the Version member is set to MIRROR_VIRTUAL_DISK_VERSION_1. public MIRROR_VIRTUAL_DISK_PARAMETERS_Version1 Version1; /// This structure is used if the Version member is set to MIRROR_VIRTUAL_DISK_VERSION_1. [PInvokeData("VirtDisk.h", MSDNShortId = "hh448680", MinClient = PInvokeClient.Windows8)] [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] public struct MIRROR_VIRTUAL_DISK_PARAMETERS_Version1 { /// /// Fully qualified path where the mirrored virtual disk will be located. If the Flags parameter to MirrorVirtualDisk is /// MIRROR_VIRTUAL_DISK_FLAG_NONE (0) then this file must not exist. If the Flags parameter to MirrorVirtualDisk is /// MIRROR_VIRTUAL_DISK_FLAG_EXISTING_FILE (1) then this file must exist. /// public IntPtr MirrorVirtualDiskPath; } } /// Contains VHD Set modification parameters, indicating how the VHD Set should be altered. [PInvokeData("VirtDisk.h", MinClient = PInvokeClient.Windows10)] [StructLayout(LayoutKind.Sequential)] public struct MODIFY_VHDSET_PARAMETERS { /// A value from the MODIFY_VHDSET_VERSION enumeration that determines that is the discriminant for the union. public MODIFY_VHDSET_VERSION Version; /// A structure with the following members. public MODIFY_VHDSET_PARAMETERS_Version1 Version1; /// A structure with the following members. [PInvokeData("VirtDisk.h", MinClient = PInvokeClient.Windows10)] [StructLayout(LayoutKind.Explicit)] public struct MODIFY_VHDSET_PARAMETERS_Version1 { /// The snapshot path [FieldOffset(0)] public MODIFY_VHDSET_PARAMETERS_Version1_SnapshotPath SnapshotPath; /// The Snapshot Id in GUID format indicating which snapshot is to be removed from the VHD Set file. [FieldOffset(0)] public Guid SnapshotId; /// The file path for the default Snapshot of the Vhd Set. [FieldOffset(0)] public StrPtrUni DefaultFilePath; } /// A structure with the following members. [PInvokeData("VirtDisk.h", MinClient = PInvokeClient.Windows10)] [StructLayout(LayoutKind.Sequential)] public struct MODIFY_VHDSET_PARAMETERS_Version1_SnapshotPath { /// The Snapshot Id in GUID format indicating which snapshot is to have its path altered in the VHD Set. public Guid SnapshotId; /// The new file path for the Snapshot indicated by the SnapshotId field. public StrPtrUni SnapshotFilePath; } } /// Identifies an area on a virtual hard disk (VHD) that has changed as tracked by resilient change tracking (RCT). [PInvokeData("VirtDisk.h", MinClient = PInvokeClient.Windows10)] [StructLayout(LayoutKind.Sequential)] public struct QUERY_CHANGES_VIRTUAL_DISK_RANGE { /// /// The distance from the start of the virtual disk to the beginning of the area of the virtual disk that has changed, in bytes. /// public ulong ByteOffset; /// The length of the area of the virtual disk that has changed, in bytes. public ulong ByteLength; /// Reserved. public ulong Reserved; } /// Contains raw SCSI virtual disk request parameters. [PInvokeData("VirtDisk.h", MinClient = PInvokeClient.Windows10)] [StructLayout(LayoutKind.Sequential)] public struct RAW_SCSI_VIRTUAL_DISK_PARAMETERS { /// /// A RAW_SCSI_VIRTUAL_DISK_VERSION enumeration that specifies the version of the RAW_SCSI_VIRTUAL_DISK_PARAMETERS structure /// being passed to or from the VHD functions. /// public RAW_SCSI_VIRTUAL_DISK_VERSION Version; /// A structure with the following members. public RAW_SCSI_VIRTUAL_DISK_PARAMETERS_Version1 Version1; /// A structure with the following members. [PInvokeData("VirtDisk.h", MinClient = PInvokeClient.Windows10)] [StructLayout(LayoutKind.Sequential, Pack = 1)] public struct RAW_SCSI_VIRTUAL_DISK_PARAMETERS_Version1 { /// If TRUE, indicates the operation will be transported to the virtual disk using the RSVD protocol. [MarshalAs(UnmanagedType.Bool)] public bool RSVDHandle; /// If TRUE, indicates the SCSI command will read data from the DataBuffer. If FALSE, indicates data may be written. [MarshalAs(UnmanagedType.U1)] public bool DataIn; /// Length, in bytes, of the command descriptor block (CDB) contained in the CDB member. public byte CdbLength; /// Length, in bytes, of the sense buffer. public byte SenseInfoLength; /// Caller-supplied SRB_FLAGS-prefixed bit flag specifying the requested operation. Flags are defined in srb.h. public byte SrbFlags; /// Length, in bytes, of the buffer to be transferred. public uint DataTransferLength; /// A pointer to the SCSI data buffer. public IntPtr DataBuffer; /// A pointer to a buffer to receive SCSI sense info after completion of the command. public IntPtr SenseInfo; /// Caller-supplied CDB data. (The CDB structure is declared in scsi.h.) public IntPtr Cdb; } /// Initializes a new instance of the struct. /// /// If , indicates the SCSI command will read data from the DataBuffer. If , /// indicates data may be written. /// /// /// Caller-supplied SRB_FLAGS-prefixed bit flag specifying the requested operation. Flags are defined in srb.h. /// /// The SCSI data buffer. /// A buffer to receive SCSI sense info after completion of the command. /// Caller-supplied CDB data. (The CDB structure is declared in scsi.h.) /// /// If , indicates the operation will be transported to the virtual disk using the RSVD protocol. /// public RAW_SCSI_VIRTUAL_DISK_PARAMETERS(bool read, byte srbFlags, SafeAllocatedMemoryHandleBase scsiData, SafeAllocatedMemoryHandleBase senseInfo, SafeAllocatedMemoryHandleBase cbdData, bool useRSVD = false) { Version = RAW_SCSI_VIRTUAL_DISK_VERSION.RAW_SCSI_VIRTUAL_DISK_VERSION_1; Version1 = new() { RSVDHandle = useRSVD, DataIn = read, CdbLength = (byte)(uint)cbdData.Size, SenseInfoLength = (byte)(uint)senseInfo.Size, SrbFlags = srbFlags, DataTransferLength = scsiData.Size, DataBuffer = scsiData, SenseInfo = senseInfo, Cdb = cbdData }; } } /// Contains raw SCSI virtual disk response parameters. [PInvokeData("VirtDisk.h", MinClient = PInvokeClient.Windows10)] [StructLayout(LayoutKind.Sequential)] public struct RAW_SCSI_VIRTUAL_DISK_RESPONSE { /// /// A RAW_SCSI_VIRTUAL_DISK_VERSION enumeration that specifies the version of the RAW_SCSI_VIRTUAL_DISK_PARAMETERS structure /// being passed to or from the VHD functions. /// public RAW_SCSI_VIRTUAL_DISK_VERSION Version; /// A structure with the following member. public RAW_SCSI_VIRTUAL_DISK_RESPONSE_Version1 Version1; /// A structure with the following member. [PInvokeData("VirtDisk.h", MinClient = PInvokeClient.Windows10)] [StructLayout(LayoutKind.Sequential, Pack = 1)] public struct RAW_SCSI_VIRTUAL_DISK_RESPONSE_Version1 { /// A SRB_STATUS-prefixed status value (defined in srb.h). public byte ScsiStatus; /// A SRB_STATUS-prefixed status value (defined in srb.h). public byte SenseInfoLength; /// Length, in bytes, of the buffer to be transferred. public uint DataTransferLength; } } /// Contains the parameters for a ResizeVirtualDisk function. [PInvokeData("VirtDisk.h", MinClient = PInvokeClient.Windows8)] [StructLayout(LayoutKind.Sequential)] public struct RESIZE_VIRTUAL_DISK_PARAMETERS { /// Discriminant for the union containing a value enumerated from the RESIZE_VIRTUAL_DISK_VERSION enumeration. public RESIZE_VIRTUAL_DISK_VERSION Version; /// If the Version member is RESIZE_VIRTUAL_DISK_VERSION_1 (1), this structure is used. public RESIZE_VIRTUAL_DISK_PARAMETERS_Version1 Version1; /// Initializes with default version and . /// Contains the new size of the virtual disk. public RESIZE_VIRTUAL_DISK_PARAMETERS(ulong newSize) { Version = RESIZE_VIRTUAL_DISK_VERSION.RESIZE_VIRTUAL_DISK_VERSION_1; Version1.NewSize = newSize; } /// If the Version member is RESIZE_VIRTUAL_DISK_VERSION_1 (1), this structure is used. [PInvokeData("VirtDisk.h", MinClient = PInvokeClient.Windows8)] [StructLayout(LayoutKind.Sequential)] public struct RESIZE_VIRTUAL_DISK_PARAMETERS_Version1 { /// Contains the new size of the virtual disk. public ulong NewSize; } } /// /// Contains virtual hard disk (VHD) information to use when you call the SetVirtualDiskInformation function to set VHD properties. /// // https://docs.microsoft.com/en-us/windows/win32/api/virtdisk/ns-virtdisk-set_virtual_disk_info typedef struct // _SET_VIRTUAL_DISK_INFO { SET_VIRTUAL_DISK_INFO_VERSION Version; union { PCWSTR ParentFilePath; GUID UniqueIdentifier; struct { // ULONG ChildDepth; PCWSTR ParentFilePath; } ParentPathWithDepthInfo; ULONG VhdPhysicalSectorSize; GUID VirtualDiskId; BOOL // ChangeTrackingEnabled; struct { GUID LinkageId; PCWSTR ParentFilePath; } ParentLocator; }; } SET_VIRTUAL_DISK_INFO, *PSET_VIRTUAL_DISK_INFO; [PInvokeData("virtdisk.h", MSDNShortId = "NS:virtdisk._SET_VIRTUAL_DISK_INFO")] [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode, #if X64 Pack = 8 #else Pack = 4 #endif )] public struct SET_VIRTUAL_DISK_INFO { /// /// A SET_VIRTUAL_DISK_INFO_VERSION enumeration that specifies the version of the SET_VIRTUAL_DISK_INFO structure being passed to /// or from the VHD functions. This determines the type of information set. /// public SET_VIRTUAL_DISK_INFO_VERSION Version; private UNION union; /// Initializes a new instance of the struct. /// The version that determines the type of information set.. public SET_VIRTUAL_DISK_INFO(SET_VIRTUAL_DISK_INFO_VERSION version) { Version = version; union = default; } /// Path to the parent backing store. [PInvokeData("VirtDisk.h", MinClient = PInvokeClient.Windows7)] public StrPtrUni ParentFilePath { get => union.ParentFilePath; set => union.ParentFilePath = value; } /// Unique identifier of the VHD. [PInvokeData("VirtDisk.h", MinClient = PInvokeClient.Windows7)] public Guid UniqueIdentifier { get => union.UniqueIdentifier; set => union.UniqueIdentifier = value; } /// Sets the parent file path and the child depth. [PInvokeData("VirtDisk.h", MinClient = PInvokeClient.Windows8)] public SET_VIRTUAL_DISK_INFO_ParentPathWithDepthInfo ParentPathWithDepthInfo { get => union.ParentPathWithDepthInfo; set => union.ParentPathWithDepthInfo = value; } /// Sets the physical sector size reported by the VHD. [PInvokeData("VirtDisk.h", MinClient = PInvokeClient.Windows8)] public uint VhdPhysicalSectorSize { get => union.VhdPhysicalSectorSize; set => union.VhdPhysicalSectorSize = value; } /// /// The identifier that is uniquely created when a user first creates the virtual disk to attempt to uniquely identify that /// virtual disk. /// [PInvokeData("VirtDisk.h", MinClient = PInvokeClient.Windows81)] public Guid VirtualDiskId { get => union.VirtualDiskId; set => union.VirtualDiskId = value; } /// Turns resilient change tracking (RCT) on or off for the VHD. TRUE turns RCT on. FALSE turns RCT off. [PInvokeData("VirtDisk.h", MinClient = PInvokeClient.Windows10)] public bool ChangeTrackingEnabled { get => union.ChangeTrackingEnabled; set => union.ChangeTrackingEnabled = value; } /// /// Sets the parent linkage information that differencing VHDs store. Parent linkage information is metadata used to locate and /// correctly identify the next parent in the virtual disk chain. /// [PInvokeData("VirtDisk.h", MinClient = PInvokeClient.Windows10)] public SET_VIRTUAL_DISK_INFO_ParentLocator ParentLocator { get => union.ParentLocator; set => union.ParentLocator = value; } [StructLayout(LayoutKind.Explicit, CharSet = CharSet.Unicode)] private struct UNION { [FieldOffset(0)] public StrPtrUni ParentFilePath; [FieldOffset(0)] public Guid UniqueIdentifier; [FieldOffset(0)] public SET_VIRTUAL_DISK_INFO_ParentPathWithDepthInfo ParentPathWithDepthInfo; [FieldOffset(0)] public uint VhdPhysicalSectorSize; [FieldOffset(0)] public Guid VirtualDiskId; [FieldOffset(0), MarshalAs(UnmanagedType.Bool)] public bool ChangeTrackingEnabled; [FieldOffset(0)] public SET_VIRTUAL_DISK_INFO_ParentLocator ParentLocator; } /// /// Sets the parent linkage information that differencing VHDs store. Parent linkage information is metadata used to locate and /// correctly identify the next parent in the virtual disk chain. /// [PInvokeData("VirtDisk.h", MSDNShortId = "dd323686", MinClient = PInvokeClient.Windows10)] [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] public struct SET_VIRTUAL_DISK_INFO_ParentLocator { /// The unique identifier for the parent linkage information. public Guid LinkageId; /// The path of the file for the parent VHD. public StrPtrUni ParentFilePath; } /// Sets the parent file path and the child depth. [PInvokeData("VirtDisk.h", MSDNShortId = "dd323686", MinClient = PInvokeClient.Windows8)] [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] public struct SET_VIRTUAL_DISK_INFO_ParentPathWithDepthInfo { /// Specifies the depth to the child from the leaf. The leaf itself is at depth 1. public uint ChildDepth; /// Specifies the depth to the parent from the leaf. The leaf itself is at depth 1. public StrPtrUni ParentFilePath; } } /// Contains storage dependency information. [PInvokeData("VirtDisk.h", MinClient = PInvokeClient.Windows7)] [StructLayout(LayoutKind.Sequential)] public struct STORAGE_DEPENDENCY_INFO { /// /// A STORAGE_DEPENDENCY_INFO_VERSION enumeration that specifies the version of the information structure being passed to or from /// the VHD functions. Can be STORAGE_DEPENDENCY_INFO_TYPE_1 or STORAGE_DEPENDENCY_INFO_TYPE_2. /// public STORAGE_DEPENDENCY_INFO_VERSION Version; /// Number of entries returned in the following unioned members. public int NumberEntries; /// Variable-length array containing STORAGE_DEPENDENCY_INFO_TYPE_1 or STORAGE_DEPENDENCY_INFO_TYPE_2 structures. public Array Entries { get { unsafe { fixed (void* first = &DependencyTypeFlags) { if (Version == STORAGE_DEPENDENCY_INFO_VERSION.STORAGE_DEPENDENCY_INFO_VERSION_1) { return NumberEntries == 0 ? new STORAGE_DEPENDENCY_INFO_TYPE_1[0] : ((IntPtr)first).ToArray(NumberEntries); } else if (Version == STORAGE_DEPENDENCY_INFO_VERSION.STORAGE_DEPENDENCY_INFO_VERSION_2) { return NumberEntries == 0 ? new STORAGE_DEPENDENCY_INFO_TYPE_2[0] : ((IntPtr)first).ToArray(NumberEntries); } } throw new NotSupportedException(); } } } DEPENDENT_DISK_FLAG DependencyTypeFlags; uint ProviderSpecificFlags; VIRTUAL_STORAGE_TYPE VirtualStorageType; uint AncestorLevel; IntPtr DependencyDeviceName; IntPtr HostVolumeName; IntPtr DependentVolumeName; IntPtr DependentVolumeRelativePath; } /// Contains storage dependency information for type 1. [PInvokeData("VirtDisk.h", MinClient = PInvokeClient.Windows7)] [StructLayout(LayoutKind.Sequential)] public struct STORAGE_DEPENDENCY_INFO_TYPE_1 { /// A DEPENDENT_DISK_FLAG enumeration. public DEPENDENT_DISK_FLAG DependencyTypeFlags; /// Flags specific to the VHD provider. public uint ProviderSpecificFlags; /// A VIRTUAL_STORAGE_TYPE structure. public VIRTUAL_STORAGE_TYPE VirtualStorageType; } /// Contains storage dependency information for type 2. [PInvokeData("VirtDisk.h", MinClient = PInvokeClient.Windows7)] [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] public struct STORAGE_DEPENDENCY_INFO_TYPE_2 { /// A DEPENDENT_DISK_FLAG enumeration. public DEPENDENT_DISK_FLAG DependencyTypeFlags; /// Flags specific to the VHD provider. public uint ProviderSpecificFlags; /// A VIRTUAL_STORAGE_TYPE structure. public VIRTUAL_STORAGE_TYPE VirtualStorageType; /// The ancestor level. public uint AncestorLevel; /// The device name of the dependent device. [MarshalAs(UnmanagedType.LPWStr)] public string DependencyDeviceName; /// The host disk volume name. [MarshalAs(UnmanagedType.LPWStr)] public string HostVolumeName; /// The name of the dependent volume, if any. [MarshalAs(UnmanagedType.LPWStr)] public string DependentVolumeName; /// The relative path to the dependent volume. [MarshalAs(UnmanagedType.LPWStr)] public string DependentVolumeRelativePath; } /// Contains snapshot parameters, indicating information about the new snapshot to be created. [PInvokeData("VirtDisk.h", MinClient = PInvokeClient.Windows10)] [StructLayout(LayoutKind.Sequential)] public struct TAKE_SNAPSHOT_VHDSET_PARAMETERS { /// A value from the TAKE_SNAPSHOT_VHDSET_VERSION enumeration that is the discriminant for the union. public TAKE_SNAPSHOT_VHDSET_VERSION Version; /// A structure with the following member. public TAKE_SNAPSHOT_VHDSET_PARAMETERS_Version1 Version1; /// A structure with the following member. [PInvokeData("VirtDisk.h", MinClient = PInvokeClient.Windows10)] [StructLayout(LayoutKind.Sequential)] public struct TAKE_SNAPSHOT_VHDSET_PARAMETERS_Version1 { /// The Id of the new Snapshot to be added to the Vhd Set. public Guid SnapshotId; } } /// Provides a handle to a virtual disk. [StructLayout(LayoutKind.Sequential)] public struct VIRTUAL_DISK_HANDLE : IHandle { private readonly IntPtr handle; /// Initializes a new instance of the struct. /// An object that represents the pre-existing handle to use. public VIRTUAL_DISK_HANDLE(IntPtr preexistingHandle) => handle = preexistingHandle; /// Returns an invalid handle by instantiating a object with . public static VIRTUAL_DISK_HANDLE NULL => new(IntPtr.Zero); /// Gets a value indicating whether this instance is a null handle. public bool IsNull => handle == IntPtr.Zero; /// Performs an explicit conversion from to . /// The handle. /// The result of the conversion. public static explicit operator IntPtr(VIRTUAL_DISK_HANDLE h) => h.handle; /// Performs an implicit conversion from to . /// The pointer to a handle. /// The result of the conversion. public static implicit operator VIRTUAL_DISK_HANDLE(IntPtr h) => new(h); /// Implements the operator !=. /// The first handle. /// The second handle. /// The result of the operator. public static bool operator !=(VIRTUAL_DISK_HANDLE h1, VIRTUAL_DISK_HANDLE h2) => !(h1 == h2); /// Implements the operator ==. /// The first handle. /// The second handle. /// The result of the operator. public static bool operator ==(VIRTUAL_DISK_HANDLE h1, VIRTUAL_DISK_HANDLE h2) => h1.Equals(h2); /// public override bool Equals(object obj) => obj is VIRTUAL_DISK_HANDLE h && handle == h.handle; /// public override int GetHashCode() => handle.GetHashCode(); /// public IntPtr DangerousGetHandle() => handle; } /// /// Contains the progress and result data for the current virtual disk operation, used by the GetVirtualDiskOperationProgress function. /// [PInvokeData("VirtDisk.h", MinClient = PInvokeClient.Windows7)] [StructLayout(LayoutKind.Sequential)] public struct VIRTUAL_DISK_PROGRESS { /// /// A system error code status value, this member will be ERROR_IO_PENDING if the operation is still in progress; otherwise, the /// value is the result code of the completed operation. /// public uint OperationStatus; /// /// The current progress of the operation, used in conjunction with the CompletionValue member. This value is meaningful only if /// OperationStatus is ERROR_IO_PENDING. /// public ulong CurrentValue; /// /// The value that the CurrentValue member would be if the operation were complete. This value is meaningful only if /// OperationStatus is ERROR_IO_PENDING. /// public ulong CompletionValue; } /// Device type identifier. [PInvokeData("VirtDisk.h", MinClient = PInvokeClient.Windows7)] [StructLayout(LayoutKind.Sequential)] public struct VIRTUAL_STORAGE_TYPE { /// The device identifier. public VIRTUAL_STORAGE_TYPE_DEVICE_TYPE DeviceId; /// Vendor-unique identifier. public Guid VendorId; /// Initializes a new instance of . /// The type of disk to create. /// true if is to be assigned VIRTUAL_STORAGE_TYPE_VENDOR_MICROSOFT. public VIRTUAL_STORAGE_TYPE(VIRTUAL_STORAGE_TYPE_DEVICE_TYPE type, bool vendorIsMicrosoft = true) { DeviceId = type; VendorId = vendorIsMicrosoft ? VIRTUAL_STORAGE_TYPE_VENDOR_MICROSOFT : Guid.Empty; } /// /// Gets an instance of that represents a Microsoft Virtual Hard Drive or .vhd file. /// public static VIRTUAL_STORAGE_TYPE VHD => new(VIRTUAL_STORAGE_TYPE_DEVICE_TYPE.VIRTUAL_STORAGE_TYPE_DEVICE_VHD); } /// Contains virtual disk open request parameters. [PInvokeData("VirtDisk.h", MinClient = PInvokeClient.Windows7)] [StructLayout(LayoutKind.Explicit)] public class OPEN_VIRTUAL_DISK_PARAMETERS { /// /// An OPEN_VIRTUAL_DISK_VERSION enumeration that specifies the version of the OPEN_VIRTUAL_DISK_PARAMETERS structure being /// passed to or from the VHD functions. /// [FieldOffset(0)] public OPEN_VIRTUAL_DISK_VERSION Version; /// This structure is used if the Version member is OPEN_VIRTUAL_DISK_VERSION_1 (1). [FieldOffset(8)] public OPEN_VIRTUAL_DISK_PARAMETERS_Version1 Version1; /// This structure is used if the Version member is OPEN_VIRTUAL_DISK_VERSION_2 (2). [FieldOffset(8)] public OPEN_VIRTUAL_DISK_PARAMETERS_Version2 Version2; /// This structure is used if the Version member is OPEN_VIRTUAL_DISK_VERSION_3 (3). [FieldOffset(8)] public OPEN_VIRTUAL_DISK_PARAMETERS_Version3 Version3; /// Initializes a new instance of the struct setting Version to OPEN_VIRTUAL_DISK_VERSION_1. /// /// /// Indicates the number of stores, beginning with the child, of the backing store chain to open as read/write. The remaining /// stores in the differencing chain will be opened read-only. This is necessary for merge operations to succeed. /// /// /// /// Value /// Meaning /// /// /// 0 /// Do not open for read/write at any depth. This value should be used for read-only operations. /// /// /// OPEN_VIRTUAL_DISK_RW_DEPTH_DEFAULT (1) /// Default value to use if no other value is desired. /// /// /// n (user-defined) /// This integer value should be the number of merge levels plus one, if a merge operation is intended. /// /// /// public OPEN_VIRTUAL_DISK_PARAMETERS(uint rwDepth) { Version = OPEN_VIRTUAL_DISK_VERSION.OPEN_VIRTUAL_DISK_VERSION_1; Version1.RWDepth = rwDepth; } /// /// Initializes a new instance of the struct setting Version to OPEN_VIRTUAL_DISK_VERSION_2. /// Only supported on Windows 8 and later. /// /// If TRUE, indicates the file backing store is to be opened as read-only. /// If TRUE, indicates the handle is only to be used to get information on the virtual disk. /// Resiliency GUID to specify when opening files. /// The snapshot identifier. Only supported on Windows 10 and later. public OPEN_VIRTUAL_DISK_PARAMETERS(bool readOnly, bool getInfoOnly = false, Guid resiliencyGuid = default, Guid snapshotId = default) { if (!PInvokeClient.Windows8.IsPlatformSupported()) throw new NotSupportedException(); if (snapshotId != Guid.Empty && !PInvokeClient.Windows10.IsPlatformSupported()) throw new NotSupportedException("Snapshots are only supported on Windows 10 and later."); Version = PInvokeClient.Windows10.IsPlatformSupported() ? OPEN_VIRTUAL_DISK_VERSION.OPEN_VIRTUAL_DISK_VERSION_3 : OPEN_VIRTUAL_DISK_VERSION.OPEN_VIRTUAL_DISK_VERSION_2; Version2.GetInfoOnly = getInfoOnly; Version2.ReadOnly = readOnly; Version2.ResiliencyGuid = resiliencyGuid; Version3.SnapshotId = snapshotId; } /// Gets the default value for this structure based on current OS version. public static OPEN_VIRTUAL_DISK_PARAMETERS Default => PInvokeClient.Windows8.IsPlatformSupported() ? new(false) : new(0); /// public override string ToString() { var v = (int)Version; return $"v{v}," + (v == 1 ? $"RWDepth={Version1.RWDepth}" : $"RO={Version2.ReadOnly},GetInfo={Version2.GetInfoOnly},RID={Version2.ResiliencyGuid}"); } /// This value is used if the Version member is OPEN_VIRTUAL_DISK_VERSION_1 (1). [PInvokeData("VirtDisk.h", MinClient = PInvokeClient.Windows7)] [StructLayout(LayoutKind.Sequential)] public struct OPEN_VIRTUAL_DISK_PARAMETERS_Version1 { /// /// Indicates the number of stores, beginning with the child, of the backing store chain to open as read/write. The remaining /// stores in the differencing chain will be opened read-only. This is necessary for merge operations to succeed. /// /// /// Value /// Meaning /// /// /// 0 /// Do not open for read/write at any depth. This value should be used for read-only operations. /// /// /// OPEN_VIRTUAL_DISK_RW_DEPTH_DEFAULT (1) /// Default value to use if no other value is desired. /// /// /// n (user-defined) /// This integer value should be the number of merge levels plus one, if a merge operation is intended. /// /// /// public uint RWDepth; } /// /// This value is used if the Version member is OPEN_VIRTUAL_DISK_VERSION_2 (2). /// Windows 7 and Windows Server 2008 R2: This structure is not supported until Windows 8 and Windows Server 2012. /// [PInvokeData("VirtDisk.h", MinClient = PInvokeClient.Windows8)] [StructLayout(LayoutKind.Sequential)] public struct OPEN_VIRTUAL_DISK_PARAMETERS_Version2 { /// If TRUE, indicates the handle is only to be used to get information on the virtual disk. [MarshalAs(UnmanagedType.Bool)] public bool GetInfoOnly; /// If TRUE, indicates the file backing store is to be opened as read-only. [MarshalAs(UnmanagedType.Bool)] public bool ReadOnly; /// Resiliency GUID to specify when opening files. public Guid ResiliencyGuid; } /// /// This value is used if the Version member is OPEN_VIRTUAL_DISK_VERSION_3 (3). /// Windows 7 and Windows Server 2008 R2: This structure is not supported until Windows 8 and Windows Server 2012. /// [PInvokeData("VirtDisk.h", MinClient = PInvokeClient.Windows10)] [StructLayout(LayoutKind.Sequential)] public struct OPEN_VIRTUAL_DISK_PARAMETERS_Version3 { /// If TRUE, indicates the handle is only to be used to get information on the virtual disk. [MarshalAs(UnmanagedType.Bool)] public bool GetInfoOnly; /// If TRUE, indicates the file backing store is to be opened as read-only. [MarshalAs(UnmanagedType.Bool)] public bool ReadOnly; /// Resiliency GUID to specify when opening files. public Guid ResiliencyGuid; /// public Guid SnapshotId; } } /// Provides a for that is disposed using . public class SafeVIRTUAL_DISK_HANDLE : SafeKernelHandle { /// Initializes a new instance of the class and assigns an existing handle. /// An object that represents the pre-existing handle to use. /// /// to reliably release the handle during the finalization phase; otherwise, (not recommended). /// public SafeVIRTUAL_DISK_HANDLE(IntPtr preexistingHandle, bool ownsHandle = true) : base(preexistingHandle, ownsHandle) { } /// Initializes a new instance of the class. private SafeVIRTUAL_DISK_HANDLE() : base() { } /// Performs an implicit conversion from to . /// The safe handle instance. /// The result of the conversion. public static implicit operator VIRTUAL_DISK_HANDLE(SafeVIRTUAL_DISK_HANDLE h) => h.handle; } } }