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

查看:330
本文介绍了.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>

我的程序。

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();
    }
}

我无法使用来自c的RegAsm注册COM:在.netcore2.1目标框架中发布项目后,在Windows上的Microsoft Windows .NET Framework Framework 4.0.30319中。

I can't register COM using RegAsm from c:\Windows\Microsoft.NET\Framework64\v4.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标准程序集。如果我使用上述源代码和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:\[...]\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程序集吗?

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天全站免登陆