title: Passing char pointer from C# to C++
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/4385783/passing-char-pointer-from-c-sharp-to-c-function">Passing char pointer from C# to c++ function</a>  </p>
  </blockquote>
  
  
  
  <p>I have this type of problem:</p>
  
  <p>I have a c++ function with this signature:</p>
  
  <pre><code>int myfunction ( char* Buffer, int * rotation)
  </code></pre>
  
  <p>The buffer parameter must be filled with space chars ( 0x20 hex )</p>
  
  <p>In C++ i can solve the problem simply doing this:</p>
  
  <pre><code>char* buffer = (char *)malloc(256);
  memset(buffer,0x20,256);
  res = myfunction (buffer, rotation);
  </code></pre>
  
  <p>I'm trying to call this function from C#.</p>
  
  <p>This is my p/invoke declaration:</p>
  
  <pre><code>[DllImport("mydll.dll", CharSet = CharSet.Ansi, SetLastError = true)]
  private static extern unsafe int myfunction (StringBuilder Buffer, int* RotDegree);
  </code></pre>
  
  <p>In my C# class i've tried to do this:</p>
  
  <pre><code>StringBuilder buffer = new StringBuilder(256);
  buffer.Append(' ', 256);
  ...
  myfunction(buffer, rotation);
  </code></pre>
  
  <p>but it doesn't work....</p>
  
  <p>Anyone can help me?</p>
  
  <p>Thanks.</p>