我想将 LPCWSTR 转换为 std::string,然后拆分它,然后再次将其转换为 LPCWSTR。但是这个函数打印?????????如何解决?
string convLPCWSTRtostring(const LPCWSTR a) {
long size = wcslen(a);
char* cString = new char[size];
for (long i = 0; i < size-1; i++)
{
cString[i] = a[i];
}
string String(cString);
return(String);
}
LPCWSTR convstringtoLPCWSTR(const string s) {
wstring my_str;
my_str.assign(s.begin(), s.end());
LPCWSTR wide_string = new WCHAR[my_str.size()+1]; //define an array with size of my_str + 1
wide_string = my_str.c_str();
return wide_string;
}
上面的函数是 LPCWSTR->std::string 和 string->LPCWSTR
std::string Split(std::string s, std::string token,int pos)
{
if (token.length() == 0 || s.find(token) == std::string::npos)
return (s);
std::vector<std::string> ret;
int size = 0;
std::string delim = ",";
auto start = 0U;
auto end = s.find(delim);
while (end != std::string::npos)
{
ret.resize(size + 1);
ret[size] = s.substr(start, end - start);
size = size + 1;
start = end + delim.length();
end = s.find(delim, start);
}
ret.resize(size + 1);
ret[size] = s.substr(start, end);
size = size + 1;
return ret[pos];
}
LPCWSTR lpcwstr_split(const LPCWSTR lpcwstr, const int pos) {
string targetStr = convLPCWSTRtostring(lpcwstr);
string token = ",";
std::string ret;
ret = Split(targetStr, token,pos);
LPCWSTR retl;
retl = convstringtoLPCWSTR(ret);
return(retl);
}
这是主要的打印功能。我想打印“123 456 789”,但这会打印“??? ??? ???”
LPCWSTR test = L"123,456,789";
m_api->LoggerOut(MT_RET_OK, L"%s %s %s", lpcwstr_split(test, 0), lpcwstr_split(test, 1), lpcwstr_split(test, 2));