Which Brace Style to Use

Among C-ish programmers, there is often dispute which brace style to use. K&R style (named after authors of The C Programming Language)

void Main() {
  Console.WriteLine("Hello World!");
}

was very popular once, but Allman style

void Main()
{
  Console.WriteLine("Hello World!");
}

is what seems to be popular now. Basically only difference is where to put starting brace. Whether it stands behind control statement or it stands on dedicated line. And that is something that people cannot decide for quite a while now. Both of them have their advantages that I will not discuss here (there is pretty good article on Wikipedia).

I am currently using K&R style. Here I will try to make two points why everyone should switch to Allman.

Visual Studio defaults

Default brace style for C# is Allman. If you need to modify code written by others, that code will rarely use K&R. While Visual Studio and C# do give you great options of reformatting code, I consider doing that just being rude.

I was known to change formatting of code in other people files and it took me a while to understand how unreadable whole project becomes when half of files use one style and half another. Now, if project has established style, I will continue adding stuff in same style.

Readability

If you are used to code in one style, you are better at scanning code in that style. If you get Allman code, it helps to be used to Allman to read it faster. It does not hurt too much if your code preference is different, but there is some speed advantage.

Conclusion

I can see that I should code in Allman all the time. I know it would be beneficial. But I cannot force myself to do it. Although all reasoning says that Allman is more readable, it seems that my mind is just not used to it. It just likes to find them by indentation rather than by matching braces.

Maybe it is because I have history in VB which looks quite like K&R in indentation style, maybe it is because I never gave Allman more than a week to get used to, maybe it is just because I like to be different.

Against of all reasoning - I am sticking with K&R for now.

MAXtv

I think that my IT background caused me to get IPTV as soon as it was available to me. Service that I decided on was MAXtv.

It had few quirks now and then, but I was generally satisfied. Few days ago they even said that there will be update making that service even better (more human-friendly, some would say).

Installation that started at 08:00 is now more than one hour in progress. Customer service expects that everything should be back to normal by noon.

Don’t you do that kind of update during night time?

Windows 7 and the Power of XP

Illustration

One of optional components of Windows 7 is Windows XP Mode. Idea sounds great: “Let’s make one virtual machine with Windows XP and run all legacy software on it.”. This approach gives you great flexibility in upgrading your software since anything compatible with XP becomes compatible with Windows 7.

In order to use it, additionally to having Windows 7 release candidate, you need to download both beta of Virtual PC 2007 and beta of Windows XP mode.

How does it look

Illustration

Interface is really minimalistic. I do miss hard disk activity icon in status bar, but if you consider that plenty of people who have no idea what virtualization is will use it, that may not be bad idea. All other options seem to be in place (not that Virtual PC had much of options anyhow).

New Virtual PC

Since this feature is based on new Virtual PC, this is great chance to see how things will look with new version.

First thing that I noticed is USB menu. Believe it or not, it seems that Microsoft finally decided to add some support for USB devices. I tested it with USB drive and it worked (after automatic installation of small stub driver), but I cannot vouch for more complicated devices. For some devices (in my case VGA camera) it shows unidentified device, but attaching it does work properly so I will give it benefit of doubt and assume that this is something inherit to beta or my laptop.

Everything else seems to be same like in Virtual PC 2007 but with less flexibility (no separate virtual disk wizard, no device activity icons…). Hopefully advanced interface (like one in old Virtual PC) will be available after this beta.

Auto publishing

Illustration

When I saw picture of Windows XP mode in action for the first time, I liked feature that they called auto publishing (to me it looks same as VirtualBox’s seamless mode).

In theory it seemed simple, you just install some legacy application in Windows XP (for me it was Google’s Chrome - it still doesn’t work on Windows 7 - shame on you Google) and auto publishing should add shortcut to that application in Windows 7 menu. Once you start your application via that shortcut, you get desktop integration and all that stuff.

Problem with that simple scenario is that it does not work. In moment of pure desperation, I even tried looking it up in help. That action wasn’t helpful since documentation is still not done and all I got was equivalent of “This page was intentionally left blank”. By pure accident, I stumbled upon Virtual PC forum. Only there I saw how that should be done. You need to manually create “Programs on the XP VM” folder under “C:\Documents and Settings\All Users\Start Menu”. Once shortcut gets placed there, it also gets copied to Windows 7 menu.

Illustration

Another thing that can cause some problems is fact that you cannot both see desktop and applications at same time. Although this is not a big problem in normal circumstances, it causes minor inconvenience when you need to go in Control Panel (or some other non-published application) since you need to save your work first. Same thing will also happen if you go from full desktop to application mode.

There are minor issues with Windows 7 specific features and application virtualization. One that I mind most is missing thumbnail preview on taskbar button. If you have few applications open (e.g. few instances of Visual Basic 6.0), it is impossible to tell them apart without actually switching.

Other minor issues

For this to have any chance of working, you need hardware support for virtualization. I do not quite get why this is prerequisite. Everything that you could do with hardware supported virtualization, you could do without it also (with some speed loss). [2010-03-19: Hardware supported virtualization is not needed any more.]

Speaking of speed, starting time for this is not so good. It takes a while for this virtual machine to boot. While I do understand reason behind slow start, I am not so sure that someone using it on daily basis would be happy with that explanation.

I also doubt decision to make hibernate a default for close action. Hibernate may seem like a decent option at first, but probability is that some imaginary “default” user will probably always perform close (and implicitly hibernate it every time). I am not that sure that application that hasn’t fix it’s compatibility bugs for last seven years will take hibernate kindly. Expecting inexperienced user to find shut-down option behind Ctrl+Alt+Del and know when to do it is little too much of optimism for my taste.

Is it any good?

For those clients stuck with some legacy application, I would say yes. You get great new interface and features, along compatibility with your Clipper-like weight. For programmers - definitely - you get one free XP system to test your programs on.

All others can safely ignore it.

QText 2.10 (Beta 1)

There are some bug fixes and minor changes along a big one. Installation engine was changed to Inno Setup. This was done in order to support both 32-bit and 64-bit installations in same file and to enjoy quite nice features. Which ones? I will probably write a post or two later.

Illustration

This change requires installation to first remove any old versions. I have tested procedure with versions 1.31, 1.40 and 2.00. If you have other version installed I would advise to uninstall it first since installation may not uninstall it automatically.

In case something goes wrong with uninstalling, you end up with two versions of QText in Uninstall dialog. Once that happens, old version will not be uninstallable. Except for that annoyance, everything else should work properly even if that happens.

No damage to your data should happen during uninstall. However if you wish to backup it first, just go Options -> Files -> Open folder. This will open Explorer window. From there you can copy your files anywhere you wish. Once new version is installed, just copy files back. This step is optional since uninstall will not delete any files or settings and install will take them over. All your files should be safe.

You can download it here.

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.