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);
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.
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:
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.