個人檔案The Gadget Workshop部落格清單 工具 說明

部落格


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 !

回應 (2)

請稍候...
很抱歉,您輸入的回應過長。請縮短您的回應。
您尚未輸入內容,請再試一次。
很抱歉,目前無法新增您的回應,請稍後再試。
若要新增回應,您的父母必須先給您權限。要求權限
您的家長已關閉回應功能。
很抱歉,目前無法刪除您的回應,請稍後再試。
您已超過每日回應上限次數,請於 24 小時後再試一次。
由於系統顯示您可能傳送垃圾郵件給其他使用者,因此您帳號中的回應功能已遭停用。 如果您認為自己帳號遭錯誤停用,請連絡 Windows Live 支援
請完成下列安全檢查,以完成回應。
您輸入的安全檢查字元必須與圖片或音訊中的字元相符。

若要新增回應,請以您的 Windows Live ID 登入 (若您使用 Hotmail、Messenger 或 Xbox LIVE,則您已擁有 Windows Live ID)。登入


沒有 Windows Live ID?註冊

bhagatshre​yas撰寫:
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)
3 月 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
3 月 27 日

引用通告

此內容的引用通告是:
http://ralphlear.spaces.live.com/blog/cns!56195B06E71F77B3!229.trak
引述這則內容的部落格