title: How to get char** using C#?
body: <p>I need to pass an argument to an unsafe DllImported function in the form of:</p>
  
  <pre><code>[DllImport("third_party.dll")]
  private static extern unsafe int start(int argc, char** argv);
  </code></pre>
  
  <p>I'm assuming it's an array of strings.  However when I try to do the following, I get 'Cannot convert from string[] to char**' error.  How do I go about getting this to work?  Thanks.</p>
  
  <pre><code>string[] argv = new string[] { };
  start(0, argv);
  </code></pre>
  
  <p><strong>EDIT 1:</strong>
  The question was marked as duplicate, but looking at the possible duplicate question, I still do not see how to get this to work.</p>
  
  <p><strong>EDIT 2:</strong> To further clafiry the question and required parameters.  It looks like your standard argc/argv parameters (parameter count, and then parameter values).  The same way you would start a c program: <code>int main(int argc, char** argv);</code>  For this particular problem, I don't want to pass any arguments at all (so count is 0).</p>
  
  <p><strong>EDIT 3:</strong>
  I got more information from the 3rd party library vendor. Here it is:</p>
  
  <ul>
  <li>the first parameter is the count of arguments</li>
  <li><s>the second parameter is an array of null terminated strings</s></li>
  <li>the strings are ANSI encoded</li>
  </ul>
  
  <p><strong>EDIT 4</strong>: Final edit with a working solution (at least in my case).  I would make this the answer, but can't because this question is marked as a duplicate.  Here's a <a href="https://stackoverflow.com/questions/2527014/how-do-i-marshal-a-pointer-to-an-array-of-pointers-to-structures">link</a> to a question that helped me the most.  In the end <strong>the dll function expected an array of pointers to buffers with ANSI strings.</strong>  So my final approach (based off the linked question), was as follows.  Create an array in memory to hold the pointers, then allocate each string elsewhere in memory, and write pointers to those strings inside the first pointer array.  This code works in production:</p>
  
  <pre><code>[DllImport("third_party.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
  private static extern int start(Int32 args, IntPtr argv);
  
  public bool start(params string[] arguments)
  {
      int result;
  
      if (arguments == null || arguments.Length == 0)
      {
          result = dll_system_startup(0, IntPtr.Zero);
      }
      else
      {
          List&lt;IntPtr&gt; allocatedMemory = new List&lt;IntPtr&gt;();
  
          int sizeOfIntPtr = Marshal.SizeOf(typeof(IntPtr));
          IntPtr pointersToArguments = Marshal.AllocHGlobal(sizeOfIntPtr * arguments.Length);
  
          for (int i = 0; i &lt; arguments.Length; ++i)
          {
              IntPtr pointerToArgument = Marshal.StringToHGlobalAnsi(arguments[i]);
              allocatedMemory.Add(pointerToArgument);
              Marshal.WriteIntPtr(pointersToArguments, i * sizeOfIntPtr, pointerToArgument);
          }
  
          result = start(arguments.Length, pointersToArguments);
  
          Marshal.FreeHGlobal(pointersToArguments);
  
          foreach (IntPtr pointer in allocatedMemory)
          {
              Marshal.FreeHGlobal(pointer);
          }
      }
  
      return result == 0;
  }
  </code></pre>