title: Pass writeable StringBuilder array to C++ from C#
body: <p>I have some of unmanaged C++ dynamic library and C# GUI application, using it. I need to pass array of strings with known size to C++ library, which populates it. Also there is maximum string length value. I'm trying to pass array of <code>StringBuilder</code> with suitable size itself, and suitable internal elements capacity (aka array of buffers).</p>
  
  <pre><code>// C++ library part    
  #define MAX_STRING_LENGTH 200
  const wchar_t* stringToPopulate = L"Hello, World";
  
  MYDLL_API uint8_t __stdcall getSomeStrings(wchar_t** strings, uint64_t count) {
      for(auto i =0; i &lt; count; ++i){
          // trivial string population
          wcsncpy(*(providers + i), stringToPopulate,
                  min(wcslen(stringToPopulate), MAX_STRING_LENGTH ));     
      }
      return 0;
  }
  
  
  // C# application part
  [DllImport("myDllName.dll", CallingConvention = CallingConvention.StdCall)]
  static extern Byte getSomeStrings(
      [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)] StringBuilder[] strings,
      UInt64 size
  );
  
  // calling
  // create suitable string array
  StringBuilder[] stringArrayToPass= new StringBuilder[desirableStringCount];         
  for(ulong i =0; i &lt; desirableStringCount; ++i) {
      stringArrayToPass[i] = new StringBuilder(maxStringLength);
  }
  
  res = getSomeStrings(stringArrayToPass, desirableStringCount); // exception
  </code></pre>
  
  <p>I've got <code>System.AccessViolationException</code> exception on the library API call. What can be a reason of such issue?</p>