Recently I entered a Gadget Competition hosted by MSOZACADEMIC and Si-Mi. The competition was announced on the MSOZACADEMIC blog and on the MSDN Flash. I do subscribe to the Flash but I did not see this competition during my skim of the newsletter. Fortunately, my work mate highlighted this for me. I thought it would be a good way for me to try out Sidebar Gadget programming and motivation to actually finish a home project. The competition closed at 23.59pm 31st October 2007 and to my surprise I had completely finished and upload my gadget a whole 4 days ahead of the closing date. To my delight my Auto Lock gadget was name Judges Choice. In the post Nick Ellery mentioned that he had seen a tutorial on this before. My prizes arrived today, so I thought it was about time to detail how I put together the gadget.
The tutorial that I was inspired by was on the MSDN Coding4Fun blog. The post was Bluetooth Screen Lock and demonstrated the use of the managed wrappers for the Coding4Fun Developer Toolkit which was still in Beta. This gave me a huge base to start off. I pulled open the toolkit and extracted just the relevant Bluetooth communications wrapper. Guided by the Bluetooth Screen Lock code, I then built a single .NET DLL exposing just the functions required to communicated to Bluetooth devices and Lock the computer. Then came what I thought would be the simpler task. Build an interface as a Vista Sidebar gadget and call the functions. Building the interface was fine, but calling the functions wasn’t so straight forward. Two steps are required to make this happen.
First, the DLL must be made COM visible. This enables the use of the DLL as an ActiveX object and is easily used from JavaScript in the gadget. This part is simply done by adding the ProgId attribute to your class which contains the public methods.
[ProgId("AutoLockActiveX.AutoLock")]
[ClassInterface(ClassInterfaceType.AutoDual)]
public class AutoLock
Second, the DLL must be registered before it can be used. This can be done by using regasm, but being a Vista Gadget, there is no installation to do this at it is just an unzipping of the files to the gadget directory. After a searching for a solution I came across a post by Jonathan Abbott, Using ActiveX DLL’s in Gadgets. This very nicely explains what needs to be done to automatically to register a DLL on the gadget start up, and also be a good citizen and remove the registration when closing the gadget. I cleaned up the code a bit, removing global variables and contained it all into it’s own file, RegisterActiveX.js. One thing the code was missing was the declaration of oShell, which is, var oShell = new ActiveXObject("WScript.Shell");. With further refactoring this could be made into a object oriented JavaScript file. Armed with RegisterActiveX.js I can easily write any .NET code for use within a Sidebar Gadget.
A little bit more tinkering and the JavaScript for the program control is put together seamlessly accessing the .NET DLL from a Vista Sidebar Gadget.
Now to go play with the new toys.