.NET Core 2.1 - 如何创建 COM 对象并生成 *.tlb 文件 [英] .NET Core 2.1 - How to create COM object and generate *.tlb file

查看:49
本文介绍了.NET Core 2.1 - 如何创建 COM 对象并生成 *.tlb 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 .net Core 中构建 COM 对象,然后通过 RegAsm 注册.

I would like to build COM object in .net Core and then register by RegAsm.

我的 .csproj 文件:

My .csproj file:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFrameworks>netcoreapp2.1;net4.7.2</TargetFrameworks>
    <RuntimeIdentifier>win7-x64</RuntimeIdentifier>
    <Platforms>x64</Platforms>
    <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
  </PropertyGroup>
</Project>

我的程序.cs:

using System;
using System.Runtime.InteropServices;

namespace ComExample
{
    [Guid("7ce1e40f-760a-4d81-b70b-61108ed15cb4")]
    [ComVisible(true)]
    public interface IComExampleClass
    {
        IComModelClass ExampleMethod(string param1, string param2);

    }

    [Guid("a8436b3f-3657-4a01-a133-fd333a84cb58")]
    [ClassInterface(ClassInterfaceType.None)]
    [ComVisible(true)]
    public class ComExampleClass : IComExampleClass
    {
        public IComModelClass ExampleMethod(string param1, string param2)
        {
            return new ComModelClass()
            {
                Result = $"{param1} + {param2}"
            };
        }
    }

[Guid("9f5aeede-ec3e-443d-8ba0-9a9f2a6b9e53")]
[ComVisible(true)]
public interface IComModelClass
{
    string Result { get; set; }
}

[Guid("526c6cb5-264d-4629-a894-fff02aeb9ec1")]
[ClassInterface(ClassInterfaceType.None)]
[ComVisible(true)]
public class ComModelClass : IComModelClass
{
    public string Result { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        var test = new ComExampleClass();
        Console.WriteLine(test.ExampleMethod("A", "B").Result);
        Console.ReadKey();
    }
}

在 .netcore2.1 目标框架中发布项目后,我无法使用来自 c:WindowsMicrosoft.NETFramework64v4.0.30319 的 RegAsm 注册 COM.

I can't register COM using RegAsm from c:WindowsMicrosoft.NETFramework64v4.0.30319 after publishing project in .netcore2.1 target framework.

将项目发布到net4.7.2后,我可以通过RegAsm注册程序集,然后在CPP项目中使用.

After publishing project to net4.7.2 I can register assembly by RegAsm and then use it in CPP project.

我也无法使用 TblExp.exe 从 .net 核心项目生成 tlb 文件.

I can't generate tlb file from .net core project using TblExp.exe too.

看起来很奇怪.我可以注册 .Net Standard 程序集.如果我使用上述源代码和 csproj 创建 .Net 标准库:

It looks strange. I can register .Net Standard assembly. If I create .Net Standard Library with above source code and with csproj:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
  </PropertyGroup>

</Project>

然后 RegAsm 效果很好

then RegAsm works good

RegAsm.exe /tlb:C:[...]DotnetStandardCominDebug
etstandard2.0DotnetStandardCom.tlb C:[...]DotnetStandardCominDebug
etstandard2.0DotnetStandardCom.dll

Microsoft .NET Framework Assembly Registration Utility version 4.7.3062.0
for Microsoft .NET Framework version 4.7.3062.0
Copyright (C) Microsoft Corporation.  All rights reserved.

Types registered successfully
Assembly exported to 'C:[...]DotnetStandardCominDebug
etstandard2.0DotnetStandardCom.tlb', and the type library was registered successfully

但是我无法注册 .Net Core 程序集?

But way I can't register .Net Core assembly?

推荐答案

由于在 .NETCore 版本 3 上完成的工作,现在可以做到这一点.正如我在评论中所指出的,托管是早期 .NETCore 版本中缺失的部分,没有替代 mscoree.dll.版本 3 提供了 comhost.dll.相关,他们现在也可以支持C++/CLI代码,ijwhost.dll是替代品.

This is now possible due to the work done on .NETCore version 3. As noted in my comment, hosting was the missing piece in earlier .NETCore versions with no substitute for mscoree.dll. Version 3 provides comhost.dll. Related, they also can now support C++/CLI code, ijwhost.dll is the substitute.

您像使用传统 COM 服务器一样注册组件,使用 Regsvr32.dll

You register the component like you did with traditional COM servers, using Regsvr32.dll

您需要知道的一切都可以在 此 MSDN 页面,于一个月前添加.请注意 .NETCore 3 现在仍然是预览质量.而且这仅适用于 Windows,没有适用于 Linux 和 macOS 的迁移路径,COM 与仅在 Windows 上可用的服务紧密相关.

Everything you need to know is available in this MSDN page, added a month ago. Do beware that .NETCore 3 is still preview quality right now. And this is Windows-only with no migration path for Linux and macOS, COM is too heavily tied to services that are only available on Windows.

这篇关于.NET Core 2.1 - 如何创建 COM 对象并生成 *.tlb 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆