0

I'm pretty new to C#, I'm trying to complete a little side project I've been working on that uses a small amount of C# code to assist the development of a Windows Desktop Gadget. Basically, I'm trying to implement the IDesktopGadget interface so that I can use the RunGadget method.

Here's what I got so far from reading information about similar interfaces:

[ComImport]
[Guid("C1646BC4-F298-4F91-A204-EB2DD1709D1A")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IDesktopGadget
{
    uint RunGadget([MarshalAs(UnmanagedType.LPWStr)] string gadgetPath);
}

Unfortunately, I get an error when I try and create an object from it: "Cannot create an instance of the abstract class or interface 'GadgetTask.IDesktopGadget'"

Can someone point me in the right direction and maybe help me understand what I'm doing wrong at the same time?

Andy E
  • 338,112
  • 86
  • 474
  • 445

2 Answers2

1

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);
tyranid
  • 13,028
  • 1
  • 32
  • 34
  • Thanks for the answer. Am I supposed to replace the Guid there with the class id of the interface? – Andy E Dec 17 '09 at 22:54
  • I get a "class not registered error" and I'm not sure why. I plucked the class id of the interface right out of my registry. – Andy E Dec 17 '09 at 23:10
  • No, the Guid is the CLSID of the object not the interface. I got that Guid from the CLSID_DesktopGadget definition in the SDK headers. – tyranid Dec 17 '09 at 23:35
  • Thanks I'll check into it some more then! – Andy E Dec 21 '09 at 13:49
  • Thanks for your help so far... I get a new error now: `System.InvalidCastException: Unable to cast COM object of type 'System.__ComObject' to interface type 'IDesktopGadget'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{924CCC1B-6562-4C85-8657-D177925222B6}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).` – Andy E Dec 21 '09 at 14:19
  • Now you have managed to go the opposite way. Leave the Guid attribute in the interface definition as C1646BC4-F298-4F91-A204-EB2DD1709D1A and the 924CCC1B-6562-4C85-8657-D177925222B6 Guid as the one you use to create the object. You need to understand how COM works abit better. There are two main uses of Guids (there are more but irrelevant), the CLSID and the IID. The CLSID (Class ID) indicates a specific implemented object on the system. That is what the code needs. The IID indicates a unique ID for an interface which can talk to the object. A single CLSID might implemented multiple IIDs – tyranid Dec 21 '09 at 16:52
  • Ahh silly me. It works great now, thank you. Working with COM objects is so much easier in Javascript/HTML. – Andy E Dec 22 '09 at 16:01
0

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");
        }
   }
}
Jason
  • 2,113
  • 2
  • 14
  • 7