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



我想在.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();
}
}

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

在将项目发布到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:[...]DotnetStandardCombinDebugnetstandard2.0DotnetStandardCom.tlb C:[...]DotnetStandardCombinDebugnetstandard2.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:[...]DotnetStandardCombinDebugnetstandard2.0DotnetStandardCom.tlb', and the type library was registered successfully

但是我不能注册。Net Core组件?

由于在上所做的工作,现在这是可能的。NETCore版本3。正如我在评论中指出的,主持是早些时候缺失的部分。NETCore版本没有mscoree.dll的替代品。版本3提供了comhost.dll。相关的,它们现在也可以支持C++/CLI代码,ijwhost.dll是替代品。

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

您需要了解的所有信息都可以在一个月前添加的MSDN页面中找到。一定要小心。NETCore 3现在仍然是预览质量。这是Windows,没有Linux和macOS的迁移路径,COM与仅在Windows上可用的服务绑定太重。

作为COM的替代方案,您可以托管。NET核心运行时调用托管代码。

托管。NET核心运行时是一个高级场景,在大多数情况下。NET核心开发人员不需要担心托管,因为。NET Core构建过程提供了一个默认的主机来运行。NET核心应用程序。不过,在某些特殊情况下,显式托管可能会很有用。NET核心运行时,或者作为在本机进程中调用托管代码的一种方式,或者为了对运行时的工作方式获得更多的控制。

链接:

  • 主机。NET核心
  • 主机。NET Core Clr

相关内容

最新更新