Cem
Cem

Reputation: 361

How to register a .NET 5+ dll to registery as COM correctly and use it from Delphi code?

I can register a dll complied by .NET framework 4.8 to registry as COM and use it from Delphi code, but I cannot do the same thing in .NET 5+

Class :

  [ComVisible(true)]
  [Guid("21487353-6AE0-403B-AA8D-57AE9725C9DB")]
  [ClassInterface(ClassInterfaceType.None)]
  public class ClassA : IClassA
  {
      public string GetStr()
      {
          return "Message text";
      }
  }

IClass:

  [ComVisible(true)]
  [Guid("1A3C6B1D-9420-45A4-BCD4-E7E31762D035")]
  [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
  public interface IClassA
  {
      string GetStr();
  }

I build the project as x86 architecture (.NET framework 4.8.1) then run regasm project.dll /codebase.

When I check the registry entries, I saw that ClassA was successfully registered and it has a ProgId (ClassLib.ClassA).

And I called it from Delphi code:

program Project1; 
uses
  ComObj,
  SysUtils,
  ActiveX;

var
  COMObject: OleVariant;
  ReturnValue: string;
begin
  try
    CoInitialize(nil);

    COMObject := CreateOleObject('ClassLib.ClassA'); 
    ReturnValue := COMObject.GetStr; 

    WriteLn('COM method called successfully.->'+ ReturnValue);

  except
    on E: Exception do
      WriteLn('Error: ' + E.Message); 
  end;
  CoUninitialize;
end.

Here, I got the "message text" from dll. There is no problem but I want to do same thing with .NET 8 project dll.

So I created a new .NET 8 class library project with x86 build configuration and add these two properties to a PropertyGroup in .csproj and compiled

<EnableComHosting>true</EnableComHosting>
<ComVisible>true</ComVisible> 

The regasm tool is not working with a dll compiled by .NET 8 and so tried regsvr32 tool as said here https://learn.microsoft.com/en-us/dotnet/core/native-interop/expose-components-to-com#sample

Command :

 regsvr32 .\ClassLib.comhost.dll

The operation completed successfully, but I get an error:

No such interface supported

when I call that code from Delphi.

Upvotes: 0

Views: 100

Answers (0)

Related Questions