Простенький код DLL библиотеки написанный на Delphi
library Project1; uses System.SysUtils, System.Classes, Winapi.Windows, System.Math; {$R *.res} function GetString(): string; begin Result := 'Hello World!'; end; function GetStringW(key: Int32; buffer: PChar; size: Int32): Int32; stdcall; var S: string; begin S := GetString; CopyMemory(buffer, @S[1], Min(size, S.Length*SizeOf(Char))); Result := S.Length; end; exports GetStringW name 'GetStringW'; begin {-} end.
И методика импорта его в C#
// ... using System.Text; // StringBuilder using System.Runtime.InteropServices; // DllImport namespace MyProgram { class Program { [DllImport("...PathToDll...\\Win64\\Debug\\Project1.dll", SetLastError = true, CharSet = CharSet.Auto)] static extern int GetStringW(int key, StringBuilder buffer, int size); static string GetString() { StringBuilder st = new StringBuilder(200); int len = GetStringW(1, st, st.Capacity); return st.ToString(); } static void Main(string[] args) { string st = GetString(); Console.WriteLine(st); Console.ReadKey(); } } }