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.
Author by
Kipcak08
Updated on July 10, 2022Comments
-
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 over 11 yearsYuck. 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 over 11 yearsActually, your answer is much better. I need to look into the marshal_as method. +1. Thanks for pointing the way.
-
Ben Voigt over 11 yearsI really want some help eradicating (well, downvoting) every answer that suggests
StringToHGlobalAnsi
for this. But that's a job for another day. -
Kipcak08 over 11 yearsThanks Ben Voigt - I will test it - give me 5 mins :-)
-
Maurice Reeves over 11 yearsThat'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 over 11 yearsThat'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 over 11 yearsBy the way, this doesn't get you a wide string.
-
Maurice Reeves over 11 yearsRight. 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 over 11 yearsben can u tell me from where I get marshal_as? Also I have include the header file
-
Ben Voigt over 11 years@user1557170: oops, you need a second header file. Sorry about that.
-
Kipcak08 over 11 yearsI'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 over 11 yearsYes, which also mentions
marshal_as
. They could do a lot better job of emphasizing which one is better, though. -
Ben Voigt over 11 yearsJust the first line, plus the two header files. Everything below the divider just explains what
marshal_as
actually does inside. -
Kipcak08 over 11 yearsshould I need a speacial header file?
-
Kipcak08 over 11 yearsMy compiler tells me that marshal_as is not defined
-
Maurice Reeves over 11 yearsI agree. They make mention of when each method is mentioned, but do not indicate which is the best, preferred, etc.
-
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 over 11 yearsyeah now it's working dude :-) Thanks for the great support you was a big help
-
Kipcak08 over 11 yearscan u tell me what is the best way to get an pro programmer like you?
-
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 almost 10 yearsI get
error C2039: 'Data' : is not a member of 'System::String'
-
Pol over 8 yearsThis work on WinRT with
C++/CX
not on the older CLR withC++/CLI
. -
Juv almost 4 yearsI like this, because this is straight forward:
String^
is in UTF16, and ifstd::wstring
should also remain in UTF16 - there is no conversion needed just plain old copy of the characters. Voted Up. -
Robinson over 3 yearsMay I ask, does msclr::interop::marshal_as require any cleanup?
-
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 likestd::string
that knows how to clean itself up) usemarshal_as
. You can't get this wrong unless you do something crazy like intentionally leak amarshal_context
object.