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!
It should be as simple as:
You'll also need header files to make that work:
What this
marshal_as
specialization looks like inside, for the curious:This works because
System::String
is stored as wide characters internally. If you wanted astd::string
, you'd have to perform Unicode conversion with e.g.WideCharToMultiByte
. Convenient thatmarshal_as
handles all the details for you.I flagged this as a duplicate, but here's the answer on how to get from
System.String^
to astd::string
.The trick is make sure you use Interop and marshalling, because you have to cross the boundary from managed code to non-managed code.
My version is:
With Visual Studio 2015, just do this:
C++/CLI
C++/CX