title: Marshal a C char[][] array to C#
body: <p>I have looked and looked and tried everything I can think of or have found suggested. I'm still not having any luck getting the data I need.  </p>
  
  <p>I'm using a third party DLL, which I believe is written in C. I need to access the functions from this DLL in C#. For the most part, I have this working, except for one function.  The function I'm having problems with has the following header:</p>
  
  <pre><code>uint queryNumOfServers(USHORT *NumOfServers, char ServerNames[8][16]);
  </code></pre>
  
  <p>I have the following declared in my C# application</p>
  
  <pre><code>[DllImport("client.dll", CharSet=CharSet.Ansi]
  public static extern uint queryNumOfServers(ref short numberofServer, StringBuilder serverNames);
  </code></pre>
  
  <p>I call this as follows:</p>
  
  <pre><code>StringBuilder serverNames = new StringBuilder(128);
  short numServers = -1;
  queryNumberOfServers(ref numServers, serverNames);
  </code></pre>
  
  <p>The problem is, while numberOfServers come back equal to 4, I only get one name in serverNames. I have tested the above C function in a small C program. I get back numberOfServer = 4 but I also get 4 names back.  </p>
  
  <hr>
  
  <p>I have tried the following with no success:</p>
  
  <pre><code>[DllImport("client.dll", CharSet=charSet.Ansi, CallingConvention = CallingConvention.StdCall)]
  public static extern uint queryNumOfServers(ref short numberOfServer,[MarshalAs(UnmanagedType.LPStr)] string serverNames);
  </code></pre>
  
  <p>Called this with</p>
  
  <pre><code>string serverNames = new string('\0', 128);
  queryNumOfServers(ref numServers, serverNames);
  </code></pre>
  
  <p>There is no change to serverNames with this option.</p>
  
  <hr>
  
  <p>The development environment is Visual Studio 2008. </p>