7
votes

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?

2
Why would you use .NET core to register COM objects? I don't think this is possible at all and it would only work on Windows so you might as well use the full .NET framework version, no?Isma
The .NETCore team is not in a hurry to provide tooling support. There is no point to it, beyond it never being portable to the *nixes. Any machine on which you could use this code always has the desktop version of the framework available. The mscoree.dll plumbing is not easy to substitute in CoreCLR, so don't hold your breath for it.Hans Passant
I created two issues in dotnet core github github.com/dotnet/corefx/issues/31359 and github.com/dotnet/coreclr/issues/19120Wojtpl2
One Concept. Put all your reusable code in a .Net Standard assembly and then create a thin Full Framework Wrapper that exposes Com Interfaces around your .Net Standard assembly.Sql Surfer
But what if the full .net is not installed? or have version below 4.6.1. I have project in .net standard 2 and it can be use from .net framework 4.6.1. docs.microsoft.com/pl-pl/dotnet/standard/net-standard .Net core support Windows 7 SP1 and Windows 8.1. These systems don't have .net framework 4.6 by default. blogs.msdn.microsoft.com/astebner/2007/03/14/…Wojtpl2

2 Answers

12
votes

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.

1
votes

As an alternative to COM, you could host the .NET Core runtime in your native process to call managed code.

Hosting the .NET Core runtime is an advanced scenario and, in most cases, .NET Core developers don't need to worry about hosting because .NET Core build processes provide a default host to run .NET Core applications. In some specialized circumstances, though, it can be useful to explicitly host the .NET Core runtime, either as a means of invoking managed code in a native process or in order to gain more control over how the runtime works.

Links: