Click here to Skip to main content
15,397,084 members
Articles / Web Development / HTML
Posted 30 Jan 2007


259 bookmarked

.NET Interop for Gadgets – A C# GMail Inbox Reader Example

Rate me:
Please Sign up or sign in to vote.
4.84/5 (108 votes)
30 Jan 2007CPOL9 min read
How to call absolutely any .NET code from your Vista Sidebar Gadget

Sample Image - GadgetInterop.png



Since the writing of my sidebar gadget introduction, I've been wrestling with the frustration of not being able to do with a gadget what can be easily done with .NET. I love how compact and simple gadgets can be, but I find it hard to build a truly useful gadget simply because there's no real power using JavaScript. Unfortunately for the .NET community, gadgets rely almost purely on JavaScript. It's hard to mix my excitement for gadgets with their huge limitations.

If Only I Could Run .NET Code from Gadgets…

Enter Gadget .NET Interop!

In this article, we'll explore how to build an interop layer between gadgets and .NET so you can run any .NET code from your sidebar gadget. We'll do that by building a C# project to read your GMail inbox.

COM and ActiveX

It's not fair to say that gadgets can't run .NET code. The truth is that it's very easy to create COM object instances from scripting languages. The real problem is that it's terribly inconvenient to have to register all your code for COM interop. Doing so would require you to first modify all your code to be COM compatible. Then you'd have to re-package your code and distribute an MSI file along with each and every gadget just to install and register your assembly (and possibly add it to the GAC if it's going to be shared across gadgets). That kind of workaround isn't a realistic solution, especially if you already have code that you don't want to rewrite and package just for COM interop. Further, you can't assume your users have the knowledge or permissions to install a COM component.

What's the answer then?

No matter what, there must be some COM pieces in place; otherwise we'll never get past the limitations of JavaScript. We'll get to the GMail part once we have a suitable COM layer (see below if you're comfortable with COM in .NET). Let's start with the real nuts and bolts of the solution by creating a basic COM object that can be used load any .NET assembly. See this article for more details on how .NET COM objects.

.NET COM Interface

The idea is simple; create a small, lightweight .NET COM component that uses reflection to load any assembly and type. Then, that type can be called directly from JavaScript. Let's take a look at the interface for the "Gadget Adapter" that will do the bulk of the work.

public interface IGadgetInterop
    object LoadType(string assemblyFullPath, string className); 
    object LoadTypeWithParams(string assemblyFullPath, string className, 
                              bool preserveParams); 
    void AddConstructorParam(object parameter); 
    void UnloadType(object typeToUnload); 

There are only four methods that the implementing Gadget Adapter class will need to handle. The point to take note of is that the interface has three attributes that will allow us to expose the implementing class as a COM object. Four methods are all we need to create and call any type in managed code.

Gadget Adapter

Now that the interface is defined, let's look at the actual Gadget Adapter implementation of this interface. We'll break it out piece-by-piece starting with the class attributes.

public class GadgetAdapter : IGadgetInterop

There are a few differences between these attributes and the attributes on the interface. The most important attribute for our purposes is the "ProgId" attribute. This attribute represents the string we'll use to create the ActiveX object via JavaScript. Now that the GadgetAdapter is decorated properly, the next step is loading assemblies and creating class instances. The AddConstructorParam method allows JavaScript code to add values that will be passed to the class constructor's arguments. This is only necessary when want to load a .NET type using a constructor with one or more arguments.

private ArrayList paramList = new ArrayList();
public void AddConstructorParam(object parameter)

The next method is where all the magic happens. The LoadTypeWithParams method has the three arguments that allow any .NET assembly to be loaded. The method takes the path to the assembly, the type to create, and a flat for handling constructor parameter disposal.

public object LoadTypeWithParams(string assemblyFullPath, string className,  
                                 bool preserveParams)
    Assembly assembly = Assembly.LoadFile(assemblyFullPath);
    object[] arguments = null; 
    if (paramList != null && paramList.Count > 0)
        arguments = new object[paramList.Count];
    BindingFlags bindings = BindingFlags.CreateInstance |
                            BindingFlags.Instance | 
    object loadedType = assembly.CreateInstance(className, false, bindings, 
                            null, arguments, CultureInfo.InvariantCulture, 

    return loadedType;

Using standard .NET reflection, the specified assembly is loaded and an instance of the input type is created. That instance is returned and is then directly callable by JavaScript (more on that to come). The preserveParams flag prevents the constructor arguments from being cleared after the object is created. This is only necessary when you're creating multiple instances of a class with the same constructor arguments.

Finally, because we're in the COM world, we have to be careful to do our own object disposal. The UnloadType method calls dispose of the incoming object to allow for graceful cleanup.

public void UnloadType(object typeToUnload)
    if (typeToUnload != null && typeToUnload is IDisposable)
        (typeToUnload as IDisposable).Dispose();
        typeToUnload = null;
    catch { }

The one convention I opted for is that classes exposed to gadgets must implement IDisposable, so only types implementing that interface will work with the sample code. That's all there is to the interop layer. It creates .NET objects and it destroys .NET objects; nothing more, nothing less.

Automatic Registration at Runtime

Now we have a working COM-friendly Gadget Adapter, but how does it get registered? Normally you would rely on an MSI installer to register and GAC your COM components. Remember that the goal here is to run .NET code in a gadget without the user having to install an MSI. To get around the MIS (or RegAsm.exe) we can "fake" the registration by adding the right values directly to the registry (My thanks to Frederic Queudret for this idea). The GadgetInterop.js, a JavaScript library, is designed to facilitate the Gadget Adapter registration (as well as all the COM object wrapping). The RegAsmInstall JavaScript method takes all the information about the Gadget Adapter interop assembly and creates all the necessary registry entries to register it. The beauty of this step is that any gadget can register the interop layer at runtime the first time the gadget executes.

function RegAsmInstall(root, progId, cls, clsid, assembly, version, codebase)
    var wshShell;
    wshShell = new ActiveXObject("WScript.Shell");
    wshShell.RegWrite(root + "\\Software\\Classes\\", progId);
    wshShell.RegWrite(root + "\\Software\\Classes\\" + progId + "\\", cls);
    wshShell.RegWrite(root + "\\Software\\Classes\\" + progId + \\CLSID\\,
    wshShell.RegWrite(root + "\\Software\\Classes\\CLSID\\" + clsid +
                      "\\", cls);
    wshShell.RegWrite(root + "\\Software\\Classes\\CLSID\\" + clsid +
                      "\\InprocServer32\\", "mscoree.dll");
    wshShell.RegWrite(root + "\\Software\\Classes\\CLSID\\" + clsid +
                      "\\InprocServer32\\ThreadingModel", "Both");
    wshShell.RegWrite(root + "\\Software\\Classes\\CLSID\\" + clsid +
                      "\\InprocServer32\\Class", cls);
    wshShell.RegWrite(root + "\\Software\\Classes\\CLSID\\" + clsid +
                      "\\InprocServer32\\Assembly", assembly);
    wshShell.RegWrite(root + "\\Software\\Classes\\CLSID\\" + clsid +
                      "\\InprocServer32\\RuntimeVersion", "v2.0.50727");
    wshShell.RegWrite(root + "\\Software\\Classes\\CLSID\\" + clsid +
                      "\\InprocServer32\\CodeBase", codebase);
    wshShell.RegWrite(root + "\\Software\\Classes\\CLSID\\" + clsid +
                      "\\InprocServer32\\" + version + "\\Class", cls);
    wshShell.RegWrite(root + "\\Software\\Classes\\CLSID\\" + clsid +
            "\\InprocServer32\\" + version + "\\Assembly", assembly);
    wshShell.RegWrite(root + "\\Software\\Classes\\CLSID\\" + clsid +
            "\\InprocServer32\\" + version + \\RuntimeVersion, 
    wshShell.RegWrite(root + "\\Software\\Classes\\CLSID\\" + clsid +
            "\\InprocServer32\\" + version + "\\CodeBase", codebase);
    wshShell.RegWrite(root + "\\Software\\Classes\\CLSID\\" + clsid +
            "\\ProgId\\", progId);
    wshShell.RegWrite(root + "\\Software\\Classes\\CLSID\\" + clsid +
      \\Implemented Categories\\{62C8FE65-4EBB-45E7-B440-6E39B2CDBF29}\\, 

Nothing too complex, just a few basic registry entries. The rest of the JavaScript library serves as a wrapper for the Gadget Adapter's methods. You can use the GadgetBuilder's methods from your own script to load or unload any .NET type.

Now that we have the interop layer in place, let's take a look at the GMail example (included in the source code download) to see how the interop layer actually gets used.

GMail Reader - Running .NET Code

Reading a GMail account inbox is as easy as reading an XML feed. The feed is so simple that it actually could be parsed using JavaScript. I purposely added some complexity to the GmailReader assembly to have something in code managed that I couldn't do in JavaScript. Plus, using .NET code adds speed and the ability to debug (That's far more than you can ask of JavaScript). To that end, the response XML from the GMail feed is run through an XslCompiledTransform to deserialize the response into a generic list (i.e., strongly typed) of a type I created. The types in that generic list can then be exposed directly to JavaScript. I won't go into GMail code here as it's easily understandable and well commented. What's really important is understanding what was done to make the code JavaScript-friendly, and how to call that code from JavaScript. There are a few key steps that are required.

public class GmailClient : IDisposable

By far, the most important step is to add the [ComVisible(true)] attribute to any class that will be used by your JavaScript. The class will simply not be callable by JavaScript without this attribute. The other convention is implementing the IDisposable interface. This is really just preventative maintenance and good practice since our object is exposed to COM.

Loading an Assembly

At this point, let's examine the Gmail.js file and take a look at how a .NET assembly is used from the gadget. The first thing to do is create and initialize an instance of the GadgetBuilder wrapper found in the GadgetInterop.js file. We'll use that wrapper to load and unload .NET types.

var builder = new GadgetBuilder();

Calling the Initialize method does a few important tasks. First, it checks if the Gadget Adapter is already registered by trying to create an ActiveX object instance of it. If that fails, the builder attempts to run the registration code listed above. The beauty here is that you never need to manually register the Gadget Adapter COM object. The JavaScript library will do it for you the first time it's run.

function Initialize()
   if(InteropRegistered() == false)

    _builder = GetActiveXObject();

Putting It All Together

The next step is to load the GmailReader assembly and create an instance of the client type. The GmailClient has two constructor arguments, userName and password which are required to create an instance. The values for both arguments come from the gadget's settings page, and are stored using the Gadget API, so once they exist for the lifetime of the gadget.

Gadget Settings

gmailClient = builder.LoadType(System.Gadget.path +
             "\\bin\\GmailReader.dll", "GmailReader.GmailClient");

We're telling the builder to load the GmailReader.dll assembly located in the gadget's bin directory. There's no need to put your assembly in a "bin" directory, or even under the same folder structure as your gadget. I simply did that for convenience in this example.

At this point, the gmailClient JavaScript variable holds a reference to a fully-loaded .NET GmailClient type. Now we can directly invoke the objects methods just like you would in managed code. To get enough information to display something meaningful on the gadget UI, we can call the following code:

var count = gmailClient.UnreadMailCount;
var mailLink = document.getElementById('mailCountLink');
mailLink.innerText = count;

New Mail

Notice that except for the var data type, it's no different that a .NET equivalent. In other words, you now have full access to any method or property you want to expose in your own object, and there's no further COM work to do. This is true for any assembly. With the Gadget Adapter in place, you don't have to do any COM work again.

Even though we're working with an inferred type, var, once we get a value back from the .NET code, it can be used like any other JavaScript value. In this case, the number of unread mail items is displayed to the user, and the and the background is changed to reflect no mail or new mail.

No Mail

Lastly, because the gmailClient is kept in memory, the mail contents can be displayed any time the user clicks on the unread mail count link. In other words, the .NET object maintains it (hence the need for manual cleanup). Here's how the details are displayed in the gadget.

Mail Details


That's really all there is to it. Once your object is created, you can use it as if you were calling it from managed .NET code. Also, because the interop layer is registered after the first time you run your gadget, it's reusable across all your gadgets. The best part is that you can package your assembly, the interop assembly, and the interop JavaScript library with your gadget, and Vista will handle the entire install process just like any other gadget.

It's unfortunate that Microsoft left managed code out of the gadget framework, especially when they have support for it in so many other areas. Still, the truth is that managed code can still be easily used, so there's still hope for some really useful gadget development.



This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Web Developer PageLabs
United States United States
I'm the founder of PageLabs, a web-based performance and SEO optimization site.

Give your site a boost in performance, even take a free speed test!

Comments and Discussions

QuestionGetting Object Error Pin
muneebalikiyani19-Mar-13 0:40
Membermuneebalikiyani19-Mar-13 0:40 
QuestionDownloaded Source Code... How do you get it to run on Windows 7 64bit? Pin
Warren Decker18-May-11 15:31
MemberWarren Decker18-May-11 15:31 
GeneralIf i compile the source i get no MailCount Pin
pinkjones31-Oct-10 8:20
Memberpinkjones31-Oct-10 8:20 
QuestionDid somebody solved the Uninstall Problem? Pin
pinkjones31-Oct-10 8:15
Memberpinkjones31-Oct-10 8:15 
GeneralReturning string array Pin
gyr31-Jul-09 3:03
Membergyr31-Jul-09 3:03 
GeneralRe: Returning string array Pin
TylerBrinks31-Jul-09 3:30
MemberTylerBrinks31-Jul-09 3:30 
GeneralRe: Returning string array Pin
gyr31-Jul-09 18:22
Membergyr31-Jul-09 18:22 
GeneralCannot Uninstall it Pin
mrcouthy11-May-09 20:14
Membermrcouthy11-May-09 20:14 
GeneralRe: Cannot Uninstall it Pin
TylerBrinks12-May-09 4:38
MemberTylerBrinks12-May-09 4:38 
GeneralA very urgent question, how to expose the .net object's event Pin
chinese_zmm22-Feb-09 23:00
Memberchinese_zmm22-Feb-09 23:00 
General!(TopMost) Pin
Siavash Mortazavi12-Dec-08 6:23
MemberSiavash Mortazavi12-Dec-08 6:23 
GeneralHaving multiple dll to reference Pin
Erwin Lee28-Oct-08 20:19
MemberErwin Lee28-Oct-08 20:19 
GeneralVBScript in Sidebar Gadgets Pin
thomastmc26-Jun-08 18:30
Memberthomastmc26-Jun-08 18:30 
GeneralRe: VBScript in Sidebar Gadgets Pin
TylerBrinks27-Jun-08 4:23
MemberTylerBrinks27-Jun-08 4:23 
GeneralFix for Unloading Problem Pin
xype28-Apr-08 4:18
Memberxype28-Apr-08 4:18 
Generalgreat apps - but... Pin
Roberto_Rwk19-Apr-08 12:27
MemberRoberto_Rwk19-Apr-08 12:27 
GeneralRe: great apps - but... Pin
TylerBrinks21-Apr-08 10:38
MemberTylerBrinks21-Apr-08 10:38 
GeneralRe: great apps - but... Pin
Roberto_Rwk8-May-08 23:59
MemberRoberto_Rwk8-May-08 23:59 
Generallicensing for commercial use Pin
xype29-Mar-08 3:00
Memberxype29-Mar-08 3:00 
GeneralRe: licensing for commercial use Pin
TylerBrinks21-Apr-08 10:35
MemberTylerBrinks21-Apr-08 10:35 
QuestionInteropTest.htm Pin
Doncp14-Feb-08 7:30
MemberDoncp14-Feb-08 7:30 
GeneralRe: InteropTest.htm Pin
TylerBrinks19-Feb-08 11:36
MemberTylerBrinks19-Feb-08 11:36 
Generalbrilliant but not working... 'Automation server can't create object' Pin
mightyCoCo4-Jan-08 21:15
MembermightyCoCo4-Jan-08 21:15 
GeneralRe: brilliant but not working... 'Automation server can't create object' Pin
mightyCoCo4-Jan-08 22:15
MembermightyCoCo4-Jan-08 22:15 
GeneralRe: brilliant but not working... 'Automation server can't create object' Pin
TylerBrinks6-Jan-08 6:00
MemberTylerBrinks6-Jan-08 6:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.