Jump to content
msi1393

import C# Dll in delphi 10.4

Recommended Posts

Posted (edited)

Hello I need to use of C# Dll in my Delphi program

i use of delphi 10.4
is this possible?
 If there is, please explain me step by step

Edited by msi1393

Share this post


Link to post

There are lots of different ways to do this depending on what the dll offers. Nobody can give you any steps without knowing how the dll exposes its functionality. 

Share this post


Link to post
17 hours ago, msi1393 said:

Hello I need to use of C# Dll in my Delphi program

i use of delphi 10.4
is this possible?
 If there is, please explain me step by step

If the DLL is COM-enabled you can use the Component -> Import component dialog from the IDE main menu:

image.thumb.png.a12c4f12a66c5a8911f1a6442b630c9e.png

 

A pure .NET assembly without COM support cannot be used from Delphi with the tools available out of the box, but there are 3rd-party libraries available, some freeware.  A google search for "host the .net framework in delphi" turns up this for example. I have no personal experience with such libraries.

 

Share this post


Link to post

Hello
Thank you for your guidance
I did this but it didn't work

Share this post


Link to post
10 hours ago, msi1393 said:

I did this but it didn't work

We still don't know anything about your dll. When I said that nobody can give you any step by step guides without knowledge of what the dll is, I stand by that. I mean, we could write you lots of hypotheticals. You've got one above. But how about you put some effort in and find out what this dll offers. 

Share this post


Link to post

Use C# to create an intermediate DLL that loads the existing DLL, and exports the methods you need on the Delphi side, using types and calling conventions compatible with Delphi.

Share this post


Link to post

Hello
And thank you for your guidance
I tried every guide I could find but it didn't work.
Actually, I want to do the following code in Delphi
that I can use a software implemented with #C
I am attaching the text file of the class related to the dll created in C#

Class1.cs

Share this post


Link to post
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

procedure Encrypt(sr: PAnsiChar; key: PAnsiChar; var encryptedText: PAnsiChar); stdcall; external 'YourDLLName.dll';
procedure Decrypt(sr: PAnsiChar; key: PAnsiChar; var decryptedText: PAnsiChar); stdcall; external 'YourDLLName.dll';

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  inputText, key, encryptedText, decryptedText: PAnsiChar;
begin
  inputText := 'Hello World';
  key := '12345678';

  // Encrypt
  Encrypt(inputText, key, encryptedText);
  ShowMessage('Encrypted Text: ' + encryptedText);

  // Decrypt
  Decrypt(encryptedText, key, decryptedText);
  ShowMessage('Decrypted Text: ' + decryptedText);
end;

end.

 

Share this post


Link to post
12 hours ago, Die Holländer said:

procedure Encrypt(sr: PAnsiChar; key: PAnsiChar; var encryptedText: PAnsiChar); stdcall; external 'YourDLLName.dll';

procedure Decrypt(sr: PAnsiChar; key: PAnsiChar; var decryptedText: PAnsiChar); stdcall; external 'YourDLLName.dll';

That would work for a DLL that contains native code and uses ANSI char based null-terminated strings.

 

The code posted is C# code, using a .NET System.String class "Representing text as a sequence of UTF-16 code units"

 

The DLL generated will be a .NET assembly containing managed code, not native code. As Peter mentioned earlier, either the DLL has to be written & compiled to support COM interop

https://blogs.embarcadero.com/using-a-net-assembly-via-com-in-delphi/

https://www.blong.com/Conferences/BorCon2004/Interop2/COMNetInterop.htm#CCW

 

or the Delphi application that loads the DLL has to host the CLR before the DLL is loaded.

https://stackoverflow.com/questions/2048540/hosting-clr-in-delphi-with-without-jcl-example

https://stackoverflow.com/questions/258875/hosting-the-net-runtime-in-a-delphi-program

https://en.delphipraxis.net/topic/1744-net-runtime-library-for-delphi/

https://adamjohnston.me/delphi-dotnet-interop-with-jvcl/

 

"Explain step by step" isn't possible, as there are a lot of factors to consider. I suspect this is why there are commercial libraries that do this.

https://www.remobjects.com/hydra/

https://www.crystalnet-tech.com/

https://www.atozed.com/crosstalk/

There was a library called Managed-VCL, which can be found online. But the website vanished a while back.

 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×