【发布时间】: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/31359 和 github.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