加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

如何通过CLSID在C#中实例化COM对象?

发布时间:2020-12-16 00:22:33 所属栏目:百科 来源:网络整理
导读:如果我的术语已经关闭,请原谅我,因为这对我来说是一个有点未知的领域. 我有一个程序需要创建FolderShortcut.关于如何在C中创建它的Microsoft has documentation,我正在尝试将方向转换为C#.指令声明需要使用CLSID_FolderShortcut作为参数调用CoCreateInstance
如果我的术语已经关闭,请原谅我,因为这对我来说是一个有点未知的领域.

我有一个程序需要创建FolderShortcut.关于如何在C中创建它的Microsoft has documentation,我正在尝试将方向转换为C#.指令声明需要使用CLSID_FolderShortcut作为参数调用CoCreateInstance函数,我推断这意味着它实例化了一个COM对象.该对象的CLSID是{0AFACED1-E828-11D1-9187-B532F1E9575D}.

我尝试从COM选项卡添加对Shell32.dll的引用,但是FolderShortcut对象没有出现在Intellisense中(也许它不在typelib中?).我还想过做一个DLLImport,但是,当然,这只能让我访问函数,而不是对象.

要在.Net中访问此对象,我需要做什么?

解决方法

这是一段代码,允许您创建文件夹快捷方式. CoCreateInstance可以(通常)通过声明一个用 Guid属性修饰的简单类来替换,该类具有所需的CLSID和 ComImport属性.新的调用将自动执行COM魔术.使用此代码,您甚至不需要Shell32引用(或者如果您愿意,可以从那里重用IShellLink声明).

用法:

static void Main(string[] args)
{
    CreateFolderShortcut(@"c:temp",Path.GetFullPath("Shortcut to Temp"));
}

码:

public static void CreateFolderShortcut(string path,string shortcutPath)
{
    CreateFolderShortcut(path,shortcutPath,null);
}

public static void CreateFolderShortcut(string path,string shortcutPath,string comment)
{
    if (path == null)
        throw new ArgumentNullException("path");

    IShellLink link = (IShellLink)new ShellLinkFolder();

    if (comment != null)
    {
        link.SetDescription(comment);
    }
    link.SetPath(path);

    IPersistFile file = (IPersistFile)link;
    file.Save(shortcutPath,false);
}

[ComImport]
[Guid("00021401-0000-0000-C000-000000000046")]
private class ShellLink
{
}

[ComImport]
[Guid("0AFACED1-E828-11D1-9187-B532F1E9575D")]
private class ShellLinkFolder
{
}

[ComImport]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("000214F9-0000-0000-C000-000000000046")]
private interface IShellLink
{
    void GetPath([Out,MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszFile,int cchMaxPath,out IntPtr pfd,int fFlags);
    void GetIDList(out IntPtr ppidl);
    void SetIDList(IntPtr pidl);
    void GetDescription([Out,MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszName,int cchMaxName);
    void SetDescription([MarshalAs(UnmanagedType.LPWStr)] string pszName);
    void GetWorkingDirectory([Out,MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszDir,int cchMaxPath);
    void SetWorkingDirectory([MarshalAs(UnmanagedType.LPWStr)] string pszDir);
    void GetArguments([Out,MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszArgs,int cchMaxPath);
    void SetArguments([MarshalAs(UnmanagedType.LPWStr)] string pszArgs);
    void GetHotkey(out short pwHotkey);
    void SetHotkey(short wHotkey);
    void GetShowCmd(out int piShowCmd);
    void SetShowCmd(int iShowCmd);
    void GetIconLocation([Out,MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszIconPath,int cchIconPath,out int piIcon);
    void SetIconLocation([MarshalAs(UnmanagedType.LPWStr)] string pszIconPath,int iIcon);
    void SetRelativePath([MarshalAs(UnmanagedType.LPWStr)] string pszPathRel,int dwReserved);
    void Resolve(IntPtr hwnd,int fFlags);
    void SetPath([MarshalAs(UnmanagedType.LPWStr)] string pszFile);
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读