我是一名有用的助手,可以为您翻译文本。
我有一个用C#编写的COM add-in,在Microsoft Office中运行得非常好。
有一个GitHub问题询问该插件是否支持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();
}
}
}
}
将VBE
和AddIn
类型的强制转换会导致程序崩溃。这个程序集已经注册并且在Office中按照预期运行。
我发现扩展VB6的文档比较难找,但是我的理解是所涉及的接口是相同的——通过附加到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>