将string转化到WCHAR

1.
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;
}
2.
将第一步反回的wstring类型利用它的c_str()方法转化成char类型的指针

即:

LPCWSTR result = "".c_str();

如:
string str;
std::wstring stemp = s2ws(str);

LPCWSTR result = stemp.c_str();



你可能感兴趣的:(C++,c,C#)