Pascal Bayer
Pascal Bayer

Reputation: 2613

How do I use C# to call a function that receives a Delphi open-array parameter?

How do I convert the Delphi code into C#? It takes an array of Byte, but I'm not sure what the C# equivalent is. My attempt doesn't work and throws exceptions like AccessViolationException.

Delphi:

function SetLevel(a: array of byte): boolean; stdcall; external 'DMX510.dll';

C#:

[DllImport("DMX510.DLL")]
public static extern Boolean SetLevel(Byte[] bytearray);

Byte[] byteArray = new Byte[5];
byteArray[1] = 75;
SetLevel(byteArray);

Upvotes: 5

Views: 1776

Answers (2)

Evalds Urtans
Evalds Urtans

Reputation: 6704

This is the way how I implemented successfully sending arrays from and to Delphi & C#.

C#:

[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 still work like 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
   //...
end;

exports LoadFromFileCHR;

Note that VAR is also with Array parameters (Delphi analog of REF).

I had all sorts of errors, because I had ref with arrays in C# code

Another problem that caused memory corruption for me was that I did not notice that these codes are not the same in Delphi and C#:

Delphi:

for i := 0 to Length(fileCHR.ChromX) do //This is wrong

C#

for(int i = 0; i < fileCHR.ChromX.Length; i++)

The same in delphi would be

for i := 0 to Length(fileCHR.ChromX) - 1 do //This is right

If you overflow boundaries of arrays passed to delphi it could also cause all sorts of errors

Upvotes: 0

David Heffernan
David Heffernan

Reputation: 613461

A Delphi open array is not a valid interop type. You can't easily match that up with a C# byte[] through a P/invoke. In an ideal world a different interface would be exposed by the native DLL but as you have stated in comments, you do not have control over that interface.

However, you can trick the C# code into passing something that the Delphi DLL will interpret correctly, but it's a little dirty. The key is that a Delphi open array declared like that has an extra implicit parameter containing the index of the last element in the array.

[DllImport(@"DMX510.DLL")]
public static extern bool SetLevel(byte[] byteArray, int high);

byte[] byteArray = new byte[] { 0, 75, 0, 0, 0};
SetLevel(byteArray, byteArray.Length-1);

To be clear, in spite of the parameter lists looking so different, the C# code above will successfully call the Delphi DLL function declared so:

function SetLevel(a: array of byte): boolean; stdcall;

I have no idea whether or not passing an array of length 5 is appropriate, or whether you really meant to just set the second item to a non-zero value.

Upvotes: 8

Related Questions