title: Marshal string to asciiz
body: <p>I'm trying to invoke a DLL function that requires Asciiz pointers:</p>
  
  <pre><code>int doExample(Asciiz* FolderPath, Asciiz* OutputPath, int Flags);
  </code></pre>
  
  <p>I've tried using <code>StringBuilder</code>, but I keep getting errors about accessing protected memory. This is my C# code:</p>
  
  <pre><code>public class Example
  {
    [DllImport("Example.dll", EntryPoint="?doExample@Example@@QADHPYDH0P6A_N0ZZ000@Z")]
          static extern int doExample(StringBuilder folderPath, StringBuilder outputPath, int flags);
  
    public int do(string folderPath, string outputPath)
    {
      StringBuilder FolderPath = new StringBuilder(folderPath);
      StringBuilder OutputPath = new StringBuilder(outputPath);
      doExample(FolderPath, OutputPath, 0);
    }
  }
  </code></pre>
  
  <p>Any ideas as to why this would be happening? Or would this strictly be an error in the C++ code?</p>
  
  <p>Edit: based on the comments, here is so more info:</p>
  
  <ul>
  <li>The DLL is built on VS2010</li>
  <li>I am building the C# code in VS2012 (could the difference here be causing an issue?)</li>
  <li>I don't know exactly what the function in the DLL does; my knowledge in C++ is very limited. Basicly, it gets a directory, compresses it and outputs it to the outputpath in a single file.</li>
  <li>I know the name mangling in the EntryPoint is incorrect; I've put some random characters in; the function name is not the same as it is in the actual DLL, but argument types and return type is the same. Also, I got the real mangled function name from Dependency Walker. If I didn't specify the EntryPoint, it couldn't find the function. (I do have the C++ source code btw of this DLL)</li>
  <li>The full error is: AccessViolationException was unhandled: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.</li>
  </ul>