1

I'm trying to create a simple COM component in .NET 5. I have gotten this far:

  • I have created an interface with the right attributes:
    [ComVisible(true)]
    [Guid("12345678-3e60-4c56-abcd-13d4098434f7")]
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    public interface IConvertor
    {
        string ConvertPatient(string s);
    }
  • I have created a class that implements this interface:
[ComVisible(true)]
//[ClassInterface(ClassInterfaceType.None)]
public class Convertor : IConvertor
{
    public Convertor()
    { }

    public string ConvertPatient(string s)
    {
        return "Hello, " + s;
    }
}

I tried with and without the ClassInterface and Guid attributes.

  • In the .csproj file I added these:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
    <TargetFramework>net5.0</TargetFramework>
    <!-- Indicate the assembly is providing a COM server -->
    <EnableComHosting>true</EnableComHosting>
    <EnableRegFreeCom>true</EnableRegFreeCom>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
  <PlatformTarget>x86</PlatformTarget>
</PropertyGroup>
<ItemGroup>
    <PackageReference Include="Hl7.Fhir.R4" Version="3.6.0" />
</ItemGroup>
</Project>

I compiled for x86 and I got these files (and some more):

  • xxx.comhost.dll
  • xxx.dll

The problem is that I don't manage to register this component.

tlbexp xxx.InterOp.dll

gives me this error:

TlbExp : error TX0000 : Type library exporter encountered an error while processing 'hdmpcloud.ehealth.FhirTools.InterOp.Convertor, hdmpcloud.ehealth.FhirTools.InterOp'. Error: Type library exporter cannot load type 'hdmpcloud.ehealth.FhirTools.InterOp.Convertor' (error: Could not load file or assembly 'System.Runtime, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified. (Exception from HRESULT: 0x80070002)).

regasm /tlb xxx.InterOp.comhost.dll 

gives me

RegAsm : error RA0000 : Failed to load 'C:_projects...\xxx.InterOp.comhost.dll' because it is not a valid .NET assembly

regsvs32 seems to work and register the component, but no TLB is created.

I want to call this component from unmanaged code (a Delphi program).

The documentation ( https://learn.microsoft.com/en-us/dotnet/framework/interop/ ) is not very clear on a couple of things:

  • Is COM still feasible in .NET 5? (without a type library its use seems limited)
  • The documentation is for .NET in general, but they don't specify a version. Any help or pointers in the right direction would be much appreciated.

Gaston

PS. I have asked this question also at https://www.codeproject.com/Messages/5844248/Re-Creating-a-COM-component-in-NET-5.

Simon Mourier
  • 132,049
  • 21
  • 248
  • 298
Gaston
  • 49
  • 5
  • 1
    this refers to .net core: https://learn.microsoft.com/en-us/dotnet/core/native-interop/expose-components-to-com – jazb Nov 23 '21 at 07:35
  • 2
    .NET Core and .NET 5 don't have any support for tlb: https://learn.microsoft.com/en-us/dotnet/core/native-interop/expose-components-to-com#embedding-type-libraries-in-the-com-host and .NET 6 has only support for embedding a tlb (but not creating it), you can't use .NET Framework's regasm.exe over .NET Core/5 assemblies – Simon Mourier Nov 23 '21 at 07:59
  • On that same page they are talking about creating and compiling an IDL file. Would you have any reference or an example for this? – Gaston Nov 23 '21 at 12:41
  • 1
    I can confirm that error. If I add System.Rumtime.dll to the dll's folder I get the next error (translated) "Reference assemblies shall not be loaded for execution. They can get only get loaded in a reflection-only loading context". So it seems a dead end... – lidqy Nov 23 '21 at 13:07
  • 3
    don't know if it's too late, but I had a similar problem and finally solved it. here's my example https://github.com/janschreier/net6COMSample – Jan May 07 '22 at 04:18
  • Does this answer your question? [What is the path for tblexp in a .NET core project?](https://stackoverflow.com/questions/68474473/what-is-the-path-for-tblexp-in-a-net-core-project) – StayOnTarget Jun 16 '23 at 12:45

0 Answers0