Can I connect a .NET 5 COM interop object with VB6?
See the question and my original answer on StackOverflow.NET Framework, .NET Core 3, and .NET 5/6/+ can expose a COM object to the COM world. This world includes VB6 and everything that can do COM.
a) Create a .NET 5 "NetClassLibrary" Class Library project (using Visual Studio for example)
b) Add a NetComObject.cs
file with the following code for example:
using System;
using System.Runtime.InteropServices;
namespace NetClassLibrary
{
[ComVisible(true)]
[Guid("cfb2ee8b-f2ec-447c-9f0d-ac19c9a6cc26")] // TODO: change this GUID
public class NetComObject
{
public string SayHello() => "Hello World from .NET " + RuntimeInformation.FrameworkDescription;
}
}
c) Modify the .csproj to set the EnableComHosting
property to true
and make sure you compile for x86 as VB6 is a 32-bit program (note if you use Excel's 64-bit VBA, not VB6, you must compile for 64-bit), like this for example:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<EnableComHosting>true</EnableComHosting>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<PlatformTarget>x86</PlatformTarget>
</PropertyGroup>
</Project>
d) Compile, start an admin prompt and run this:
regsvr32 yourPathTo\NetClassLibrary.comhost.dll
e) Create a VB6 project, add this VB6 code:
Private Sub Form_Load()
Dim obj As Object
Set obj = CreateObject("new:{cfb2ee8b-f2ec-447c-9f0d-ac19c9a6cc26}") // TODO: use the same GUID!
MsgBox obj.SayHello()
End Sub
f) Run
Note 1: you can use a ProgId attribute too, like COM object often do, you don't have to stick to Guids only...
Note 2: I've only used IDispatch
("Lazy Loading") interface for this demonstration but you can use IUnknown
-derived interfaces ("Early Loading") and TLBs too. Unfortunately .NET 5 doesn't create the .TLB for you, you'll have to build it by yourself or create it with another tool and import it in .NET and/or in VB6.