I'm writing a MFC project by Visual Studio 2015, character set config to "Use Unicode Character set"
I need to convert from std::string
to LPWSTR
to use with some MFC object properties like LVITEM::pszText
in CListCtrl
, AfxMessageBox
, ... So I use this snipset from internet:
String str = "Hello world!";
std::wstring wname(str.begin(), str.end());
LPWSTR lStr = const_cast<wchar_t*>(wname.c_str());
MessageBox(lStr);
This approach work fine. But the problem is that every time I need to convert I must rewrite these statement, and I put this snipset into a function:
LPWSTR convertLPWSTR(std::string &str) {
std::wstring wname(str.begin(), str.end());
return const_cast<wchar_t*>(wname.c_str());
}
/...
String str = "Hello world!";
LPWSTR lStr = convertLPWSTR(str);
MessageBox(lStr);
But the message box output an error string (like error font)
:
Any one know how to fix this? Thanks!