使VSIX软件包自动化对象com可见withou手动recasm重新恢复

发布于 2025-02-09 02:18:32 字数 2017 浏览 0 评论 0 原文

我正在处理一个可以提供自动化对象的Visual Studio软件包,后来可以通过COM和DTE.GetObject(字符串名称)调用在第三方应用程序中使用。

为此,我创建了以下设置。 在Visual Studio实例中,我有以下VSIX软件包运行。

namespace CustomAutomationObjectProvider
{
    [Guid("5B152019-535C-4602-9084-3F5250118A7D")]
    public interface ICustomAutomationObject
    {
        string GetCustomString();
    }

    [ComVisible(true)]
    public class CustomAutomationObject : ICustomAutomationObject
    {
        public string GetCustomString()
        {
            return "Hello from automation object!";
        }
    }
    
    [PackageRegistration(UseManagedResourcesOnly = true, AllowsBackgroundLoading = true)]
    [Guid("61C27A04-A031-4DF6-8E48-0B4BFEB8F49B")]
    [ProvideAutomationObject("CustomAutomationObject")]
    public sealed class AutomationObjectProvider : AsyncPackage
    {
        private CustomAutomationObject _instance;

        protected override object GetAutomationObject(string name)
        {
            if(name == "CustomAutomationObject")
            {
                if(_instance == null)
                {
                     _instance = new CustomAutomationObject();
                }
            }
            
            return _instance;
        }
    }
}

在第三方应用程序中,我要连接到Visual Studio的实例,获取其DTE对象,并尝试获取这样的自动化对象。我能够从getObject调用中获取一些对象,但是我无法将其施放到目标界面 - 它在E_NOInterface失败了,

[STAThread]
public static void Main(String[] args)
{
    ...
    ...

    var customAutomationObjectRaw = DTE.GetObject("CustomAutomationObject");
    var customAutomationObject = (CustomAutomationObjectProvider.ICustomAutomationObject)customAutomationObjectRaw;
}

我想我可能会错过解决方案或VSIX软件包配置中的一些COM可见度设置查找有关它的任何信息。我可以在RegasM.exe中注册汇编手册,但基于此线程

I'm working on a visual studio package that could provide automation object, which later could be used in third-party applications through COM and DTE.GetObject(string name) calls.

For this, I've created the following setup.
In a visual studio instance I have the following VSIX package running.

namespace CustomAutomationObjectProvider
{
    [Guid("5B152019-535C-4602-9084-3F5250118A7D")]
    public interface ICustomAutomationObject
    {
        string GetCustomString();
    }

    [ComVisible(true)]
    public class CustomAutomationObject : ICustomAutomationObject
    {
        public string GetCustomString()
        {
            return "Hello from automation object!";
        }
    }
    
    [PackageRegistration(UseManagedResourcesOnly = true, AllowsBackgroundLoading = true)]
    [Guid("61C27A04-A031-4DF6-8E48-0B4BFEB8F49B")]
    [ProvideAutomationObject("CustomAutomationObject")]
    public sealed class AutomationObjectProvider : AsyncPackage
    {
        private CustomAutomationObject _instance;

        protected override object GetAutomationObject(string name)
        {
            if(name == "CustomAutomationObject")
            {
                if(_instance == null)
                {
                     _instance = new CustomAutomationObject();
                }
            }
            
            return _instance;
        }
    }
}

In a third-party application, I'm connecting to instance of visual studio, getting its DTE object, and trying to get this automation object like this. I'm able to get some object from GetObject call, but I'm unable to cast it to the target interface - it fails with E_NOINTERFACE

[STAThread]
public static void Main(String[] args)
{
    ...
    ...

    var customAutomationObjectRaw = DTE.GetObject("CustomAutomationObject");
    var customAutomationObject = (CustomAutomationObjectProvider.ICustomAutomationObject)customAutomationObjectRaw;
}

I think I might miss some COM visibility setup in my solution or VSIX package configuration but I can't find any information on it. I'm able to register assembly manualy with regasm.exe, but based on this thread
How to make VSIX installer to register assembly automatically I assume there should be a way to do it automatically. Also, I was mainly using this one as a reference https://github.com/microsoft/PTVS/blob/main/Python/Product/Profiling/PythonProfilingPackage.cs

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文