cannot return delphi array in C# array -
i want use delphi array function in c#.
my delphi code:
tintegerarray = array of integer; function testarray(): tintegerarray stdcall; export; var res: tintegerarray2; begin setlength(res, 10); res[5] := 55; result := res; end; exports testarray;
c# code:
[dllimport("gitatele.dll", callingconvention = callingconvention.stdcall)] public static extern int[] testarray();
show me error: cannot marshal 'return value': invalid managed/unmanaged type combination.
delphi dynamic arrays not valid interop types. need have caller allocate array, , let callee populate it.
procedure populatearray(arr: pinteger; var len: integer); stdcall; var i: integer; returnarray: tarray<integer>; begin returnarray := getarray; len := min(len, length(returnarray)); := 0 len - 1 begin arr^ := returnarray[i]; inc(arr); end; end;
do note export
has no meaning, ignored, , should removed sake of simplicity.
from c# calling code is:
[dllimport("...", callingconvention = callingconvention.stdcall)] public static extern void populatearray( [in, out] [marshalas(unmanagedtype.lparray, sizeparamindex = 1)] int[] arr, ref int len ); .... int[] arr = new int[50]; int len = arr.length; populatearray(arr, ref len); // len contains actual length
Comments
Post a Comment