1、string类型转LPCSTR
string str="abcd";
LPCSTR str1=str.c_str();
2、string类型转LPCWSTR
自定义函数 str2LPCWSTR
- 方法一
LPCWSTR stringtoLPCWSTR(string str){
size_t strsize = str.length() + 1;
const size_t newsize = 100;
size_t convertedChars = 0;
wchar_t* wcstring = (wchar_t*)malloc(sizeof(wchar_t) * (str.length() - 1));
mbstowcs_s(&convertedChars, wcstring, strsize, orig.c_str(), _TRUNCATE);
return wcstring;
}
例子:
string str="abcd";
LPCWSTR str1=str2LPCWSTR(str);
- 方法二
LPCWSTR str2LPCWSTR( string str) {
int len = str.length();
int lenbf = MultiByteToWideChar(CP_ACP, 0, str.c_str(), len, 0, 0);
wchar_t* buffer = new wchar_t[lenbf];
MultiByteToWideChar(CP_ACP, 0, str.c_str(), len, buffer, sizeof(wchar_t) * lenbf);
buffer[len] = 0;
return buffer;
}
来源:CSDN
作者:一只小白的进阶
链接:https://blog.csdn.net/qq_39570626/article/details/103793037