Ralph's profileThe Gadget WorkshopBlogLists Tools Help

Blog


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 !

Comments (2)

Please wait...
Sorry, the comment you entered is too long. Please shorten it.
You didn't enter anything. Please try again.
Sorry, we can't add your comment right now. Please try again later.
To add a comment, you need permission from your parent. Ask for permission
Your parent has turned off comments.
Sorry, we can't delete your comment right now. Please try again later.
You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
Complete the security check below to finish leaving your comment.
The characters you type in the security check must match the characters in the picture or audio.

To add a comment, sign in with your Windows Live ID (if you use Hotmail, Messenger, or Xbox LIVE, you have a Windows Live ID). Sign in


Don't have a Windows Live ID? Sign up

Hi,

I want to use User32.dll's GetDesktopWindow() and GetForeGroundWindow() methods in my gadget to have somekind of desktop focus control for my gadget flyouts. Can you help me out in this. I tried creating a Wrapper class but i get an error when i create ActiveX Object in my Javascirpt without any error description. Can you mail me an sample example of how my Wrapper class would be like, i think here i am wrong.

Thanks,
Shreyas(mail id: bhagatshreyas@gmail.com)
Mar. 18
Hi!

I have 2 problems with  my Gadget. It's a windows service contoler gadget and each time i call a c# method from the Javascript  the gadget is disappear and appear.and a string method don't return with value, but in a win form application works.I dont know why the gadget do this. pls help
Mar. 27

Trackbacks

The trackback URL for this entry is:
http://ralphlear.spaces.live.com/blog/cns!56195B06E71F77B3!229.trak
Weblogs that reference this entry
  • None