Ralph's profileThe Gadget WorkshopBlogLists Tools Help

The Gadget Workshop

Turing your ideas into practical software

Ralph Lear

Occupation
Location
Interests
I am an american living in Germany. I've been professionaly engaged in computer technology and programming for over 25 years.

I grew up and spent my youth living in Seaside, Oregon.
The SidebarRadio is a small but powerful gadget for documenting and playing your favorite radio station's audio stream directly from the Vista sidebar. The Radio is now available as version 4 incorporating several new features.
PasswordShield is a sidebar gadget used to store and protect your passwords and login information.

Sideboard Gadgets that talk to C# Modules

Often I've seen the questioned posted "How can my gadget communicate with C#?"  

Actually the steps needed to get your gadget communicating with C# are actually quite simple.  You create your module, you test it, you create your gadget files, you export your .dll to the gadget's files and then prepare your gadget for COM operations.

But why do you want to do this?  There are actually several reasons: you might want to hide parts of your code, you may want to perform something not available to web services or maybe just for fun.
Whatever your reason might be; here's how.

I'm not going to explain to you why each step is needed.  After all, you use your microwave with out knowing how it works. I assume that you already have a working knowledge of constructing a gadget and are familiar with C#.   Furthermore, I recommend Visual C# Express and Visual Web Developer.

  1. Create a new Class Library Project to hold and test our C# Module
    Create a new project. newproject
    Choose the "Class Library" template and enter a valid name for your library.  For instance mine test is called "gadgetClassLibrary". classlib

  2. Set your project properties to COM-Visible
    On the Explorer toolbar, choose "Project" and then "Properties". properties
    On the properties window, choose the tab "Application".  Click on the button "Assembly Information".   asminfo
    On the Information window, check the "Make assembly COM visible" check box.  Also note the GUID.  The GUID will be needed later. comvis

  3. Code your C# module
    In the "Solution Explorer", rename the class that was automatically created to a name which identifies your module.  For instance my module will be named "mygadgetDLL.cs". rename
    Add a using statement for "System.Runtime.InteropServices" to your module.
    Add a "COM-visible" directive between the namespace and the class statement.  The GUID is the ID as described in step 2.  The ProgID is the namespace name "." the class name.
    Add the keyword "partial" to the class.

    Code the method  (class) that will be called from your gadget.  My method is called "greetings"  This method accepts a string value from the gadget and returns a string value back to the gadget.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Runtime.InteropServices;

    namespace gadgetClassLibrary
    {
        [ComVisible(true),
         Guid("c30987fe-5374-4f38-812b-82f9ceeaaaf7"),
         ProgId("gadgetClassLibrary.mygadgetDLL"),
         ClassInterface(ClassInterfaceType.None)]
       
        public partial class mygadgetDLL
        {
            public string greetings(string name)
            {
                return "Hello " + name;
            }
        }
    }
  4. Testing your module
    If you have ever written a gadget, then you know this is where Microsoft got lazy.  Without Visual Studio Debugger it is nearly impossible to debug your C# code from a gadget.  That is why I suggest adding a second project (Windows or Console) for the sole purpose of testing your code before calling it from a gadget.   Test each of the methods in your gadget's class.  Make sure everything executes correctly.  
    In the "Solution Explorer", right click on the solution choose "Add" then "New Project".
    Choose "Console Application" and give your new class a name.  For instance: "testClass".   You could also implement a "Windows Application".
    testproject
    In the "Solution Explorer", right click on the project and choose "Set as Startup Project". startup
    In the "Solution Explorer", right click on the project and choose "Project Dependencies".
    Check the box next to your DLL's project name.
    depends
    In the "Solution Explorer", right click on the project and choose "Add Reference".  Choose "Projects" and select your gadget's class project. reference
    Now edit the program and add the code to reference the gadget's class and the method in that class that we wish to call.  Remember, in my example, my method was called "greetings".  It accepted a string and returned a string.

    using gadgetClassLibrary;
    namespace testDLLClass
    {
        class Program
        {
            static void Main(string[] args)
            {
                mygadgetDLL myClass = new mygadgetDLL();
                string answer = myClass.greetings("ralph");
                Console.WriteLine(answer);
                Console.Read();
            }
        }
    }
    Execute the program to test the module. execute
       
  5. Making your gadget COM-ready
    Create a folder called "binaries" in your gadgets folder. Copy the  C# class .dll file from your Class Library Projects's "Debug" folder into the new gadget's "binaries" folder.  You have to repeat this each time you change your .dll.  Also, you may need to stop and start the sideboard each time you make a change to the .dll.

    Add the "interop" script to your gadget.  Download the script and add it to your scripts folder.  Link the script file at the top of your "gadget.html". Following the script file add the script to register your .dll.  Enter the Namespace Name, Class Name and GUID.

    <script language="javascript" type="text/javascript" src="../scripts/interop.js"></script>
    <script language="javascript" type="text/javascript">
    var comNSpc = "gadgetClassLibrary";    // Namespace
    var comCls     = "mygadgetDLL";     // Class name
    var comGuid      = "c30987fe-5374-4f38-812b-82f9ceeaaaf7"; // GUID
    if (!activateDLL("HKCU"))
        if (!activateDLL("HKLM"))
            prompt("E R R O R","Error creating ActiveX object");
    </script>

    You can now begin referencing your C# module.  The objects name is "myDLL".  For instance to call my method greetings I would code:

    var text = myDLL.greetings("Ralph");

    Thats It !
 
Here a list of links containg further information to developing Sidebar gadgets:
A list of useful forums dealing with gadget development