Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8807307
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:20:17+00:00 2026-06-14T02:20:17+00:00

The question title is basically what I’d like to ask: [MarshalAs(UnmanagedType.LPStr)] – how does

  • 0

The question title is basically what I’d like to ask:

[MarshalAs(UnmanagedType.LPStr)] – how does this convert utf-8 strings to char* ?

I use the above line when I attempt to communicate between c# and c++ dlls;
more specifically, between:

somefunction(char *string) [c++ dll]

somefunction([MarshalAs(UnmanagedType.LPStr) string text) [c#]

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:

  1. the c# string was successfully converted to char*

  2. the resulting char* properly reflects the corresponding utf-8 chars (including the bit in Korean) in the watch window.

Here’s a screenshot (with more details):

ss

As you can see, initialScriptText[0] returns the single byte(char): ‘B’ and the contents of char* initialScriptText are displayed properly (including Korean) in the VS watch window.

Going through the char pointer, it seems that English is saved as one byte per char, while Korean seems to be saved as two bytes per char. (the Korean word in the screenshot is 3 letters, hence saved in 6 bytes)

This seems to show that each ‘letter’ isn’t saved in equal size containers, but differs depending on language. (possible hint on type?)

I’m trying to achieve the same result in pure c++: reading in utf-8 files and saving the result as char*.

Here’s an example of my attempt to read a utf-8 file and convert to char* in c++:

enter image description here

observations:

  1. loss in visual when converting from wchar_t* to char*
  2. since result, s8 displays the string properly, I know I’ve converted the utf-8 file content in wchar_t* successfully to char*
  3. 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 char*.

(the screenshot also shows my terrible failure in using wcstombs)

note: I’m using the utf8 header from (http://utfcpp.sourceforge.net/)

Please correct me on any mistakes in my code/observations.

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T02:20:18+00:00Added an answer on June 14, 2026 at 2:20 am

    [MarshalAs(UnmanagedType.LPStr)] – how does this convert utf-8 strings to char* ?

    It doesn’t. There is no such thing as a “utf-8 string” in managed code, strings are always encoded in utf-16. The marshaling from and to an LPStr is done with the default system code page. Which makes it fairly remarkable that you see Korean glyphs in the debugger, unless you use code page 949.

    If interop with utf-8 is a hard requirement then you need to use a byte[] in the pinvoke declaration. And convert back and forth yourself with System.Text.Encoding.UTF8. Use its GetString() method to convert the byte[] to a string, its GetBytes() method to convert a string to byte[]. Avoid all this if possible by using wchar_t[] in the native code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I hope the title is chosen well enough to ask this question. Feel free
Tough question to ask, but basically I have a web form with a title,
Apologies for the very broadly title question. Basically this follows on from my earlier
Not even really sure what kind of title this question needs. Basically I have
I was unsure as to how to title this question... Basically, I've downloaded some
The title of this question should be self explanatory, but basically I have this
Excuse the question title, couldn't think how to describe this.. Basically, using a few
Question basically as in the title. I could use c/c++/objective-c. I would like to
I'm not really sure how to title this question but basically I have an
This seems like a trivial question where I'm likely stumped on the syntax. Basically,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.