Jump to content

Eric H

Members
  • Content Count

    2
  • Joined

  • Last visited

Everything posted by Eric H

  1. Eric H

    import C# Dll in delphi 10.4

    I forgot to mention that you need to include the package "UnmanagedExports by Robert Giesecke" for the C# project.
  2. Eric H

    import C# Dll in delphi 10.4

    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; }
×