如何通过CLSID在C#中实例化COM对象? [英] How do I go about instantiating a COM Object in C# by CLSID?

查看:483
本文介绍了如何通过CLSID在C#中实例化COM对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

原谅我的术语,请原谅我,因为这对我来说是一个未知的领域.

Forgive me if my terminology is off, as this is somewhat uncharted territory for me.

我有一个需要创建FolderShortcut的程序. Microsoft有文档CoCreateInstance函数需要以CLSID_FolderShortcut作为参数来调用,我推断这意味着它正在实例化COM对象.该对象的CLSID为{0AFACED1-E828-11D1-9187-B532F1E9575D}.

I have a program which needs to create a FolderShortcut. Microsoft has documentation on how to create it in C++, and I'm trying to translate the directions to C#. The instructions state that the CoCreateInstance function needs to be called with CLSID_FolderShortcut as a parameter, which I infer to mean that it's instantiating a COM object. The CLSID for this object is {0AFACED1-E828-11D1-9187-B532F1E9575D}.

我尝试从COM选项卡添加对 Shell32.dll 的引用,但是FolderShortcut对象未显示在Intellisense中(也许不在typelib中?).我也考虑过要做DLLImport,但是,当然,这只允许我访问函数,而不是对象.

I've tried adding a reference to Shell32.dll from the COM tab, but the FolderShortcut object does not show up in Intellisense (maybe it's not in the typelib?). I also thought about doing a DLLImport, but, of course, that only gives me access to functions, not objects.

我需要怎么做才能在.Net中访问此对象?

What do I need to do to get access to this object in .Net?

推荐答案

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

Here is a piece of code that allows you to create a folder shortcut. The CoCreateInstance can (in general) be replaced by declaring a simple class decorated with the Guid attribute with the required CLSID, and the ComImport attribute. The new call will do the COM magic automatically. With this code, you don't even need a Shell32 reference (or you can reuse the IShellLink declaration from there if you prefer).

用法:

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);
}

这篇关于如何通过CLSID在C#中实例化COM对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆