Creating .NET Sidebar Gadgets...The Easy Way

So you want to create a Vista Sidebar gadget and you want to use .NET to do so. While it is certainly possible, it does have a few hurtles to jump. 2 of the major hurdles include assembly registration and gadget uninstallation. Let me explain:

First off, let me explain the typical setup of a gadget. Skip this paragraph if you already know. A gadget consists of a main HTML page with corresponding image and javascript supporting files. Each gadget also can optionally use a separate HTML file for settings. Each HTML file can utilize the gadget API provided by Sidebar. All the files and the gadget information itself is described using an XML file. This file contains information such as the gadget name, author, support URL, main gadget HTML filename, etc... Now, if you want to use a .NET UserControl, you have to register it as a COM server and embed it using the IE AcitveX object embedding mechanism. Easier done than said. OK, with that out of the way, let's continue.

The ideal situation is to have a user download your gadget from https://gallery.live.com and have the gadget install for you (Sidebar uses the .gadget file extension for this purpose). However, since you're not following a traditional installation path, you don't have the ability to register your assembly for COM so that it can be embedded within the HTML file your gadget uses. The current workaround is to create a Windows Shell Scripting object and manually add the registry entries each time your gadget is loaded. I won't display the code here because frankly it's somewhat ugly and susceptible with error. To further compound the problem, you have to create the registry entries in either HKCU or HKLM depending on whether the user is an administrator or not. Well, if you manage to do that, you're good to go. Of course, next time you create another gadget, you'll have to do it all again (ok, just a cut-n-paste and GUID changes but....still a pain).

The 2nd issue that occurs when trying to create a .NET based Sidebar gadget is gadget uninstallation. This is the main issue that I've seen people just can't seem to figure out (until now that is...be patient and read on. Don't skip to the end). Once your gadget is installed and running, a user should be able to open the Add Gadget window (click the + sign on top of Sidebar), right click on your gadget and select Uninstall. Sidebar then removes any instances from the Sidebar and attempts to delete the gadget files. Here's the problem (and I can't get too specific). Even though the gadget is removed from the Sidebar, the .NET assembly is still loaded within the Sidebar process. Sidebar therefore cannot remove the files and will display an error message. The 2 options you have are to restart the Sidebar or to wait a certain amount of time (I forget, but it's awhile...like 10 or 20 minutes). Not ideal for users is it.

While some developers might just think it'll be ok to let the uninstallation failure slide, you should know that when you submit your gadget to https://gallery.live.com, it will fail since it doesn't uninstall.

So, what's a developer to do? I ran into this same problem with my ScreenSaver gadget. To that end, I now present the .NET Sidebar Gadget creator (link at bottom). This is an application that will make creating a .NET based Sidebar gadget extremely easy. Wouldn't it be nice if all you had to do was (after creating your .NET control of course) fill out a few form fields, click a button and be finished? It is now. The application itself should be fairly self-explanatory when you run it. The main sticky point is that you obviously need a .NET assembly containing a UserControl that will be the main UI of your gadget (if need be, I can post the source code for the BouncingBall gadget that is included as a sample for the application).

Run the application, browse to your assembly, fill out the required information and click the Build button. The .NET Sidebar gadget creator will then reflect on the assembly and extract control information, generate HTML, JavaScript and the gadget XML files and place everything in the destination directory you specify. Here's a screenshot:

image

Download the application, try it out and let me know if it works/doesn't work for you.

Here's the link: NetSBHostCreator.exe

UPDATE: There is a problem with UAC. I think I have a working solution but I need to test a little more to make sure everything works fine. I'll post an updated solution soon followed closely by source code.