wstring与string互转
- std::wstring StringToWString(const std::string &strSrc)
- {
- int nLen = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)strSrc.c_str(), strSrc.length(), 0, 0);
- if (nLen <= 0) return std::wstring(L"");
-
- WCHAR *pwszDst = new WCHAR[nLen + 1];
- if (NULL == pwszDst) return std::wstring(L"");
- memset(pwszDst, 0, nLen + 1);
-
- MultiByteToWideChar(CP_ACP, 0, (LPCSTR)strSrc.c_str(), strSrc.length(), pwszDst, nLen);
- pwszDst[nLen] = 0;
-
- std::wstring wstrTemp(pwszDst);
- delete pwszDst;
- pwszDst = NULL;
- return wstrTemp;
- }
-
- std::string WStringToString(const std::wstring &wstrSrc)
- {
- int nLen = WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)wstrSrc.c_str(), -1, NULL, 0, NULL, NULL);
- if (nLen <= 0) return std::string("");
-
- char* pszDst = new char[nLen + 1];
- if (NULL == pszDst) return std::string("");
- memset(pszDst, 0, nLen + 1);
-
- WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)wstrSrc.c_str(), -1, pszDst, nLen, NULL, NULL);
- pszDst[nLen] = 0;
-
- std::string strTemp(pszDst);
- delete[] pszDst;
- pszDst = NULL;
- return strTemp;
- }