public static extern int AttachVirtualDisk( IntPtr VirtualDiskHandle, ref SECURITY_DESCRIPTOR SecurityDescriptor, ATTACH_VIRTUAL_DISK_FLAG Flags, int ProviderSpecificFlags, ref ATTACH_VIRTUAL_DISK_PARAMETERS Parameters, IntPtr Overlapped);
public static extern unsafe int AttachVirtualDisk( SafeFileHandle VirtualDiskHandle, IntPtr SecurityDescriptor, ATTACH_VIRTUAL_DISK_FLAG Flags, uint ProviderSpecificFlags, ref ATTACH_VIRTUAL_DISK_PARAMETERS Parameters, [In] NativeOverlapped *Overlapped);
/// <summary> /// Attaches a virtual hard disk (VHD) by locating an appropriate VHD provider to accomplish the attachment. /// </summary> /// <param name="flags"> /// A combination of values from the attachVirtualDiskFlags enumeration which will dictate how the behavior of the VHD once mounted. /// </param> public void Attach( ATTACH_VIRTUAL_DISK_FLAG flags = ATTACH_VIRTUAL_DISK_FLAG.ATTACH_VIRTUAL_DISK_FLAG_NONE, ATTACH_VIRTUAL_DISK_VERSION version = ATTACH_VIRTUAL_DISK_VERSION.ATTACH_VIRTUAL_DISK_VERSION_1) { if (IsAttached) { return; } // Get the current disk index. We need it later. int diskIndex = DiskIndex; var attachParameters = new NativeMethods.ATTACH_VIRTUAL_DISK_PARAMETERS(); // For attach, the correct version is always Version1 for Win7 and Win8. attachParameters.Version = version; attachParameters.Reserved = 0; if (!NativeMethods.InitializeSecurityDescriptor(out NativeMethods.SECURITY_DESCRIPTOR securityDescriptor, 1)) { throw new Win32Exception(Marshal.GetLastWin32Error()); } int ret = NativeMethods.AttachVirtualDisk(CheckDisposed(), ref securityDescriptor, flags, 0, ref attachParameters, IntPtr.Zero); if (ret != NativeMethods.ERROR_SUCCESS) { throw new Win32Exception(ret); } // There's apparently a bit of a timing issue here on some systems. // If the disk index isn't updated, keep checking once per second for five seconds. // If it's not updated after that, it's probably not our fault. var attempts = 5; while (attempts-- >= 0 && diskIndex == DiskIndex) { Thread.Sleep(1000); } }
public static extern Int32 AttachVirtualDisk(VirtualDiskSafeHandle VirtualDiskHandle, IntPtr SecurityDescriptor, ATTACH_VIRTUAL_DISK_FLAG Flags, Int32 ProviderSpecificFlags, ref ATTACH_VIRTUAL_DISK_PARAMETERS Parameters, IntPtr Overlapped);
internal static extern Int32 DetachVirtualDisk(IntPtr VirtualDiskHandle, ATTACH_VIRTUAL_DISK_FLAG Flags, Int32 ProviderSpecificFlags);
internal static extern Int32 AttachVirtualDisk(IntPtr VirtualDiskHandle, IntPtr SecurityDescriptor, ATTACH_VIRTUAL_DISK_FLAG Flags, Int32 ProviderSpecificFlags, ref ATTACH_VIRTUAL_DISK_PARAMETERS Parameters, IntPtr Overlapped);
public static extern uint AttachVirtualDisk(IntPtr Handle, IntPtr SecurityDescriptor, ATTACH_VIRTUAL_DISK_FLAG Flags, uint ProviderSpecificFlags, IntPtr Parameters, IntPtr Overlapped);