title: Calling from C# to C function which accept a struct array allocated by caller
body: <p>I have the following C struct</p>
  
  <pre><code>struct XYZ
  {
  void            *a;
  char            fn[MAX_FN];     
  unsigned long   l;          
  unsigned long   o;  
  };
  </code></pre>
  
  <p>And I want to call the following function from C#:</p>
  
  <pre><code>extern "C"  int     func(int handle, int *numEntries, XYZ *xyzTbl);
  </code></pre>
  
  <p>Where xyzTbl is an array of XYZ of size numEntires which is allocated by the caller</p>
  
  <p>I have defined the following C# struct:  </p>
  
  <pre><code>[StructLayoutAttribute(Sequential, CharSet = CharSet.Ansi)]
  public struct XYZ
  {
     public System.IntPtr rva;
     [MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 128)]
     public string fn;
     public uint l;
     public uint o;
  }
  </code></pre>
  
  <p>and a method:</p>
  
  <pre><code> [DllImport(@"xyzdll.dll", CallingConvention = CallingConvention.Cdecl)]
   public static extern Int32 func(Int32 handle, ref Int32 numntries,
       [MarshalAs(UnmanagedType.LPArray)] XYZ[] arr);
  </code></pre>
  
  <p>Then I try to call the function : </p>
  
  <pre><code>XYZ xyz = new XYZ[numEntries];
  for (...) xyz[i] = new XYZ();
  func(handle,numEntries,xyz);
  </code></pre>
  
  <p>Of course it does not work. Can someone shed light on what I am doing wrong ?</p>