Last Tuesday (31.08.2010), Microsoft released a new version for the excellent managed library, Windows API Code Pack.
For those who don't know, this library serves as a managed wrapper for many Windows APIs which are not included in the .NET framework, especially new features from Windows Vista and Windows 7.
I've been working with this library for quite some time now and I must say it is “.NET missing piece”. Most of the Windows features which don't exist in .NET are at your fingertips, without handling all the Interop stuff yourself.
With the coming of .NET Framework v4.0 which supports side by side execution, we can finally write managed shell extensions without having to worry about race conditions between the installed CLRs. And what could be a better starting point for such a shell extension than Windows API Code Pack?
So, what’s new in this release?
Well, the official site says that other than bug fixes, the main new features are:
- Shell Object Watcher
- Preview Handler APIs
- Thumbnail Handler APIs
Let’s try to understand what it means..
Shell Object Watcher
ShellObjectWatcher is a new class in Windows API Code Pack that provides notifications on all elements in the shell, including: files, folders, virtual folders (libraries, search results, network items), etc.
Using this class, you can get notifications on: Create, Delete, Rename, Share, Drive Added, etc.
The magic is done using the Win32 API SHChangeNotifyRegister.
The managed API is rather simple and there is a WPF sample application that shows how to get all events of a user selected path:
In the image, we see Shell Object Watcher sample application behavior when inserting a USB drive, creating and renaming a folder on C:\ and creating a new text file in the folder.
Shell extensions are custom extensions to the Windows operating system, or to be more specific, the shell. Different types of shell extensions extend different parts of the Windows shell.
Many parts of the shell can be extended, for example, Windows API Code Pack provides support for the following shell extensions:
- Preview Handlers – lets you customize the way the preview window in Windows Explorer will behave on specific file extensions.
- Thumbnail Handlers – lets you customize the thumbnail of files with a specific file extension.
One thing worth noting is that all shell extensions are usually COM-based components. But using COM Interop, we can implement them using the .NET Framework.
The way it usually works is as follows:
- First, you need to write code that adheres to a specific, predefined interface. The
interfaces depend on the part of the shell you want to extend.
- Second, you register your object in a specific part of the registry. Different types of shell extensions are registered in different locations of the registry.
- Finally, you use your shell extension by simply going to the part of the shell you have extended and see it in action.
Preview Handler APIs
Preview handler is one type of shell extension. It allows you to customize the preview window in Windows Explorer for a specific file extension.
In the image below, we can see on the right a custom preview handler for the file extension xyz2.
In this example, the preview handler reads the information from the xyz2 file and displays it. Here is the content of the file SampleFile.xyz2:
<Name>Handler Sample File</Name>
<Content>Windows? API Code Pack for Microsoft... </Content>
Of course, you can make preview handlers for any file extension, regardless of the file format.
Thumbnail Handler APIs
Thumbnail handler is another type of shell extension. It allows you to customize the thumbnail of a file extension in Windows Explorer.
For example, in the image below, we can see that the thumbnail of the file SampleFile.xyz2 shows the image that is encoded inside the file.
That’s it for now,