msi1393 0 Posted April 26 (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 April 26 by msi1393 Share this post Link to post
David Heffernan 2353 Posted April 27 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
PeterBelow 239 Posted April 27 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: 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
msi1393 0 Posted April 27 Hello Thank you for your guidance I did this but it didn't work Share this post Link to post
David Heffernan 2353 Posted April 28 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
eivindbakkestuen 47 Posted April 29 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
msi1393 0 Posted April 29 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
David Heffernan 2353 Posted April 29 Rather than linking to files, please show the code inline, formatted Share this post Link to post
Die Holländer 49 Posted April 30 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
JonRobertson 72 Posted April 30 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