I would like to build a COM object in .NET Core and then register it by RegAsm.
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>
My program.cs file:
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();
}
}
I can't register COM using RegAsm from c:\Windows\Microsoft.NET\Framework64\v4.0.30319 after publishing the project in the .NET Core 2.1 target framework.
After publishing the project to .NET 4.7.2, I can register the assembly by RegAsm and then use it in the CPP project.
I can't generate a .tlb file from the .NET Core project using TblExp.exe either.
It looks strange. I can register a .NET Standard assembly. If I create a .NET Standard Library with the above source code and with csproj:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
</Project>
then RegAsm works good:
RegAsm.exe /tlb:C:\[...]\DotnetStandardCom\bin\Debug\netstandard2.0\DotnetStandardCom.tlb C:\[...]\DotnetStandardCom\bin\Debug\netstandard2.0\DotnetStandardCom.dll
Output:
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
But why can't I register the .NET Core assembly?
This is now possible due to the work done on .NET Core version 3. As noted in my comment, hosting was the missing piece in earlier .NET Core versions without any substitute for mscoree.dll. Version 3 provides comhost.dll. Related, they also can now support C++/CLI code, and ijwhost.dll is the substitute.
You register the component like you did with traditional COM servers, using Regsvr32.dll.
Everything you need to know is available in this MSDN page, added a month ago. Do beware that .NET Core 3 is still preview quality right now. And this is Windows-only without any migration path for Linux and macOS, COM is too heavily tied to services that are only available on Windows.