Open and Attach

This will be just short example of using Windows 7 Virtual disk API functions. Although I also gave full example, quite a few people asked me for shorter example which will just illustrate operations that will be used most often - open and attach.

Here it is:

string fileName = @"C:\test.vhd";

IntPtr handle = IntPtr.Zero;


// open disk handle
var openParameters = new OPEN_VIRTUAL_DISK_PARAMETERS();
openParameters.Version = OPEN_VIRTUAL_DISK_VERSION.OPEN_VIRTUAL_DISK_VERSION_1;
openParameters.Version1.RWDepth = OPEN_VIRTUAL_DISK_RW_DEPTH_DEFAULT;

var openStorageType = new VIRTUAL_STORAGE_TYPE();
openStorageType.DeviceId = VIRTUAL_STORAGE_TYPE_DEVICE_VHD;
openStorageType.VendorId = VIRTUAL_STORAGE_TYPE_VENDOR_MICROSOFT;

int openResult = OpenVirtualDisk(ref openStorageType, fileName, VIRTUAL_DISK_ACCESS_MASK.VIRTUAL_DISK_ACCESS_ALL, OPEN_VIRTUAL_DISK_FLAG.OPEN_VIRTUAL_DISK_FLAG_NONE, ref openParameters, ref handle);
if (openResult != ERROR_SUCCESS) {
    throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Native error {0}.", openResult));
}


// attach disk - permanently
var attachParameters = new ATTACH_VIRTUAL_DISK_PARAMETERS();
attachParameters.Version = ATTACH_VIRTUAL_DISK_VERSION.ATTACH_VIRTUAL_DISK_VERSION_1;
int attachResult = AttachVirtualDisk(handle, IntPtr.Zero, ATTACH_VIRTUAL_DISK_FLAG.ATTACH_VIRTUAL_DISK_FLAG_PERMANENT_LIFETIME, 0, ref attachParameters, IntPtr.Zero);
if (attachResult != ERROR_SUCCESS) {
    throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Native error {0}.", attachResult));
}


// close handle to disk
CloseHandle(handle);

System.Windows.Forms.MessageBox.Show("Disk is attached.");

Of course, in order for this to work, few P/Interop definitions are needed:

public const Int32 ERROR_SUCCESS = 0;

public const int OPEN_VIRTUAL_DISK_RW_DEPTH_DEFAULT = 1;

public const int VIRTUAL_STORAGE_TYPE_DEVICE_VHD = 2;

public static readonly Guid VIRTUAL_STORAGE_TYPE_VENDOR_MICROSOFT = new Guid("EC984AEC-A0F9-47e9-901F-71415A66345B");


public enum ATTACH_VIRTUAL_DISK_FLAG : int {
  ATTACH_VIRTUAL_DISK_FLAG_NONE               = 0x00000000,
  ATTACH_VIRTUAL_DISK_FLAG_READ_ONLY          = 0x00000001,
  ATTACH_VIRTUAL_DISK_FLAG_NO_DRIVE_LETTER    = 0x00000002,
  ATTACH_VIRTUAL_DISK_FLAG_PERMANENT_LIFETIME = 0x00000004,
  ATTACH_VIRTUAL_DISK_FLAG_NO_LOCAL_HOST      = 0x00000008
}

public enum ATTACH_VIRTUAL_DISK_VERSION : int {
  ATTACH_VIRTUAL_DISK_VERSION_UNSPECIFIED = 0,
  ATTACH_VIRTUAL_DISK_VERSION_1 = 1
}

public enum OPEN_VIRTUAL_DISK_FLAG : int {
  OPEN_VIRTUAL_DISK_FLAG_NONE       = 0x00000000,
  OPEN_VIRTUAL_DISK_FLAG_NO_PARENTS = 0x00000001,
  OPEN_VIRTUAL_DISK_FLAG_BLANK_FILE = 0x00000002,
  OPEN_VIRTUAL_DISK_FLAG_BOOT_DRIVE = 0x00000004
}

public enum OPEN_VIRTUAL_DISK_VERSION : int {
  OPEN_VIRTUAL_DISK_VERSION_1 = 1
}


[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct ATTACH_VIRTUAL_DISK_PARAMETERS {
  public ATTACH_VIRTUAL_DISK_VERSION Version;
  public ATTACH_VIRTUAL_DISK_PARAMETERS_Version1 Version1;
}

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
ublic struct ATTACH_VIRTUAL_DISK_PARAMETERS_Version1 {
  public Int32 Reserved;
}

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct OPEN_VIRTUAL_DISK_PARAMETERS {
  public OPEN_VIRTUAL_DISK_VERSION Version;
  public OPEN_VIRTUAL_DISK_PARAMETERS_Version1 Version1;
}

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct OPEN_VIRTUAL_DISK_PARAMETERS_Version1 {
  public Int32 RWDepth;
}

public enum VIRTUAL_DISK_ACCESS_MASK : int {
  VIRTUAL_DISK_ACCESS_ATTACH_RO = 0x00010000,
  VIRTUAL_DISK_ACCESS_ATTACH_RW = 0x00020000,
  VIRTUAL_DISK_ACCESS_DETACH    = 0x00040000,
  VIRTUAL_DISK_ACCESS_GET_INFO  = 0x00080000,
  VIRTUAL_DISK_ACCESS_CREATE    = 0x00100000,
  VIRTUAL_DISK_ACCESS_METAOPS   = 0x00200000,
  VIRTUAL_DISK_ACCESS_READ      = 0x000d0000,
  VIRTUAL_DISK_ACCESS_ALL       = 0x003f0000,
  VIRTUAL_DISK_ACCESS_WRITABLE  = 0x00320000
}

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct VIRTUAL_STORAGE_TYPE {
  public Int32 DeviceId;
  public Guid VendorId;
}

[DllImport("virtdisk.dll", CharSet = CharSet.Unicode)]
public static extern Int32 AttachVirtualDisk(IntPtr VirtualDiskHandle, IntPtr SecurityDescriptor, ATTACH_VIRTUAL_DISK_FLAG Flags, Int32 ProviderSpecificFlags, ref ATTACH_VIRTUAL_DISK_PARAMETERS Parameters, IntPtr Overlapped);

[DllImportAttribute("kernel32.dll", SetLastError = true)]
[return: MarshalAsAttribute(UnmanagedType.Bool)]
public static extern Boolean CloseHandle(IntPtr hObject);

[DllImport("virtdisk.dll", CharSet = CharSet.Unicode)]
public static extern Int32 OpenVirtualDisk(ref VIRTUAL_STORAGE_TYPE VirtualStorageType, String Path, VIRTUAL_DISK_ACCESS_MASK VirtualDiskAccessMask, OPEN_VIRTUAL_DISK_FLAG Flags, ref OPEN_VIRTUAL_DISK_PARAMETERS Parameters, ref IntPtr Handle);

I think that this is as short as it gets without hard-coding values too much.

If you hate copy/paste, you can download this code sample. Notice that this code only opens virtual disk. If you want to create it or take a look at more details, check full code sample.

P.S. Notice that this code will work with Windows 7 RC, but not with beta (API was changed in meantime).

P.P.S. If you get “Native error 1314.” exception, you didn’t run code as user with administrative rights. If you get “Native error 32.”, virtual disk is already attached. Just go to Disk Management console and select Detach VHD on right-click menu.

IE8 and Spelcheker

I do like new Internet Explorer features, but I do miss Firefox style spell checker. There is free (for non-commercial purposes) ieSpell add-on, but I do not like it’s modal dialog interface.

Is there any IE8 spell checker that is capable of red-underlining unknown words?

P.S. Title pun in intended. :)

Virtual Disk API After RC

I already wrote about Virtual disk API support in Windows 7. All that was based on beta so, now with Windows 7 RC in wild, some corrections are in order.

Microsoft decided to replace word surface with attach. While plenty of those changes were done in beta also (e.g. DISKPART), renaming of API functions was timed for release candidate. Since this change invalidates my previous code examples on how to work with virtual disks, here is new example that works with RC (but doesn’t work with beta).

Usual practice for release candidate is to be code-complete - hopefully no further API changes will be done before final version.

P.S. This code is work in progress, in no way it is production quality.

[2009-05-03: There was bug with one parameter in code. On beta, that bug went unnoticed, but on release candidate it causes open operation to fail with native error 87. I updated source code so all further downloads will point to correct files.]

[2009-06-12: There was change in enumerations from beta to RC that I missed. Side effect of it was inability to permanently attach virtual disk. This is fixed now (to make it more embarrassing, I used correct code in Open and attach sample but somehow I missed to notice the difference).]

Windows 7 RC

Release candidate is finally out for MSDN subscribers.

Thanks for subscription Marc. :)