title: pass char * to C DLL from C# string
body: <p>I need to use a library of C functions in a DLL from a C# application.  I am having trouble calling DLL functions with char * arguments: </p>
  
  <p>The C DLL:</p>
  
  <pre><code>extern "C" __declspec(dllexport) int CopyFunc(char *, char *);
  int CopyFunc(char *dest, char *src)
  {
      strcpy(dest, src);
      return(strlen(src));
  }
  </code></pre>
  
  <p>the C# app needs to look something like this:</p>
  
  <pre><code>[DllImport("dork.dll")]
  public static extern int CopyFunc(string dst, string src);
  
  int GetFuncVal(string source, string dest)
  {
      return(CopyFunc(dest,source));
  }
  </code></pre>
  
  <p>I've seen examples using string or StringBuilder, or IntPtr as replacements for the char * required by the DLL function prototype, but I've not been able to get any of them to work.  The most common exception I get is that PInvoke is unbalancing the stack because the function call does not match the prototype.  </p>
  
  <p>Is there a simple solution to this? </p>