Import C# Assembly to Delphi function limit

Viewed 95

I have been trying to use a C# dll with Delphi 10.3 using COM Interface and have encountered a very strange behaivour.

Here is my minimal sample of c#

namespace comtest
{
    [ComVisible(true)]
    [Guid("0F6BF46C-8B5F-4CCD-8E0D-71C175AD8234")]
    [InterfaceType(ComInterfaceType.InterfaceIsDual)]
    public interface IComInterface
    {
        public int func1(); 
        public int func2(); 
        public int func3(); 
        public int func4(); 
        public int func5(); 
        public int func6(); 
        public int func7(); 
        //public int func8(); 
        //public int func9(); 
        //public int func10();

    }

    [ComVisible(true)]
    [Guid("9F39DD6D-053B-4DD6-850A-DC7D370E0506")]
    [ClassInterface(ClassInterfaceType.None)]
    public class ComClass : IComInterface
    {
        public int func1() => 1;
       
        public int func2() => 1;
        public int func3() => 1;
        public int func4() => 1;
        public int func5() => 1;
        public int func6() => 1;
        public int func7() => 1;
        //public int func8() => 1;
        //public int func9() => 1; 
        //public int func10()=> 1;
    }
}

and csproj:

    <Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net5.0</TargetFramework>
    <EnableComHosting>true</EnableComHosting>
    <EnableRegFreeCom>true</EnableRegFreeCom>
  </PropertyGroup>

</Project>

so if I use this assembly and go to Rad studio "Components-> Import Component -> .Net Assembly -> add " I can import this and get a nice *_TLB.pas file.

If I remove the comments of function func8 and do the same, i get "OLE Error 80131124"

So basically everything is fine up to 7 functions. If there are more than 7 functions I cant import the Assembly. Why isnt it possible to use more than 7 functions? Am i Missing something?

How can i use an Assembly with more than 7 functions. Is it possible to just extend the *_TLB.pas with additional functions?

EDIT Workaround

I just tried a "brute force" attempt by adding func8 to the *_tlb.pas after creating it with 7 functions:

 IComInterface = interface(IDispatch)
    ['{0F6BF46C-8B5F-4CCD-8E0D-71C175AD8234}']
    function func1: Integer; safecall;
    function func2: Integer; safecall;
    function func3: Integer; safecall;
    function func4: Integer; safecall;
    function func5: Integer; safecall;
    function func6: Integer; safecall;
    function func7: Integer; safecall;
    function func8: Integer; safecall;
  end;

// *********************************************************************//
// DispIntf:  IComInterfaceDisp
// Flags:     (4416) Dual OleAutomation Dispatchable
// GUID:      {0F6BF46C-8B5F-4CCD-8E0D-71C175AD8234}
// *********************************************************************//
  IComInterfaceDisp = dispinterface
    ['{0F6BF46C-8B5F-4CCD-8E0D-71C175AD8234}']
    function func1: Integer; dispid 1610743808;
    function func2: Integer; dispid 1610743809;
    function func3: Integer; dispid 1610743810;
    function func4: Integer; dispid 1610743811;
    function func5: Integer; dispid 1610743812;
    function func6: Integer; dispid 1610743813;
    function func7: Integer; dispid 1610743814;
     function func8: Integer; dispid 1610743815;
  end;

// *********************************************************************//
// Die Klasse CoComClass stellt die Methoden Create und CreateRemote zur
// Verfügung, um Instanzen des Standard-Interface IComInterface, dargestellt
// von CoClass ComClass, zu erzeugen. Diese Funktionen können
// von einem Client verwendet werden, der die CoClasses automatisieren
// will, die von dieser Typbibliothek dargestellt werden.                                           
// *********************************************************************//
  CoComClass = class
    class function Create: IComInterface;
    class function CreateRemote(const MachineName: string): IComInterface;
  end;


// *********************************************************************//
// OLE-Server-Proxy-Klassendeklaration
// Server-Objekt     : TComClass
// Hilfe-String      : 
// Standard-Interface: IComInterface
// Def. Intf. DISP?  : No
// Ereignis-Interface: 
// TypeFlags         : (2) CanCreate
// *********************************************************************//
  TComClass = class(TOleServer)
  private
    FIntf: IComInterface;
    function GetDefaultInterface: IComInterface;
  protected
    procedure InitServerData; override;
  public
    constructor Create(AOwner: TComponent); override;
    destructor  Destroy; override;
    procedure Connect; override;
    procedure ConnectTo(svrIntf: IComInterface);
    procedure Disconnect; override;
    function func1: Integer;
    function func2: Integer;
    function func3: Integer;
    function func4: Integer;
    function func5: Integer;
    function func6: Integer;
    function func7: Integer;
    function func8 : integer;
    property DefaultInterface: IComInterface read GetDefaultInterface;
  published
  end;

plus matching implementation further down, worked like a charm, although i dont like to do this on my own tbh

0 Answers
Related