These days I’ve participated in the contest Gadgetizate which goal was to build a Sidebar Gadget.
As you know the gadgets should be coded using Html and script and you have also access to a wide variety of namespaces, but there are some cases in which you want more. There are some tricks to expand his functionality like embedding WPF applications or using ActiveX.
With .NET we can build easily ActiveX and/or COM components. Next you can see how to do it.
The first thing you need to do when you create your project is go to the "Build" properties tab and mark the option "Register for COM interop". This indicates that your application will expose a COM object and Visual Studio will register automatically for you. You can do that later on by using the tool Regasm.exe.
There are some requirements for the classes to be able to use .NET objects as COM:
- The class, properties, methods and events must be public
- The class must implement the interface with the members that will be exposed, methods not defined in the interfaces will not be exposed as COM.
With the next code you will be ready to create your first ActiveX component.
Notice that the interface and the class are marked with the GuidAttribute, these are the Guid’s that will be used to identify your classes, this is an optional attribute since the tool TlbImp.exe creates for you if the attribute is not set, but assigning manually gives you more control over the ActiveX, since you can then use it to locate, load …
If you pay attention to the interface you will see that the method has also an attribute, the DispId, this as you can imagine, assigns a dispatch id to the method HelloWorld, in order it can be accessed through the IDispatch interface.