我正在寻找一种方法,或用于将std :: string转换为LPCWSTR的代码片段
Tor*_*ups 125
感谢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();
Run Code Online (Sandbox Code Playgroud)
小智 106
解决方案实际上比任何其他建议容易得多:
std::wstring stemp = std::wstring(s.begin(), s.end());
LPCWSTR sw = stemp.c_str();
Run Code Online (Sandbox Code Playgroud)
最重要的是,它与平台无关.h2h :)
如果您在ATL/MFC环境中,则可以使用ATL转换宏:
#include <atlbase.h>
#include <atlconv.h>
. . .
string myStr("My string");
CA2W unicodeStr(myStr);
Run Code Online (Sandbox Code Playgroud)
然后,您可以将unicodeStr用作LPCWSTR.unicode字符串的内存在堆栈上创建并释放,然后执行unicodeStr的析构函数.