Creating Virtual Disk

After open and attach, most common virtual disk operation will be create.

Here it is:

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

long size = 128 * 1024 * 1024;

IntPtr handle = IntPtr.Zero;

var parameters = new CREATE_VIRTUAL_DISK_PARAMETERS();
parameters.Version = CREATE_VIRTUAL_DISK_VERSION.CREATE_VIRTUAL_DISK_VERSION_1;
parameters.Version1.BlockSizeInBytes = 0;
parameters.Version1.MaximumSize = size;
parameters.Version1.ParentPath = IntPtr.Zero;
parameters.Version1.SectorSizeInBytes = CREATE_VIRTUAL_DISK_PARAMETERS_DEFAULT_SECTOR_SIZE;
parameters.Version1.SourcePath = IntPtr.Zero;
parameters.Version1.UniqueId = Guid.Empty;

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

int res = CreateVirtualDisk(ref storageType, fileName, VIRTUAL_DISK_ACCESS_MASK.VIRTUAL_DISK_ACCESS_ALL, IntPtr.Zero, CREATE_VIRTUAL_DISK_FLAG.CREATE_VIRTUAL_DISK_FLAG_FULL_PHYSICAL_ALLOCATION, 0, ref parameters, IntPtr.Zero, ref handle);
if (res == ERROR_SUCCESS) {
} else {
    throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Native error {0}.", res));
}

// close handle to disk
CloseHandle(handle);

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

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

public const Int32 ERROR_SUCCESS = 0;

public const int CREATE_VIRTUAL_DISK_PARAMETERS_DEFAULT_SECTOR_SIZE = 0x200;
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 CREATE_VIRTUAL_DISK_FLAG : int {
  CREATE_VIRTUAL_DISK_FLAG_NONE                     = 0x00000000,
  CREATE_VIRTUAL_DISK_FLAG_FULL_PHYSICAL_ALLOCATION = 0x00000001
}

public enum CREATE_VIRTUAL_DISK_VERSION : int {
  CREATE_VIRTUAL_DISK_VERSION_UNSPECIFIED = 0,
  CREATE_VIRTUAL_DISK_VERSION_1            = 1
}

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 CREATE_VIRTUAL_DISK_PARAMETERS {
  public CREATE_VIRTUAL_DISK_VERSION Version;
  public CREATE_VIRTUAL_DISK_PARAMETERS_Version1 Version1;
}

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct CREATE_VIRTUAL_DISK_PARAMETERS_Version1 {
  public Guid UniqueId;
  public Int64 MaximumSize;
  public Int32 BlockSizeInBytes;
  public Int32 SectorSizeInBytes;
  public IntPtr ParentPath;
  public IntPtr SourcePath;
}

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


[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 CreateVirtualDisk(ref VIRTUAL_STORAGE_TYPE VirtualStorageType, String Path, VIRTUAL_DISK_ACCESS_MASK VirtualDiskAccessMask, IntPtr SecurityDescriptor, CREATE_VIRTUAL_DISK_FLAG Flags, int ProviderSpecificFlags, ref CREATE_VIRTUAL_DISK_PARAMETERS Parameters, IntPtr Overlapped, 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 creates virtual disk. If you want to 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 80.”, file you are trying to create is already there.

Windows Media Center Without IR Extender

Illustration

I tried to configure my MAXtv to work with Windows Media Center. Since that set-top box has S-Video, I though this would be really easy. Oh, how wrong I was.

Someone at Microsoft decided that you MUST have small piece of hardware attached if you wish to receive signal from any external port. Message said it all: “IR Hardware Not Detected”. That small piece is usually called IR blaster and its sole purpose is emulating remote toward your set-top box. While that is useful in normal circumstances, you may not need it all time (e.g. you want to control set-top box with universal remote - like Logitech’s Harmony 555).

However, there is a solution. Installing virtual Vista MCE IR blaster driver takes care of missing hardware and lying to Media Center takes care of configuring it.

Once configured, everything works fine, but I wonder why I needed to jump through hoops to get my S-Video input working.

Windows 7 - VHD

Illustration

I am giving lecture at KulenDayz 2009 developer conference. I am quite curious how it will go since I am primary a developer and this is my first lecture in system administration track.

If you are in neighborhood, please do come.

P.S. Guess which one I am on this picture (hint: look what pumpkin’s eyes are saying)…

Visual Studio and WPF

Illustration

Visual Studio 2010 really shines with its new WPF interface. Side effect of this is relying little bit more on your graphics card. If graphics driver has a bug, you have a problem.

It seems that nobody at Microsoft tested their Visual Studio 2010 beta 1 with Intel’s GE45 integrated graphics. At least once per hour I get “Display driver stopped responding and had recovered” balloon. While this is not end of world, it is pretty annoying.

While this is probably driver problem, I cannot help but to put some blame on Microsoft since only application that ever caused this bug was Visual Studio 2010. Of course I need to blame myself a little also, since I am the one using both Windows 7 and Visual Studio 2010 in their beta stage.

[2009-07-22: New Intel graphics driver is issued over Windows Update with June 2009 markings. It seems that this driver solves problem. I used it for few hours straight without any issues. I do hope it stays that way.]

Wireless

New Virtual PC (currently in beta, integrated with Windows 7) is looking more and more like “light” Hyper-V. It requires hardware support for virtualization, uses same key combinations…

One thing that remains same is support for wireless network. Hyper-V deliberately doesn’t have direct support for it because of some issues, but Virtual PC ignores all and makes life a lot easier.