Building the ActiveX Control
The ActiveX control for this example is a simple one-method class that does nothing but send an email message via SMTP. The content, subject, format, and SMTP settings are all encapsulated in the parameters of the single method, which minimizes the interface footprint. Listing 1
shows the entire source for the object:
The code in Listing 1
simply invokes the built in System.Net.SmtpServer class to send an email represented within a System.Net.Mail.MailMessage. It's a relatively trivial operation using the Microsoft .NET Framework version 2.0, but two things make the component COM-worthy after compilation:
- The inclusion of ComVisible(true) in the AssemblyInfo.cs file
- The code is signed with a strong name. This isn't really a hard requirement, but it sidesteps some issues that sometimes pop up when exposing .NET components as ComVisible, (namely, the component can be installed into the Global Assembly Cache.)
After building the project, copy the DLL from the output directory to somewhere where it is safe to register. I located it in the gadget directory, which for the sake of the example is named QuickMailr.gadget
Registering the DLL
After compiling, you need to register the DLL for COM. You'll need administrator privileges to register it. One Vista feature is User Account Control (UAC), which allows administrators to run most processes with normal user (non-admin) privileges. This means that you have to elevate your permissions to register the library using the standard RegAsm.exe
tool. UAC isn't a show-stopper at allyou just need to invoke the command shell as an administrator. Here's the step-by-step procedure to register the DLL:
- Right click on the Visual Studio Command Prompt in the Start Menu, or the C:\Windows\System32\cmd.exe icon in Windows Explorer and select "Run As Administrator." A normal-looking command shell window will appear.
- From the command prompt, change directory (cd) to the directory where you placed the compiled DLL file.
- Enter RegAsm.exe /codebase NameOfYourDll.exe
Assuming you named your DLL ActiveXMailer
, for this example, the command line for step #3 would be:
RegAsm.exe /codebase ActiveXMailer.dll
|Author's Note: The included Install.cmd file contains that line of script; you can simply right click on the file and choose "Run As Administrator" to perform the registration, provided that the RegAsm.exe executable (from the .NET Framework version 2.0 SDK) is in your path. If it's not in your path, you can invoke the Install.cmd file from a Visual Studio Command Prompt.
If you see the standard success message, "Types registered successfully," the DLL is registered properly, and you can close the command shell.
Installing the Gadget
|Figure 3. Configuring the Gadget: You access the settings by clicking on the small "wrench" settings icon.|
I mentioned earlier that Sidebar gadgets must reside in folders with names that end in .gadget
, but you can also package them as CAB or ZIP archives. The easiest method is to take the .gadget
in this case) and zip it using the "Send To
Zip Archive" option in the Windows Explorer context menu. After the .zip
file is created, rename the file by replacing .zip
. This tells Windows that the files contained within the archive are a Sidebar gadget.
After zipping and renaming the gadget file, you can install it by simply double clicking on the file icon. A prompt will appear asking if you'd like to install the gadget. Choosing "Install" will publish the gadget and extract the contents to a folder (%userprofiledir%\AppData\Local\Microsoft\Windows Sidebar\Gadgets\QuickMailr.gadget
). To add your gadget to the Sidebar, right click anywhere on the Sidebar background and select "Add Gadgets
If all has gone according to plan, the new gadget will appear in the list, and you can select it using a double click.
That's it! The gadget is up and running and should look like Figure 1
. All that's left to do is configure the SMTP settings. If you hover your mouse over the gadget in the Sidebar, a small 'wrench' icon appears next to it (see Figure 3
The "settings" icon is a standard feature of all gadgets that support a settings panel. Click the settings icon and enter your SMTP information as shown in Figure 4
|Figure 4. QuickMailr Settings: Here's the QuickMailr settings panel, where users enter their SMTP information so they can send email messages.|
After closing the settings panel, you're now ready to send some QuickMail! Just enter one of your own email addresses in the TO:
field, and paste your favorite link (e.g. www.devx.com
) into the Message field, and click "Send." Seconds later, you'll get an email with the subject "QuickMailr Message."
The concept of the Sidebar is relatively familiar, but it does provide a consistent and useful host for DHTML applets that can enhance the overall Vista experience. There are many more avenues to explore in the realm of building Sidebar gadgets, and I highly recommend that you peruse the official Microsoft documentation