title: Pass writable string array to C++ from C# via P/Invoke
body: <p>I'm trying to pass a writable (pre-allocated) string array from C# to a C++ dll. It fails with "Access violation writing location".</p>
  
  <p>C++:  </p>
  
  <pre><code>int StringArrayTest(size_t numberOfStrings, char **valueOut, size_t maxStringLength) {
      for (unsigned int i = 0; i &lt; numberOfStrings; i++) {
          auto str = std::to_string(i); //Create simple string
          strncpy(valueOut[i], str.c_str(), maxStringLength); //Copy to output
      } 
      return 0;
  }
  </code></pre>
  
  <p>C#:  </p>
  
  <pre><code>[DllImport("MyDLL", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)] 
  static extern int StringArrayTest(ulong arraySize, [MarshalAs(UnmanagedType.LPArray)]StringBuilder[] valuesOut, ulong maxStringLength);
  
  public string[] GetTestStrings(ulong arraySize, ulong maxStringLength) {
      var stringBuilder = new StringBuilder[(int)arraySize];
      for (var i = 0; i &lt; (int)arraySize; i++) {
          stringBuilder[i] = new StringBuilder((int)maxStringLength);
      }
      var result = StringArrayTest(arraySize, stringBuilder, maxStringLength);
      var returnValues = new string[arraySize];
      for (var i = 0; i &lt; (int)arraySize; i++) {
          returnValues[i] = result.ToString();
      }
      return returnValues;
  }
  </code></pre>
  
  <p>Note that doing this with a single string (char * signature and passing single StringBuilder) works as intended.</p>