Create dll in C # and use in Delphi 7

4

I need to create a C # dll so I can use it in delphi.

I've tried the following:

I created a basic dll with a sum method, but calling it in delphi does not return anything, it would be like the created method did not exist.

Below the code in C #:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;

namespace ClasseCom
{
    [ClassInterface(ClassInterfaceType.None)]
    public class soma        
    {
        public soma() { }
        public double somar(int a, int b)
        {
            return a + b;
        }

    }
}

Here's how I try to call it in Delphi 7:

procedure TForm1.Button1Click(Sender: TObject);
var
  Handle : THandle;
  somar : TSomaDll;
  total : Double;

begin

  Handle:=LoadLibrary ('ClasseCom.dll');
  if Handle <> 0 then begin

    @somar:=GetProcAddress(Handle, 'somar');

    if @somar <> nil then
      total := somar(1, 2);

    FreeLibrary (Handle);

  end;

  ShowMessage(FloatToStr(total));

end;

In Delphi the variable @soma is returning as nil .

I've also used an application called DDL Export Viewer which shows the methods of the dll, but for my does not present anything, and trying the validation of the State Registration provided by the syntegra presents the same method.

    
asked by anonymous 02.09.2016 / 15:52

2 answers

2

I already had to do this integration of C # and delphi and I hit the head very rsrs ..

Well in your C #, you install via nuget UnmanagedExports

And in your C # mark with the DllExport attribute the method you want delphi to "pinch".

Also mark your class with [ComVisible(true)] and define a Guid

Your project must be configured to compile on the x86 platform.

For example:

[Guid("14fd1190-df04-488c-ab0f-b120ea3e3f3a")]
[ComVisible(true)]
public class Exported
{

    [DllExport]
    public static int func(int a, int b) 
    {
        Thread.CurrentThread.SetApartmentState(ApartmentState.STA); //é necessario se você quiser que o delphi abra um form do C#

        Form1 f = new Form1();
        f.ShowDialog();

        return a + b;
    }
}

And in your delphi declares the function, referenced dll, remembering that dll should be in the same directory as your delphi application (.exe) ..

function func(var a: Integer, var b: Integer ): Integer; stdcall; external suaDllEmCSharp.dll;

and during your code just call

...
if func(1,2)=3 then
...

I remember trying to do to load the dll at run time, like you did, but I did not succeed.

I had to make the static statement.

I hope I have helped ...

    
02.09.2016 / 16:22
2

As previously stated the path is to consume assembly as COM:


using System;
using System.Runtime.InteropServices;

namespace ClasseCom
{
    [ComVisible(true)]
    [ClassInterface(ClassInterfaceType.None)]
    [ComDefaultInterface(typeof(ISoma))]
    [Guid("E8D53EED-3B2F-45A8-BB29-CC111BB426D1")]
    public class Soma : ISoma
    {
        public double Somar(int a, int b)
        {
            return a + b;
        }
    }

    [ComVisible(true)]
    [Guid("97D1968E-DC7F-45BD-BD0E-1AC822D95264")]
    public interface ISoma
    {
        double Somar(int a, int b);
    }
}

With this you will have your class exposed via COM. From here you use the tlbexp utility to export the assembly :

tlbexp.exe Caminho\Do\Seu\Assembly\AssemblyDaClasseCOM.dll

The easiest way to access the utility (assuming you have Visual Studio installed) is through the prompt of the Visual Studio Developer Command Prompt that can be found inside the folder of Visual Studio from the Start menu.

After generating the tlb using tlbexp you only have to import it into Delphi just as you would any ActiveX. As said by @ Caffé the Delphi importer will generate wrappers for those COM classes that will help you a lot.

An important point to remember right now is that you will have to have this .NET assembly registered on your machine using the Regasm

If you want to use without a registry (which I strongly recommend to avoid the famous DLL Hell) you can take a look at this article: With Registration Free

That's it, any question is just talk.

Edit

Delphi Sample Code (Running):


uses
  MeuComponenteCom_TLB;

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  LSoma: ISoma;
  LResultado: Double;
begin
    LSoma := CoSoma.Create;
    LResultado := LSoma.Somar(1, 2);

    ShowMessage(FloatToStr(LResultado));
end;

end.

Note: The example was done using Delphi XE7.

Edit2

If you have questions about adding a TLB, here are some screenshots that show you where you can do this:

    
02.12.2016 / 12:35