Search code examples
.net.net-corecom-interopregasmtlbexp

.NET Core 2.1 - How to create COM object and generate *.tlb file


I would like to build COM object in .net Core and then register 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:

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 project in .netcore2.1 target framework.

After publishing project to net4.7.2 I can register assembly by RegAsm and then use it in CPP project.

I can't generate tlb file from .net core project using TblExp.exe too.

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>

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

But way I can't register .Net Core assembly?


Solution

  • 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.

    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 .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.