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.
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.
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?
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).]