Csharp/C#教程:C#:引用Windows shell界面分享


C#:引用Windows shell界面

我是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提供了一个标准的COM对象来在Windows 7上执行它。您可以通过执行以下操作来创建实例:

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

感谢您的指导。 对于更直接的帮助,这对我有用:

上述就是C#学习教程:C#:引用Windows shell界面分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

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 FilesWindows SidebarGadgetsxxxxxxxxx.Gadget"); } } } 

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/1006441.html

(0)
上一篇 2021年12月29日
下一篇 2021年12月29日

精彩推荐