Convert CString to std::wstring

Viewed 36258

How can I convert from CString to std::wstring?

4 Answers

To convert CString to std::wstring:

CString hi("Hi");
std::wstring hi2(hi);

And to go the other way, use c_str():

std::wstring hi(L"Hi");
CString hi2(hi.c_str());

This should work as CString has operator LPCTSTR() defined:

CString s;
std::wstring s1 = s;

Try this:

std::wstring strString((LPCTSTR)strCString);
CString s = _T("Привет");
USES_CONVERSION;
std::wstring ws(A2W((LPCTSTR)s));
Related