【发布时间】:2018-03-30 13:44:25
【问题描述】:
我正在尝试将字符串转换为“LPCTSTR”,但是出现以下错误。
错误:
cannot convert from 'const char *' to 'LPCTSTR'
代码:
std::string str = "helloworld";
LPCTSTR lp = str.c_str();
也试过了:
LPCTSTR lp = (LPCTSTR)str.c_str();
但是,打印垃圾值。
【问题讨论】:
-
记住
str.c_str()的返回是临时的,你也不应该在这里投。 -
您的构建系统中是否定义了
UNICODE,所以LPCTSTR是LPCWSTR?如果是这样:考虑使用std::wstring。 -
你调查过
LPCTSTR到底是什么吗? -
停止使用 TCHAR 类型,它们在十多年前就不再有意义了。使用 mbstowcs() 或 MultiByteToWideChar() 将 char 字符串转换为 utf16。或者在这个 sn-p 中始终使用 wchar_t,std::wstring。
标签: c++ windows visual-studio tchar widestring