Since you are using MFC, you have access to the ATL String Conversion Macros.
This greatly simplifies the conversion vs. using MultiByteToWideChar
. Assuming that filepath
is encoded in your system's default code page, this should do the trick:
CA2W wideFilepath(filepath.c_str());
wstring fp(static_cast<const wchar_t*>(wideFilepath));
If filepath
is not in your system's default code page (let's say it's in UTF-8), then you can specify the encoding to convert from:
CA2W wideFilepath(filepath.c_str(), CP_UTF8);
wstring fp(static_cast<const wchar_t*>(wideFilepath));
To convert the other way, from std::wstring
to std::string
, you would do this:
// Convert from wide (UTF-16) to UTF-8
CW2A utf8Filepath(fp.c_str(), CP_UTF8);
string utf8Fp(static_cast<const char*>(utf8Filepath));
// Or, convert from wide (UTF-16) to your system's default code page.
CW2A narrowFilepath(fp.c_str(), CP_UTF8);
string narrowFp(static_cast<const char*>(narrowFilepath));