This is how I successfully implemented sending arrays from and to Delphi and C #.
FROM#:
[DllImport("Vendors/DelphiCommunication.dll", CallingConvention = CallingConvention.StdCall)] public static extern void LoadFromFileCHR( string sFileName, ref int iSize, ref double AreaCoef, ref double FWaveLength, ref bool FHasWaveLength, double[] ChromX, double[] ChromY );
Note that single types have REF and arrays DO NOT have REF , but arrays will work as REF anyway
Delphi:
Type ArrayDouble100k = array [0..99999] of Double; procedure LoadFromFileCHR( FileName : String; var Size : Integer; var AreaCoef : Double; var FWaveLength: Double; var FHasWaveLength : Boolean; var ChromX : ArrayDouble100k; var ChromY : ArrayDouble100k); StdCall; begin
Note that VAR also has Array parameters (similar to Delphi REF).
I had all kinds of errors because I had a ref with arrays in C # code
Another problem that caused memory corruption for me was that I did not notice that these codes did not match Delphi and C #:
Delphi:
for i := 0 to Length(fileCHR.ChromX) do //This is wrong
FROM#
for(int i = 0; i < fileCHR.ChromX.Length; i++)
The same thing in delphi would be
for i := 0 to Length(fileCHR.ChromX) - 1 do //This is right
If you overflowed the boundaries of arrays passed to delphi, this could also cause all kinds of errors
Evalds Urtans
source share