title: Call delphi dll function from C# passing in a byte array
body: <p>I'm having trouble figuring out the best way to have a delphi function operate on a byte array from .net.</p>
  
  <p>The delphi signature looks like this:</p>
  
  <pre><code>procedure Encrypt(
      var Bytes: array of byte;
      const BytesLength: Integer;
      const Password: PAnsiChar); stdcall; export;
  </code></pre>
  
  <p>The C# code looks like this:</p>
  
  <pre><code>[DllImport("Encrypt.dll",
      CallingConvention = CallingConvention.StdCall,
      CharSet = CharSet.Ansi)]
  public static extern void Encrypt(
      ref byte[] bytes,
      int bytesLength,
      string password);
  </code></pre>
  
  <p>Omitting <code>var</code> and <code>ref</code> before  the byte array declaration seemed to fail, but is it required since I'll be changing only the contents of the array and not the array itself?</p>
  
  <p>Also, for some reason I can't seem to get the length of the array in delphi, if I remove the <code>BytesLength</code> parameter than <code>Length(Bytes)</code> will not work, if I add the <code>BytesLength</code> parameter, <code>Length(Bytes)</code> starts to work but <code>BytesLength</code> has a wrong value.</p>