![](/img/trans.png)
[英]Interface referencing another interface but class referencing another class in C#
[英]C#: Referencing a windows shell interface
我对C#还是很陌生,我正在尝试完成一个我正在进行的小项目,该项目使用少量的C#代码来协助Windows桌面小工具的开发。 基本上,我正在尝试实现IDesktopGadget
接口,以便可以使用RunGadget方法。
这是到目前为止我还没有阅读有关类似接口的信息:
[ComImport]
[Guid("C1646BC4-F298-4F91-A204-EB2DD1709D1A")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IDesktopGadget
{
uint RunGadget([MarshalAs(UnmanagedType.LPWStr)] string gadgetPath);
}
不幸的是,当我尝试从中创建对象时出现错误: "Cannot create an instance of the abstract class or interface 'GadgetTask.IDesktopGadget'"
有人可以指出我正确的方向,也许可以帮助我同时了解我在做什么错?
为了使用该接口,您实际上需要DesktopGadget对象的实现。 MS提供了在Windows 7上执行此操作的标准COM对象。您可以通过执行以下操作来创建实例:
Type t = Type.GetTypeFromCLSID(new Guid("924ccc1b-6562-4c85-8657-d177925222b6")); IDesktopGadget dg = (IDesktopGadget)Activator.CreateInstance(t);
感谢您的指导。 对于更直接的帮助,这对我有用:
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);
}
}
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");
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.