不能将convert std::string to LPWSTR放入函数中
MFC: Cannot put convert std::string to LPWSTR into a function
我正在用Visual Studio 2015编写一个MFC项目,字符集配置为"使用Unicode字符集"
我需要从std::string
转换到LPWSTR
,以使用一些MFC对象属性,如LVITEM::pszText
在CListCtrl
, AfxMessageBox
,…所以我使用这个来自internet的代码片段集:
String str = "Hello world!";
std::wstring wname(str.begin(), str.end());
LPWSTR lStr = const_cast<wchar_t*>(wname.c_str());
MessageBox(lStr);
这个方法工作得很好。但问题是,每次我需要转换,我必须重写这些语句,我把这个片段集到一个函数:
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);
但是消息框输出一个错误字符串(如错误字体)
:
有谁知道怎么解决这个问题吗?谢谢!
为什么不使用
CString str = _T("Hello world!") ;
相关文章:
- "error: no matching function for call to"构造函数错误
- 什么时候调用组成单元对象的析构函数
- 继承函数的重载解析
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- C++模板来检查友元函数的存在
- 递归函数计算序列中的平方和(并输出过程)
- 对RValue对象调用的LValue ref限定成员函数
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 在C++STL中是否有Polyval(Matlab函数)等价物?
- 为什么使用 "this" 指针调用派生成员函数?
- 将对象数组的引用传递给函数
- 函数调用中参数的顺序重要吗
- 函数向量_指针有不同的原型,我可以构建一个吗
- 使用不带参数的函数访问结构元素
- AfxBeginThread破坏类中作为参数传递给线程函数的LPWSTR值.为什么?
- 将 LPWSTR& foo 返回给函数
- 如何在使用GetDlgItemText()函数时计算LPWSTR大小
- Int到LPWSTR函数的问题
- 不能将convert std::string to LPWSTR放入函数中