【问题标题】:.NET Core 2.1 - How to create COM object and generate *.tlb file.NET Core 2.1 - 如何创建 COM 对象并生成 *.tlb 文件
【发布时间】:2018-07-25 09:34:33
【问题描述】:

我想在 .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>

我的程序.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:\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>

那么 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

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

【问题讨论】:

  • 为什么要使用 .NET core 来注册 COM 对象?我认为这根本不可能,它只能在 Windows 上运行,所以您不妨使用完整的 .NET 框架版本,不是吗?
  • .NETCore 团队是not in a hurry 提供工具支持。除了它永远不能移植到* nixes之外,它没有任何意义。您可以在其上使用此代码的任何机器始终具有可用的框架的桌面版本。 mscoree.dll 管道不容易在 CoreCLR 中替代,所以不要屏住呼吸。
  • 我在 dotnet core github github.com/dotnet/corefx/issues/31359github.com/dotnet/coreclr/issues/19120 中创建了两个问题
  • 一个概念。将所有可重用代码放入 .Net Standard 程序集中,然后创建一个精简的 Full Framework Wrapper,在 .Net Standard 程序集周围公开 Com 接口。
  • 但是如果没有安装完整的 .net 怎么办?或版本低于 4.6.1。我有 .net 标准 2 中的项目,它可以从 .net 框架 4.6.1 中使用。 docs.microsoft.com/pl-pl/dotnet/standard/net-standard .Net 核心支持 Windows 7 SP1 和 Windows 8.1。这些系统默认没有 .net framework 4.6。 blogs.msdn.microsoft.com/astebner/2007/03/14/…

标签: .net .net-core com-interop regasm tlbexp


【解决方案1】:

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

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

您需要知道的一切都可以在一个月前添加的this MSDN page 中找到。请注意 .NETCore 3 目前仍处于预览质量状态。而且这是仅限 Windows,没有适用于 Linux 和 macOS 的迁移路径,COM 与仅在 Windows 上可用的服务紧密相关。

【讨论】:

    【解决方案2】:

    作为 COM 的替代方案,您可以在本机进程中托管 .NET Core 运行时以调用托管代码。

    托管 .NET Core 运行时是一种高级方案,在大多数情况下,.NET Core 开发人员无需担心托管问题,因为 .NET Core 构建过程提供了运行 .NET Core 应用程序的默认主机。不过,在某些特殊情况下,显式托管 .NET Core 运行时可能会很有用,既可以作为在本机进程中调用托管代码的一种方式,也可以更好地控制运行时的工作方式。

    链接:

    【讨论】:

      猜你喜欢
      • 2022-06-14
      • 2010-11-12
      • 2011-03-05
      • 1970-01-01
      • 1970-01-01
      • 2015-06-20
      • 2019-05-19
      • 2018-03-13
      • 2012-11-21
      相关资源
      最近更新 更多