title: MarshalAs(UnmanagedType.LPStr) - how does this convert utf-8 strings to char*
body: <p>The question title is basically what I'd like to ask:</p>
  
  <p><code>[MarshalAs(UnmanagedType.LPStr)]</code> - how does this convert utf-8 strings to char* ?</p>
  
  <p>I use the above line when I attempt to communicate between c# and c++ dlls;
  more specifically, between:</p>
  
  <pre><code>somefunction(char *string) [c++ dll]
  
  somefunction([MarshalAs(UnmanagedType.LPStr) string text) [c#]
  </code></pre>
  
  <p>When I send my utf-8 text (scintilla.Text) through c# and into my c++ dll,
  I'm shown in my VS 10 debugger that:</p>
  
  <ol>
  <li><p>the c# string was successfully converted to <code>char*</code></p></li>
  <li><p>the resulting <code>char*</code> properly reflects the corresponding utf-8 chars (including the bit in Korean) in the watch window.</p></li>
  </ol>
  
  <p>Here's a screenshot (with more details):</p>
  
  <p><a href="https://i.stack.imgur.com/uVpEv.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/uVpEv.png" alt="ss"></a></p>
  
  <p>As you can see, <code>initialScriptText[0]</code> returns the single <code>byte(char)</code>: 'B' and the contents of  <code>char* initialScriptText</code> are displayed properly (including Korean) in the VS watch window.</p>
  
  <p>Going through the <code>char</code> pointer, it seems that English is saved as one <code>byte</code> per <code>char</code>, while Korean seems to be saved as two bytes per <code>char</code>. (the Korean word in the screenshot is 3 letters, hence saved in 6 bytes)</p>
  
  <p>This seems to show that each 'letter' isn't saved in equal size containers, but differs depending on language. (possible hint on type?)</p>
  
  <p>I'm trying to achieve the same result in pure c++: reading in utf-8 files and saving the result as <code>char*</code>. </p>
  
  <p>Here's an example of my attempt to read a utf-8 file and convert to <code>char*</code> in c++:</p>
  
  <p><a href="https://i.stack.imgur.com/1KBpA.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/1KBpA.png" alt="enter image description here"></a></p>
  
  <p>observations:</p>
  
  <ol>
  <li>loss in visual when converting from <code>wchar_t*</code> to <code>char*</code></li>
  <li>since result, s8 displays the string properly, I know I've converted the utf-8 file content in <code>wchar_t*</code> successfully to <code>char*</code></li>
  <li>since 'result' retains the bytes I've taken directly from the file, but I'm getting a different result from what I had through c# (I've used the same file), I've concluded that the c# marshal has put the file contents through some other procedure to further mutate the text to <code>char*</code>. </li>
  </ol>
  
  <p>(the screenshot also shows my terrible failure in using wcstombs)</p>
  
  <p>note: I'm using the utf8 header from (<a href="http://utfcpp.sourceforge.net/" rel="nofollow noreferrer">http://utfcpp.sourceforge.net/</a>)</p>
  
  <p>Please correct me on any mistakes in my code/observations.</p>
  
  <p>I'd like to be able to mimic the result I'm getting through the c# marshal and I've realised after going through all this that I'm completely stuck. Any ideas?</p>