title: Delphi-to-C dll: Passing Arrays
body: <p>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:</p>
  
  <pre><code>program CallcCode
  uses
    SysUtils, Windows, 
    DLLUnit in 'DLLUnit.pas'; // Header conversion
  
  var
    DLLHandle: cardinal;
    n: Integer;
    A: TArray&lt;Integer&gt;;
    result1: Integer; 
  
  begin
    // Initialize each Array
    SetLength(A,n);
    A[0] = ...;
  
    // Load the DLL (and confirm its loaded)
    DLLhandle := LoadLibrary('dllname.dll');
      if DLLhandle &lt;&gt; 0 then
        begin
         result1 := dll_func1(n,A); // A and B are not passed correctly
      end
    FreeLibrary(DLLHandle);
  end.
  </code></pre>
  
  <p>I successfully "Trace into" dll_func1 the first time, entering DLLUnit, which has:</p>
  
  <pre><code>const
    nameofDLL = 'dllname';
  function dll_func1(n: Integer; A: TArray&lt;Integer&gt;): Integer; cdecl; external nameofDLL;
  </code></pre>
  
  <p>"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:</p>
  
  <pre><code>[-]  A       :(Aplha-Numeric)
      ..[0]    0 (0x00000000)
  </code></pre>
  
  <p>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</p>
  
  <pre><code>function dll_func1(n: Integer; A: PInteger): Integer; cdecl; external nameofDLL;
  ...
  result1 := dll_func1(n,PInteger(A))
  </code></pre>
  
  <p>or</p>
  
  <pre><code>function dll_func1(n: Integer; A: PInteger): Integer; cdecl; external nameofDLL;
  ...
  result1 := dll_func1(n,@A[0])
  </code></pre>
  
  <p>(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?</p>
  
  <p><strong>EDIT</strong> The function in C as:</p>
  
  <pre><code>int dll_func1(int n, int A [])
  </code></pre>