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 <msclrmarshal.h>
#include <msclrmarshal_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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…