gpt4 book ai didi

c# - 无法将 COM 对象转换为 Microsoft.Vbe.Interop.VBE

转载 作者:行者123 更新时间:2023-11-30 16:55:36 26 4
gpt4 key购买 nike

我有一个 COM add-in written in C# ,与 Microsoft Office 完美配合。

有一个GitHub issue asking if the add-in supported the VB6 IDE ;我需要做些什么才能使其与 Visual Studio 6.0 一起工作?

这是相关的类列表:

using System;
using Extensibility;
using Microsoft.Vbe.Interop;
using System.Runtime.InteropServices;
using System.ComponentModel;
using System.Windows.Forms;

namespace Rubberduck
{
[ComVisible(true)]
[Guid(ClassId)]
[ProgId(ProgId)]
[EditorBrowsable(EditorBrowsableState.Never)]
//underscores make classes invisible to VB6 object explorer
//Nothing breaks because we declare a ProgId
public class _Extension : IDTExtensibility2, IDisposable
{
public const string ClassId = "8D052AD8-BBD2-4C59-8DEC-F697CA1F8A66";
public const string ProgId = "Rubberduck.Extension";

private App _app;

public void OnAddInsUpdate(ref Array custom)
{
}

public void OnBeginShutdown(ref Array custom)
{
}

public void OnConnection(object Application, ext_ConnectMode ConnectMode, object AddInInst, ref Array custom)
{
try
{
// these casts fail when host is VB6 environment
_app = new App((VBE)Application, (AddIn)AddInInst);
}
catch (Exception exception)
{
MessageBox.Show(exception.Message, "Rubberduck Add-In could not be loaded", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}

public void OnStartupComplete(ref Array custom)
{
if (_app != null)
{
_app.CreateExtUi();
}
}

public void OnDisconnection(ext_DisconnectMode RemoveMode, ref Array custom)
{
Dispose();
}

public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing)
{
if (disposing & _app != null)
{
_app.Dispose();
}
}
}
}

VBEAddIn 类型的强制转换完全失败了。该程序集已注册并且在 Office 中完全按预期工作

我很难找到有关扩展 VB6 的文档,但我的理解是所涉及的接口(interface)是相同的 - 并且通过附加到 VB6 进程,在强制转换之前中断,并检查 Application 对象,我可以看到我希望在那里看到的所有成员。

为什么它不工作呢?以下是项目文件中的相关引用资料:

<Reference Include="extensibility, Version=7.0.3300.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<EmbedInteropTypes>True</EmbedInteropTypes>
</Reference>

...

<COMReference Include="VBIDE">
<Guid>{0002E157-0000-0000-C000-000000000046}</Guid>
<VersionMajor>5</VersionMajor>
<VersionMinor>3</VersionMinor>
<Lcid>0</Lcid>
<WrapperTool>primary</WrapperTool>
<Isolated>False</Isolated>
<EmbedInteropTypes>True</EmbedInteropTypes>
</COMReference>

最佳答案

问题是 VBA 有自己的 VBIDE 类型库,VB6 也有自己的 VBIDE 类型库。这两个类型库非常相似(尽管 VB6 版本更丰富),并且都公开了 IDTExtensibility2 接口(interface)。此外,由于 IDTExtensibility2 是随 VB6 和 Office 2000 的 VBA 6.x 一起引入的,因此还有用于 VB5 的 VBIDE 版本,而且我认为,对于 Office 97 的 VBA 5.x,它们公开了原始的 IDT可扩展性

与 Hans Passant 的回答相反,该加载项不是 Office 加载项,但实际上是 VBE 加载项,因此它可以在任何支持 VBA 6/7 的主机(Office、AutoCAD、CorelDraw、 SolidWorks 等),它也绝对可以在 VB6 中工作...只是需要多做一些工作。

如果您想支持 VB6(或 VB5 或 VBA 5.x),则需要添加对相关 Interop 程序集的引用,并且您需要转换为适当的连接类型。

我发布了一个proof of concept on CodeReview支持 VB6 和 VBA 6.x\7.x。它远非完整的实现,但您会看到我创建了一个 IVBE 接口(interface)来抽象不同的 VBIDE 实现。

关于c# - 无法将 COM 对象转换为 Microsoft.Vbe.Interop.VBE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29294260/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com