Managed Preview Handlers for Vista and Office

Wed, December 13, 2006, 02:25 PM under Windows | Vista
Truly fantastic article on MSDN magazine January 2007 issue: Managed Preview Handler Framework.

If you don't know what preview handlers are, on your Vista box open explorer, click on "Organise", "Layout" and then "Preview Pane". Now select a file and watch the preview pane. The file must be an office file or video or image and some others. For each file type/extension a preview handler exists that renders it in the preview pane (this also works for Outlook 2007 preview pane for attachments).

So how do you write a preview handler? Until now the answer was COM. Now with this article, Stephen Toub shares a framework that allows us to build them with managed code! Visit the article to download the framework which includes all the source code too.

Not only that, the download also includes preview handlers for files with extensions: bin;dat;csd;xps;xml;psq;isf;msi;pdf;resx;snk;zip and xaml. So even if you are not a developer caring about the framework, go get the download for the handlers themselves, to enrich your own Vista experience.

So, in a nutshell the framework has a simple inheritance hierarchy that encapsulates the COM ugliness and offers extensibility points for hooking in your own managed preview handler. When reading the article, if you are interested in the framework’s internals, keep this class diagram in mind. Another class diagram shown below should make clear how ridiculously easy it is to create these things now:

So to write a simple Hello World preview handler:
1. Create a new Class library, and reference the MsdnMagPreviewHandlers.dll from the article download.
2. Add a new code file and add to it these few lines of code.
3. Build the dll, gac it and regasm it (see the article if you have trouble with that, both tools are in your C:\Program Files\Microsoft SDKs\Windows\v6.0\Bin folder)
4. Create a new file somewhere, test.moth and watch the preview:

Now do you see how extremely cool the Managed Preview Handler Framework is? Use it for your own file formats! At a future entry I'll show you a more meaningful preview handler.

BTW, if you have no interest in preview handlers, the code is still a great example of how to wrap COM APIs so read the article for the in-depth explanation of this diagram.