C#: Referencing a windows shell interface

↘锁芯ラ 提交于 2019-12-01 14:40:15

You actually need an implementation of the DesktopGadget object in order to use the interface. MS provide a standard COM object to do it on Windows 7. You can create an instance by doing something like:

Type t = Type.GetTypeFromCLSID(new Guid("924ccc1b-6562-4c85-8657-d177925222b6"));
IDesktopGadget dg = (IDesktopGadget)Activator.CreateInstance(t);

Thanks for the guidance. For a little more straight forward help, this is what worked for me:

IDesktopGadget.cs

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

namespace GadgetActivator
{
    [ComImport]
    [Guid("C1646BC4-F298-4F91-A204-EB2DD1709D1A")]
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]

    interface IDesktopGadget
    {
        uint RunGadget([MarshalAs(UnmanagedType.LPWStr)] string gadgetPath);
    }
}

Program.cs

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


namespace GadgetActivator
{
    class Program
    {
        static void Main(string[] args)
        {
            Type t = Type.GetTypeFromCLSID(new Guid("924ccc1b-6562-4c85-8657-d177925222b6"));
            IDesktopGadget dg = (IDesktopGadget)Activator.CreateInstance(t);
            dg.RunGadget(@"C:\Program Files\Windows Sidebar\Gadgets\xxxxxxxxx.Gadget");
        }
   }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!