Monday, December 04, 2006

Sidebar Gadget

Recently, I had to create a Windows Vista Sidebar Gadget which pulls out data from a database locally installed on the machine and displays it to the user in a eye-catching manner.

Being new to the .Net and given that my past projects had more or less revolved around UNIX and C, I had to do a lot of research. Although there was a lot of information available on the internet on how to develop a Vista Sidebar there was practically none on how to access data from a managed piece of code from within Jscript. I discovered two ways to do it and in the following section I'll describe precisely that.

For the uninitiated, a Vista Sidebar is nothing but a bunch of HTML and Jscript files packed together. That is it. For more information on how to develop a Sidebar Gadget, please refer to this page.

First way:

AxComp.cs
using System;
using System.Runtime.InteropServices;
namespace Foo.Something

{
  public interface Ix
  {
    string CallMe();
  }
  public class Bar: Ix

  {
    public string CallMe()
    {
      return "Friendly string.";
    }
  }
}


Run.bat
@echo.
@echo To be run from an administrator command prompt
@echo.
csc /nologo /t:library AxComp.cs
regasm AxComp.dll /nologo /codebase
TestActiveXControl.html


Regasm is the Assembly Registration tool that reads the metadata within an assembly and adds the necessary entries to the registry, which allows COM clients to create .NET Framework classes transparently. Once a class is registered, any COM client can use it as though the class were a COM class. The class is registered only once, when the assembly is installed. Instances of classes within the assembly cannot be created from COM until they are actually registered.

To test it out, define a webpage as shown below:
TestActiveXControl.html
<html>
  <title>
    Test ActiveX Control
  </title>
  <script language="JAvaScript">
    var obNewActiveXComponent = new ActiveXObject("Foo.Something.Bar");
    alert(obNewActiveXComponent.CallMe());
  </script>
  <body>
    <hr>
    <h3>Communicating with managed APIs from Javascript</h3>
    <hr>
  </body>
</html>


If you want to explore all the technologies which enable managed/unmanaged interaction, this page would be helpful.

Watch out this space for more information...

1 comment:

VJ said...

So u finally figured that one out :)

I've added you to my blog roll :)