title: How do I call an unmanaged function that has a char[] as OUT parameter from C#?
body: <p>Say, I've got that prototype of a function that is exposed on a DLL:</p>
  
  <pre><code>int CALLBACK worker (char* a_inBuf, int a_InLen,
                       char** a_pOutBuf, int* a_pOutLen,
                       char** a_pErrBuf, int* a_pErrLen)
  </code></pre>
  
  <p>I'm sure that it's ridiculously easy to call that DLL function from my C# code but it doesn't work with this code:</p>
  
  <pre><code>[DllImport("mydll.dll")]  
       public static extern int worker(
           [In, MarshalAs(UnmanagedType.LPArray)] byte[] inBuf,  
           int inputLen,  
           [Out, MarshalAs(UnmanagedType.LPArray)] byte[] outBuf,  
           out int outputLen,  
           [Out, MarshalAs(UnmanagedType.LPArray)] byte[] errBuf,  
           out int errorLen);
  
  ... 
  
  int outputXmlLength = 0;
  int errorXmlLength  = 0;
  
  byte[]  outputXml = null;
  byte[]  errorXml  = null;
  worker(input, input.Length, output, out outputLength, error, out errorLength);
  </code></pre>
  
  <p>I get an access violation when I'm going to fetch the memory for <code>output</code> and <code>error</code> inside my unmanaged library (and therefore de-reference the passed pointer):</p>
  
  <pre><code>*a_ppBuffer = (char*) malloc(size*sizeof(char));
  </code></pre>
  
  <ol>
  <li><p>How do I write the <code>DLLIMPORT</code> statement in my C# code for this function?  </p></li>
  <li><p>How do I actually call the function so that <code>a_pOutBuf</code> and <code>a_pErrBuf</code> are accessible and not <code>null</code> from within <code>worker</code> (i.e. use a real double pointer)?</p></li>
  </ol>