Monday, April 13, 2009

Code Snippets for C++ in VS2005

Visual Studio 2005 introduced the concepts of code snippets. Handy little chunks of commonly-used code that are easily inserted to your own with some Intellisense magic. Hit a keypress, choose the snippet, fill in any fields and voila.

C# and VB users were spoilt; MS supplied a bunch of snippets to do many common tasks. Iterating over containers, creating regions, defining classes…many common tasks were snippetified. Further, there were many more online.

Alas, C++ users were left in the dark with no snippet support at all.

Thankfully the release of the "Microsoft Visual Studio 2005 IDE Enhancements" addressed that issue (also known as the VS2005 "Powertoys"). After installation you get – among a few other questionably useful tools - C++ snippet support in VS2005. There are only two problems. 1) If you're using Vista you get a bizarre error 1 when you try to run the installer. 2) You don't get any C++ snippets installed. Instead Microsoft makes the C# snippets available in C++. Thanks guys. Anyway, let's address those problems.

1) The issue here is that Vista requires administrator privileges to install this package. Had the install package been an exe we'd have been fine; shift-right click and select "Run as Administrator". Alas it's an MSI so we'll have to find another way. The simplest, as described by the MS UK Dev Team, is to fire up a command shell, running it as admin (open the start menu, type 'cmd' then shift + right-click the cmd.exe icon), change to the directory where you downloaded the PowerToy installer then execute the command:

msiexec /i "VSSDK_PowerToys.msi"

Then follow the GUI installer as normal. Oh, you'll need to exit from all VS2005 instances.

[You may notice – well, I did – that Visual Studio will have a bit of a conniption fit when you restart. It appears that it's setting itself up for the first time again. Not sure why. Thankfully all my keybindings etc remained intact and there were no noticeable problems but YMMV.]

2) Once everything is installed correctly you should find that the "Tools->Code Snippets Manager" should now have "Visual C++" listed as a language. Good sign. However by looking through the supplied list of snippets you'll find they're all actually C# snippets. You'll want to write your own C++ snippets. To do that you'll need a snippet editor since writing the XML by hand is tedious (far from impossible but annoying).

Of the bunch of tools I found that edited snippets only Snippy supported C++ snippets. Snippy works fine but doesn't seem to have a binary distribution so you'll need to download and build it (a VS2005 solution file is supplied). After you run it create your own snippets and save them somewhere (like "Users\YourName\Documents\Visual Studio 2005\Code Snippets\Visual C++\YourSnippets"). You can then add those snippets to VS2005 using the Snippet Manager.

I haven't discussed the format of a snippet, it's pretty easy to figure it out from Microsoft's documentation. But here's one to get you started. If people ask nicely I'll try and post more.

And that's it! Now you should be ready to create your own wicked-cool C++ snippets. Let me know if you have any rippers!

1: The error is something like: "Error 1721. There is a problem with this Windows Installer package. A program required for this install to complete could not be run. Contact your support personnel or package vendor." I love how the error makes it so clear that administrator privileges are required.