I have created a COM DLL in C# and am using it in Delphi 10 as a COM object. After registering the DLL, it is working fine. But I want to use it without registrating the DLL. How can it be possible?
Below is my C# code:
I have set the ComVisible attribute to true in Assembly.cs:
[assembly: ComVisible(true)]
using System;
using System.Runtime.InteropServices;
namespace ComCalculator
{
[ComVisible(true)]
[Guid("c0a3a815-fade-4fb3-9c75-f6eff1851e23")]
public interface ICalculator
{
int Add(int a, int b);
}
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
[Guid("427754c1-c975-4099-9ff7-df788281d3a8")]
public class Calculator : ICalculator
{
public int Add(int a, int b)
{
return a + b;
}
}
}
Below is my Delphi code:
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, ComObj, ComCalculator_TLB;
type
TForm1 = class(TForm)
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
comObject: ICalculator;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
A, B, Result: Integer;
begin
A := 10;
B := 5;
Result := comObject.Add(A, B);
ShowMessage('The result is: ' + IntToStr(Result));
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
comObject := CoCalculator.Create;
end;
end.