C# call native dll with string parameter

Date: 2017-05-22
using System;
using System.Runtime.InteropServices;
using System.Text;

namespace ConsoleApplication2
{
 class Program
 {
 [DllImport("DelphiTools.dll", CallingConvention = CallingConvention.StdCall)]
 public static extern void GetSettings(int bufferSize, ref StringBuilder sb);

 static void Main(string[] args)
 {
 int bufferSize = 10000;
 var sb = new StringBuilder(bufferSize);
 sb.Append(@"param");

 GetSettings(bufferSize, ref sb);

 Console.Write(sb.ToString());
 Console.ReadLine();
 }
 }
}
library DelphiTools;

{$R *.res}

procedure GetSettings(bufferSize : integer; var buffer : PAnsiChar); stdcall; export;
var p : PAnsiChar;
 param : AnsiString;
 Result : String;
begin
 param := String(AnsiString(buffer));
 result := 'test';
 p := PAnsiChar(AnsiString(result));
 StrCopy(buffer, p);
end;

exports
 GetSettings;

begin


end.
7460cookie-checkC# call native dll with string parameter