Convert a String^ to wstring C++

10,244

It should be as simple as:

std::wstring result = msclr::interop::marshal_as<std::wstring>(curItem);

You'll also need header files to make that work:

#include <msclr\marshal.h>
#include <msclr\marshal_cppstd.h>

What this marshal_as specialization looks like inside, for the curious:

#include <vcclr.h>
pin_ptr<WCHAR> content = PtrToStringChars(curItem);
std::wstring result(content, curItem->Length);

This works because System::String is stored as wide characters internally. If you wanted a std::string, you'd have to perform Unicode conversion with e.g. WideCharToMultiByte. Convenient that marshal_as handles all the details for you.

Share:
10,244
Kipcak08
Author by

Kipcak08

Updated on July 10, 2022

Comments

  • Kipcak08
    Kipcak08 almost 2 years

    I programmed a little Application in C++. There is a ListBox in the UI. And I want to use the selected Item of ListBox for an Algorithm where I can use only wstrings.

    All in all I have two questions: -how can I convert my

        String^ curItem = listBox2->SelectedItem->ToString();
    

    to a wstring test?

    -What means the ^ in the code?

    Thanks a lot!

  • Ben Voigt
    Ben Voigt over 11 years
    Yuck. I refuse to reinvent this particular wheel, especially since the reinvention uses wrongly-named functions. There's no HGLOBAL here at all. Using that code is a documentation nightmare, because you have to write comments explaining what the not-an-HGLOBAL pointers actually are.
  • Maurice Reeves
    Maurice Reeves over 11 years
    Actually, your answer is much better. I need to look into the marshal_as method. +1. Thanks for pointing the way.
  • Ben Voigt
    Ben Voigt over 11 years
    I really want some help eradicating (well, downvoting) every answer that suggests StringToHGlobalAnsi for this. But that's a job for another day.
  • Kipcak08
    Kipcak08 over 11 years
    Thanks Ben Voigt - I will test it - give me 5 mins :-)
  • Maurice Reeves
    Maurice Reeves over 11 years
    That'll be difficult, as this is the answer that even Microsoft recommends for this type of problem: msdn.microsoft.com/en-us/library/…
  • Ben Voigt
    Ben Voigt over 11 years
    That's the StringToHGlobalAnsi page, they have to use that function. By no means does an example somewhere indicate that it's the best way to perform the task.
  • Ben Voigt
    Ben Voigt over 11 years
    By the way, this doesn't get you a wide string.
  • Maurice Reeves
    Maurice Reeves over 11 years
    Right. But if you do a search, that's one of the more...referenced pages. For example: support.microsoft.com/kb/311259 Which I understand if your point. :-D
  • Kipcak08
    Kipcak08 over 11 years
    ben can u tell me from where I get marshal_as? Also I have include the header file
  • Ben Voigt
    Ben Voigt over 11 years
    @user1557170: oops, you need a second header file. Sorry about that.
  • Kipcak08
    Kipcak08 over 11 years
    I'm now a little bit confused - can u simply tell me which code I need ? I'm not a pro programmer like u
  • Ben Voigt
    Ben Voigt over 11 years
    Yes, which also mentions marshal_as. They could do a lot better job of emphasizing which one is better, though.
  • Ben Voigt
    Ben Voigt over 11 years
    Just the first line, plus the two header files. Everything below the divider just explains what marshal_as actually does inside.
  • Kipcak08
    Kipcak08 over 11 years
    should I need a speacial header file?
  • Kipcak08
    Kipcak08 over 11 years
    My compiler tells me that marshal_as is not defined
  • Maurice Reeves
    Maurice Reeves over 11 years
    I agree. They make mention of when each method is mentioned, but do not indicate which is the best, preferred, etc.
  • Ben Voigt
    Ben Voigt over 11 years
    @user1557170: There's a namespace too. Sorry for leaving that out. You can also say using namespace msclr::interop;
  • Kipcak08
    Kipcak08 over 11 years
    yeah now it's working dude :-) Thanks for the great support you was a big help
  • Kipcak08
    Kipcak08 over 11 years
    can u tell me what is the best way to get an pro programmer like you?
  • Ben Voigt
    Ben Voigt over 11 years
    @user1557170: Practice. Follow tutorials written by experts. Solve small problems. Read other peoples' code that solves big problems. Learn from it. Write more code. Read more code. Ask what makes code good or bad and why.
  • tfinniga
    tfinniga almost 10 years
    I get error C2039: 'Data' : is not a member of 'System::String'
  • Pol
    Pol over 8 years
    This work on WinRT with C++/CX not on the older CLR with C++/CLI.
  • Juv
    Juv almost 4 years
    I like this, because this is straight forward: String^ is in UTF16, and if std::wstring should also remain in UTF16 - there is no conversion needed just plain old copy of the characters. Voted Up.
  • Robinson
    Robinson over 3 years
    May I ask, does msclr::interop::marshal_as require any cleanup?
  • Ben Voigt
    Ben Voigt over 3 years
    @Robinson: There are two groups of conversions, ones that require cleanup require you to create a marshal_context instance that performs the cleanup when it goes out of scope. Ones that don't require cleanup (because they return a smart object like std::string that knows how to clean itself up) use marshal_as. You can't get this wrong unless you do something crazy like intentionally leak a marshal_context object.