msi1393 0 Posted April 26, 2024 (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, 2024 by msi1393 Share this post Link to post
David Heffernan 2415 Posted April 27, 2024 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 250 Posted April 27, 2024 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, 2024 Hello Thank you for your guidance I did this but it didn't work Share this post Link to post
David Heffernan 2415 Posted April 28, 2024 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 55 Posted April 29, 2024 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, 2024 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 2415 Posted April 29, 2024 Rather than linking to files, please show the code inline, formatted Share this post Link to post
Die Holländer 83 Posted April 30, 2024 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 81 Posted April 30, 2024 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
Eric H 0 Posted Wednesday at 06:01 PM A little late to the conversation, but might help future people. Sample code I used with Delphi to interface with a C# .Net DLL that included AWS DynamoDB using the C# AWSSDK.DynamoDBv2 package. I wanted to receive a class object or send a class object to C#, so I had to deserialize/serialize JSON to do this. In Delphi I call RegistrationLoad or RegistrationUpdate, which in turn deals with the C# DLL. Delphi: function DllRegistrationLoad(pRegCode: String): PWideChar; stdcall; external 'MyDllFunctions.dll'; function DllRegistrationUpdate(pRegistrationJson: String): Boolean; stdcall; external 'MyDLLFunctions.dll'; function TAwsDynamoDBQueryDLL.RegistrationLoad(const pRegCode: String): TRegistration; begin Result := nil; var vDllResult := DllRegistrationLoad(pRegCode); try if vDllResult <> '' then Result := TJson.JsonToObject<TRegistration>(vDllResult); finally CoTaskMemFree(vDllResult); end; end; function TAwsDynamoDBQueryDLL.RegistrationUpdate(var pRegistration: TRegistration): Boolean; begin var vReg := TJsonAWSRegistration.Create(pRegistration); try var vJson := TJson.ObjectToJsonString(vReg); Result := DllRegistrationUpdate(vJson); finally FreeAndNil(vReg); end; end; C# .NET: [DllExport(CallingConvention = CallingConvention.StdCall)] [return: MarshalAs(UnmanagedType.LPWStr)] public static string DllRegistrationLoad([MarshalAs(UnmanagedType.LPWStr)] string regCode) { var registration = new RegistrationModel(); ... var jsonSettings = new JsonSerializerSettings() { DateFormatString = "yyyy-MM-dd" }; return JsonConvert.SerializeObject(registration, jsonSettings); } [DllExport(CallingConvention = CallingConvention.StdCall)] public static bool DllRegistrationUpdate([MarshalAs(UnmanagedType.LPWStr)] string registrationJson) { var registration = JsonConvert.DeserializeObject<RegistrationModel>(registrationJson); ... return true; } Share this post Link to post
ertank 29 Posted Wednesday at 07:40 PM On 4/29/2024 at 3:48 PM, msi1393 said: 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 You do not need to use the DLL. You already have complete DLL code as far as I can tell. I think it is possible to convert that code to Delphi. I cannot be sure about the details of C# encryption method. It seems like DES, CBC and PCKS#7 padding. You need to confirm that information. Once you have all details, there are several free encryption libraries available for Delphi which can help you do the same directly in your project without any need for a DLL. You can install LockBox from GetIt as an example. There are other alternatives and you may want to check some of them. In case you are using older versions of Delphi like Delphi 7, you can still build your own DLL using a recent version in case you are not able to use the cryptography library in your Delphi version. Nowadays there is a lot of AI use. They can also help you convert that C# code into Delphi. if you choose to do it that way just make sure that final code is actually correct. Share this post Link to post
Eric H 0 Posted Thursday at 07:36 PM On 4/16/2025 at 12:01 PM, Eric H said: A little late to the conversation, but might help future people. Sample code I used with Delphi to interface with a C# .Net DLL that included AWS DynamoDB using the C# AWSSDK.DynamoDBv2 package. I wanted to receive a class object or send a class object to C#, so I had to deserialize/serialize JSON to do this. In Delphi I call RegistrationLoad or RegistrationUpdate, which in turn deals with the C# DLL. Delphi: function DllRegistrationLoad(pRegCode: String): PWideChar; stdcall; external 'MyDllFunctions.dll'; function DllRegistrationUpdate(pRegistrationJson: String): Boolean; stdcall; external 'MyDLLFunctions.dll'; function TAwsDynamoDBQueryDLL.RegistrationLoad(const pRegCode: String): TRegistration; begin Result := nil; var vDllResult := DllRegistrationLoad(pRegCode); try if vDllResult <> '' then Result := TJson.JsonToObject<TRegistration>(vDllResult); finally CoTaskMemFree(vDllResult); end; end; function TAwsDynamoDBQueryDLL.RegistrationUpdate(var pRegistration: TRegistration): Boolean; begin var vReg := TJsonAWSRegistration.Create(pRegistration); try var vJson := TJson.ObjectToJsonString(vReg); Result := DllRegistrationUpdate(vJson); finally FreeAndNil(vReg); end; end; C# .NET: [DllExport(CallingConvention = CallingConvention.StdCall)] [return: MarshalAs(UnmanagedType.LPWStr)] public static string DllRegistrationLoad([MarshalAs(UnmanagedType.LPWStr)] string regCode) { var registration = new RegistrationModel(); ... var jsonSettings = new JsonSerializerSettings() { DateFormatString = "yyyy-MM-dd" }; return JsonConvert.SerializeObject(registration, jsonSettings); } [DllExport(CallingConvention = CallingConvention.StdCall)] public static bool DllRegistrationUpdate([MarshalAs(UnmanagedType.LPWStr)] string registrationJson) { var registration = JsonConvert.DeserializeObject<RegistrationModel>(registrationJson); ... return true; } I forgot to mention that you need to include the package "UnmanagedExports by Robert Giesecke" for the C# project. Share this post Link to post