How can I convert CString to WCHAR* and std::string to WCHAR* in C++, not wchar_t*

895 Views Asked by At

I want to convert CString to WCHAR* or std::string to WCHAR* in Visual Studio C++ MFC Application. I found in stackoverflow that it only has the solution for convert to wchar_t*.

Please help me. Thank you!

2

There are 2 best solutions below

3
Yujian Yao - MSFT On

When you use CString, you could access the CW2A ATL macro,which converts CString to string.

CString theCStr;
std::string STDStr( CW2A( theCStr.GetString(), CP_UTF8 ) );

Or refer to this document.

3
Vlad Feinstein On

I believe CString class can do such conversion:

CStringA a = "ANSI";
CStringW w(a);