I have a COM add-in written in C#, that works perfectly fine with Microsoft Office.
There was a GitHub issue asking if the add-in supported the VB6 IDE; is there anything different I need to do, to make it work with Visual Studio 6.0?
Here's the relevant class listing:
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();
}
}
}
}
The casts to VBE
and AddIn
types simply blow up. The assembly is registered and works exactly as intended in Office.
I'm having a bit of a hard time finding documentation about extending VB6, but my understanding is that the interfaces involved are the same - and by attaching to the VB6 process, breaking just before the casts, and inspecting the Application
object, I can see all the members I'm expecting to see there.
Why is it not working then? Here are the relevant references from the project file:
<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>