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.