Using .NET Components from Vista Sidebar Gadgets

Posted by Andrew on April 04, 2007

Sometimes, when developing a non-trivial gadget, you need the full power of good old .NET. Fear not, with just a little bit of tasty COM Interop, .NET components can be successfully used from a gadget.

STEP 1 - Create your .NET component
[ComVisible(true)]
public interface IMyComponent
{
  void DoStuff();
}
 
[Guid("31267db9-1912-4524-a757-4224b70d0282")]
[ProgId("Mindscape.MyComponent")]
[ClassInterface(ClassInterfaceType.None)]
[ComVisible(true)]
public sealed class MyComponent : IMyComponent
{
  void DoStuff()
  {
    // yep, doing stuff...
  }
}
STEP 2 - Register your .NET component

First, ensure your assembly is signed.

Either:

  • Run regasm.exe <My.DotNet.dll> /codebase. Or,
  • Or check “Register for COM Interop” in the Project Properties/Build tab. Or,
  • Include gadget code to self-register the component. More to come on this…
STEP 3 - Consume your component from the gadget

Either:

Create your component using new ActiveXObject:

var myComponent = new ActiveXObject("Mindscape.MyComponent");
myComponent.DoStuff();

Create your component using the object tag. This seems to be required to have the gadget handle events from the component. More to come on this.

<object id="myComponent" 
        classid="clsid:31267db9-1912-4524-a757-4224b70d0282"></object>

In upcoming posts I plan to cover in a bit more detail event handling and component self-registration.

Hope this helps.

Trackbacks

Use this link to trackback from your own site.

Comments

Leave a response

  1. James BNo Gravatar Thu, 05 Apr 2007 21:50:05 CDT

    That’s nice, but you don’t actually need to REGISTER the object.

    (and I’m betting a lot of sys admins would be happy if you don’t)

    http://www.c-sharpcorner.com/UploadFile/dsandor/ActiveXInNet11102005040748AM/ActiveXInNet.aspx

    Illustrates how to host your object without registering the DLL.

  2. AndrewNo Gravatar Wed, 11 Apr 2007 17:14:05 CDT

    Thanks James. But can you use this approach without messing with CAS?

Comments