简体   繁体   English

我可以将 .NET 5 COM 互操作对象与 VB6 连接起来吗?

[英]Can I connect a .NET 5 COM interop object with VB6?

I have been doing some research on .NET Core COM interop and how to expose them .我一直在对 .NET Core COM 互操作以及如何公开它们进行一些研究。

Though I am having some issues coming up with a conclusion and understanding some concepts.尽管我在得出结论和理解一些概念时遇到了一些问题。 My boss told me they did some tests and they thought the only way to create C# code that could be called from within VB6 (including events) was to create a .NET Framework project, as it was not available in .NET Core 3.1 (at the moment they had to use this feature).我的老板告诉我他们做了一些测试,他们认为创建可以从 VB6(包括事件)中调用的 C# 代码的唯一方法是创建一个 .NET Framework 项目,因为它在 .NET Core 3.1 中不可用(在他们必须使用此功能的那一刻)。

But seeing the previously mentioned page and doing some more research, I do think it looks like .NET Core supports this interoperability.但是看到前面提到的页面并进行了更多研究,我确实认为 .NET Core 似乎支持这种互操作性。 And I do not understand why wouldn't it be possible for the .NET Core platform to do this.而且我不明白.NET Core 平台为什么不能这样做。

I am sorry if this question does not belong here, but could someone shed some light on this issue?如果这个问题不属于这里,我很抱歉,但有人可以对这个问题有所了解吗?

.NET Framework, .NET Core 3, and .NET 5/6/+ can expose a COM object to the COM world. .NET Framework、.NET Core 3 和 .NET 5/6/+ 可以将 COM 对象公开给 COM 世界。 This world includes VB6 and everything that can do COM.这个世界包括 VB6 和一切可以做 COM 的东西。

a) Create a .NET 5 "NetClassLibrary" Class Library project (using Visual Studio for example) a) 创建一个 .NET 5“NetClassLibrary”类库项目(例如使用 Visual Studio)

b) Add a NetComObject.cs file with the following code for example: b) 添加带有以下代码的NetComObject.cs文件,例如:

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: c) 修改 .csproj 以将EnableComHosting属性设置为true并确保为 x86编译,因为 VB6 是 32 位程序(请注意,如果您使用 Excel 的 64 位 VBA,而不是 VB6,则必须为 64 位编译) ,例如这样:

<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: d) 编译,启动管理员提示并运行:

regsvr32 yourPathTo\NetClassLibrary.comhost.dll

在此处输入图像描述

e) Create a VB6 project, add this VB6 code: e) 创建一个 VB6 项目,添加这个 VB6 代码:

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 f) 运行

在此处输入图像描述

Note 1: you can use a ProgId attribute too, like COM object often do, you don't have to stick to Guids only...注意 1:您也可以使用ProgId属性,就像 COM 对象经常做的那样,您不必只使用 Guid...

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.注意 2:我在此演示中只使用了IDispatch (“延迟加载”)接口,但您也可以使用IUnknown派生接口(“早期加载”)和 TLB。 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.不幸的是,.NET 5 不会为您创建 .TLB,您必须自己构建它或使用其他工具创建它,然后将其导入 .NET 和/或 VB6 中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM