Delphi dll in c # array - var as parameter

I need to use the Delphi DLL in my C # code.

I have some success using other methods with common parameters, but in this case the solution is still hidden.

The DLL documentation presents this declaration:

Function Get_Matrix (var Matrix : array [ 1..200 ] of char) : boolean ; stdcall; 

I tried using:

 [DllImport("DLL.dll")] public static extern bool Get_Matrix(ref char[] Matrix); 

Failed. Some help?

+7
source share
1 answer

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!

+8
source

All Articles