|
发表于 2020-3-11 11:40:04
|
显示全部楼层
本楼为最佳答案
string 内部实际上是对 char* 的封装,所以,LPCWSTR 也就是 wchar_t*
所以,简单来说楼主的需求就是从窄字节转到宽字节
先调用这个函数将 string 转为 wstring:
- std::wstring gbk2unicode(std::string str_gbk)
- {
- if (str_gbk.size() <= 0) return std::wstring(L"");
- int u_len = MultiByteToWideChar(CP_ACP, 0, str_gbk.c_str(), str_gbk.size(), NULL, 0);
- if (u_len <= 0) return std::wstring(L"");
- wchar_t* lpsz_w = nullptr;
- try
- {
- lpsz_w = new wchar_t[u_len + 2];
- memset(lpsz_w, 0, (u_len + 2)*sizeof(wchar_t));
- }
- catch (bad_alloc& e)
- {
- cout << "gbk2unicode : " << e.what() << endl;
- return std::wstring(L"");
- }
- int nRtn = MultiByteToWideChar(CP_ACP, 0, str_gbk.c_str(), str_gbk.size(), lpsz_w, u_len);
- if (nRtn != u_len)
- {
- delete[] lpsz_w;
- return std::wstring(L"");
- }
- std::wstring str_unicode;
- str_unicode = lpsz_w;
- delete[] lpsz_w;
- return str_unicode;
- }
复制代码
之后,返回的 wstring 对象调用其 .c_str() 方法就是 LPCWSTR 类型啦 |
评分
-
查看全部评分
|