我正在寻找将std::string转换为LPCWSTR的方法或代码片段
发布于 2008-08-26 10:36:58
感谢您提供指向MSDN文章的链接。这正是我要找的。
std::wstring s2ws(const std::string& s)
{
int len;
int slength = (int)s.length() + 1;
len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0);
wchar_t* buf = new wchar_t[len];
MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, buf, len);
std::wstring r(buf);
delete[] buf;
return r;
}
std::wstring stemp = s2ws(myString);
LPCWSTR result = stemp.c_str();
发布于 2010-11-10 07:12:22
实际上,这个解决方案比其他任何建议都简单得多:
std::wstring stemp = std::wstring(s.begin(), s.end());
LPCWSTR sw = stemp.c_str();
最棒的是,它是独立于平台的。
发布于 2008-08-26 02:30:25
如果是在ATL/MFC环境中,则可以使用ATL转换宏:
#include <atlbase.h>
#include <atlconv.h>
. . .
string myStr("My string");
CA2W unicodeStr(myStr);
然后,您可以将unicodeStr用作LPCWSTR。unicode字符串的内存在堆栈上创建并释放,然后执行unicodeStr的析构函数。
https://stackoverflow.com/questions/27220
复制相似问题