The first thing you need to do is use stdcall from C # side:
[DllImport("DLL.dll", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Auto)]
I also want to be sure that the Delphi side is Post Delphi 2009 and therefore uses wide characters. If so, then there is no problem. If you are using non-Unicode Delphi, you will need CharSet.Ansi .
I would probably also return LongBool on the Delphi side and marshal it with
[return: MarshalAs(UnmanagedType.Bool)]
back on the .NET side.
Finally, a fixed-length array needs to be split differently. The standard approach for fixed-length character arrays is to use a StringBuilder on the .NET side, which is sorted as you wish.
By placing it fully and setting the Delphi syntax, you will get:
Delphi
type TFixedLengthArray = array [1..200] of char; function Get_Matrix(var Matrix: TFixedLengthArray): LongBool; stdcall;
FROM#
[DllImport("DLL.dll", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Auto)] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool Get_Matrix(StringBuilder Matrix); static void Main(string[] args) { StringBuilder Matrix = new StringBuilder(200); Get_Matrix(Matrix); }
Finally, make sure you end the null string when you return it from your DLL!
David heffernan
source share