title: How to write the C# prototype to P/Invoke a function which takes a char* as an argument, and returns its value there
body: <p>As the title suggests, I'm trying to write some C# code to interop with a C DLL (It's a Windows device driver, basically).  The error code function signature in C is:</p>
  
  <pre><code>UINT DLLErrorMsg( DWORD errorCode, char * pBuf, UINT nSize );
  </code></pre>
  
  <p>nSize is the size of pBuf.  If nSize can fit the error message, it is copied into pBuf and 0 returned, otherwise (the buffer was too small and) the return value is the minimum size the buffer needs to be to fit the error message.</p>
  
  <p>I've tried some variants of the following:</p>
  
  <pre><code>internal class DLLWrapper
  {
      [DllImport(_libName, EntryPoint="DLLErrorMsg", CallingConvention=CallingConvention.Cdecl)]
      public static extern UInt32 GetErrorMessage(UInt32 dwError, ref char * pBuf, UInt32 nBufSize);
  }
  </code></pre>
  
  <p>The client code at the moment looks something like this:</p>
  
  <pre><code>GetError( UInt32 errorCode )
  {
      char[] errorMsg = new char[bufSize];            
      UInt32 moreChars = DLLWrapper.GetErrorMessage(errorCode, out errorMsg, bufSize);
      if (moreChars &gt; 0)
      {
          errorMsg = new char[moreChars];
          TWLDLLInterface.GetErrorMessage(errorCode, out errorMsg, moreChars);
      }
  }
  </code></pre>
  
  <p>But I get an exception at the call to GetErrorMessage:</p>
  
  <blockquote>
    <p>An unhandled exception of type 'System.ArgumentException' occurred in NdevInterface.dll</p>
    
    <p>Additional information: Method's type signature is not Interop compatible.</p>
  </blockquote>
  
  <p>I've also tried playing around with IntPtr and trying Marshal.IntPtrToStringAuto(), but that wasn't helpful because I need to allocate the buffer, and I obviously can't cast a char[] into an IntPtr.</p>
  
  <p>I've tried searching through the MSDN and general internet for tips on how to do this, but most of it seems to be a little different than what I'm trying.</p>
  
  <p>What am I doing wrong?</p>