.NET Core 2.1-如何创建COM对象并生成* .tlb文件

Woj*_*pl2 5 .net tlbexp com-interop regasm .net-core

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

我的.csproj文件:

<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>
Run Code Online (Sandbox Code Playgroud)

我的program.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();
    }
}
Run Code Online (Sandbox Code Playgroud)

在.netcore2.1目标框架中发布项目后,无法从c:\ Windows \ Microsoft.NET \ Framework64 \ v4.0.30319使用RegAsm注册COM。

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

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

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

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

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

</Project>
Run Code Online (Sandbox Code Playgroud)

然后RegAsm运作良好

RegAsm.exe /tlb:C:\[...]\DotnetStandardCom\bin\Debug\netstandard2.0\DotnetStandardCom.tlb C:\[...]\DotnetStandardCom\bin\Debug\netstandard2.0\DotnetStandardCom.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:\[...]\DotnetStandardCom\bin\Debug\netstandard2.0\DotnetStandardCom.tlb', and the type library was registered successfully
Run Code Online (Sandbox Code Playgroud)

但是,我不能注册.Net Core程序集吗?

Han*_*ant 6

由于在.NETCore版本3上所做的工作,现在这是可能的。正如我的评论中所述,在早期的.NETCore版本中,托管是缺少的部分,不能替代mscoree.dll。版本3提供了comhost.dll。相关的是,它们现在还可以支持C ++ / CLI代码,ijwhost.dll可以替代。

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

您需要了解的所有信息都可以在一个月前添加的MSDN页面中找到。请注意,.NETCore 3现在仍然具有预览质量。而且这仅是Windows,没有针对Linux和macOS的迁移路径,COM与只能在Windows上使用的服务紧密相关。