Delphi-to-C dll: Passing Arrays

2019-08-08 18:25发布

问题:

I'm using Delphi to load a dll (that I created in Delphi XE-3) for the purposes of interfacing with some C code. My problem is figuring out why my arrays aren't being passed to the c functions - they're the only ones not to. The delphi file (simplified) looks like this:

program CallcCode
uses
  SysUtils, Windows, 
  DLLUnit in 'DLLUnit.pas'; // Header conversion

var
  DLLHandle: cardinal;
  n: Integer;
  A: TArray<Integer>;
  result1: Integer; 

begin
  // Initialize each Array
  SetLength(A,n);
  A[0] = ...;

  // Load the DLL (and confirm its loaded)
  DLLhandle := LoadLibrary('dllname.dll');
    if DLLhandle <> 0 then
      begin
       result1 := dll_func1(n,A); // A and B are not passed correctly
    end
  FreeLibrary(DLLHandle);
end.

I successfully "Trace into" dll_func1 the first time, entering DLLUnit, which has:

const
  nameofDLL = 'dllname';
function dll_func1(n: Integer; A: TArray<Integer>): Integer; cdecl; external nameofDLL;

"Tracing-into" again, I arrive at the c file, which still has the correct n and DLLdefs values, but A (under the "Local Variables" heading) has become:

[-]  A       :(Aplha-Numeric)
    ..[0]    0 (0x00000000)

I know that I'm at least accessing the DLL (hopefully) correctly because other function calls work as they should and I am able to trace into the dll_func1.c file without a problem. I tried changing the function to

function dll_func1(n: Integer; A: PInteger): Integer; cdecl; external nameofDLL;
...
result1 := dll_func1(n,PInteger(A))

or

function dll_func1(n: Integer; A: PInteger): Integer; cdecl; external nameofDLL;
...
result1 := dll_func1(n,@A[0])

(using both TArray and array of Integer or A) but there is no change, which leaves me to believe this is related to a problem I'm not seeing. The whole thing compiles and runs, but result1 is incorrect because of the TArray failures. Any ideas on what is going wrong?

EDIT The function in C as:

int dll_func1(int n, int A [])

回答1:

Your question contains two Delphi declarations for the external function. One of them uses TArray<T> as a parameter. That is completely wrong. Don't do that. You cannot use a Delphi dynamic array as an interop type. The reason being that TArray<T> is a complex managed type that can only be created and consumed by Delphi code.

You need to do as I do below, and as I explained in my answer to your previous question, and declare the array parameter as pointer to element type. For example, PInteger, PDouble, etc.

There's quite a lot of confusion here, and needless complexity. What you need is the simplest possible example that shows how to pass an array from your Delphi code to C code.

Here is is.

C code

//testarray.c

void printDouble(double d); // linker will resolve this from the Delphi code

void test(double *arr, int count)
{
    int i;
    for (i=0; i<count; i++)
    {
        printDouble(arr[i]);
    }
}

Delphi code

program DelphiToC;

{$APPTYPE CONSOLE}

uses
  Crtl;

procedure _printDouble(d: Double); cdecl;
begin
  Writeln(d);
end;

procedure test(arr: PDouble; count: Integer); cdecl; external name '_test';

{$L testarray.obj}

var
  arr: TArray<Double>;

begin
  arr := TArray<Double>.Create(1.0, 2.0, 3.0, 42.0, 666.0);
  test(PDouble(arr), Length(arr));
  Readln;
end.

Compile the C code using, for example, the Borland C compiler like this:

bcc32 -c testarray.c

And the output is:

 1.00000000000000E+0000
 2.00000000000000E+0000
 3.00000000000000E+0000
 4.20000000000000E+0001
 6.66000000000000E+0002

Note that I linked to the C code statically because that was easier for me. Nothing much changes if you put the C code in a DLL.

The conclusion is that the code I gave you in my answer to your previous, and that I repeat here, is correct. That approach succeeds in passing an array from Delphi code to C. It looks like your diagnostics and debugging is in error.

You are only inspecting A[0] so it's hardly surprising that you only see one value. If only you would look at A[1], A[2], ... , A[n-1] you would see that that all the values are being passed correctly. Or perhaps your debugging was carried out on the erroneous declaration of the external function that used TArray<T> as a parameter.