Udostępnij za pośrednictwem


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.

Comments

  • Anonymous
    July 17, 2007
    Creating a .NET Sidebar Gadget - The Easy Way

  • Anonymous
    July 17, 2007
    I keep getting an error at: AxSBHost.RegisterGadgetAssembly(gadgetFilePath); Microsoft JScript runtime error: Object doesn't support this property or method;

  • Anonymous
    July 17, 2007
    Interesting. Are you running as an administrator on your system (not sure if that's the problem, just troubleshooting). Look in your registry and see if you can see this key: HKEY_CLASSES_ROOTCLSID{30E72785-9BEB-4ed0-B60A-4504D0AF7AAE}

  • Anonymous
    July 17, 2007
    I am running as an admin and I did not find that key in my reg.

  • Anonymous
    July 17, 2007
    The comment has been removed

  • Anonymous
    July 17, 2007
    I then get an error message that says: The module "C:...outputAxSBHost.dll" was loaded but the call to DllRegisterServer failed with error code 0x80070005.

  • Anonymous
    July 17, 2007
    Ok, that narrows it down enough for me to look at it. If I'm guessing correctly, you have UAC turned on and you're running into registry permission problems. I'll look into this and update the app. Thanks for looking into it. -Mark

  • Anonymous
    July 17, 2007
    Cool...Thanks for your work on this.  I'm very excited about using it!

  • Anonymous
    July 17, 2007
    "(if need be, I can post the source code for the BouncingBall gadget that is included as a sample for the application)" Yes, please!!! :-)

  • Anonymous
    July 18, 2007
    Are you sure this will work?  REGSVR32 will only work if the user is an elevated Administrator.  You'll need to manually put the DLL reg entries into HKCU for a standard Administrator.

  • Anonymous
    July 19, 2007
    This is awesome!  I've been itching to write a sidebar gadget (I'd love to have something that monitors our software), but the idea of slumming in COMland has kept me from it.  I'm going to do this soon! I'll let you know how it goes...

  • Anonymous
    July 24, 2007
    I get an error running registergadget.exe on vista that the index of the array was out of bounds?  Even with the sample.

  • Anonymous
    July 24, 2007
    I get the out of bounds index error on Vista as well. I am really looking forward to see this working! :)

  • Anonymous
    July 24, 2007
    NOTE A new version of the tool has been posted. See http://blogs.msdn.com/markhsch/archive/2007/07/24/net-sidebar-gadget-creator-update.aspx -Mark