Your code works fine, but as you observe it is awkward. For interop like this I would bite the bullet and use the $POINTERMATH
directive. This allows you to treat a pointer as if it were an array, just as you do in C or C++. For example:
{$POINTERMATH ON}
function GetSum(arr: PDouble; len: Integer): Double; cdecl;
var
i: Integer;
begin
Result := 0.0;
for i := 0 to len-1 do
Result := Result + arr[i];
end;
Another option would be to copy to a native Delphi array and use that for onward processing. Obviously that involves a copy, but sometimes that's actually what you want. In that case you could do it like this:
var
x: TArray<Double>;
....
SetLength(x, len);
Move(arr^, Pointer(x)^, len*SizeOf(arr^));
Or if you don't like the use of Move
, a good old loop:
{$POINTERMATH ON}
var
i: Integer;
x: TArray<Double>;
....
SetLength(x, len);
for i := 0 to len-1 do
x[i] := arr[i];