如何在 vc++ 中将 char* 转换为 LPWSTR .....................
How to convert char* to LPWSTR in vc++..........................?
是否将 char* 转换为 LPWSTR 的正确方法?
void convertcharpointerToLPWSTR(char *a)
{
int nSize = MultiByteToWideChar(CP_ACP, 0, a, -1, NULL, 0);
LPWSTR a_LPWSTR = new WCHAR[nSize];
MultiByteToWideChar(CP_ACP, 0, a, -1, a_LPWSTR, nSize);
}
您的实现要么导致内存泄漏,要么使调用者响应释放您的函数分配的内存,这始终是一个非常错误和糟糕的模式。你应该更好地 return 一个关心自己内存的对象,就像 std::wstring 那样:
inline std::wstring a2w(LPCSTR psz, UINT codepage)
{
if (!psz || *psz == 0)
return std::wstring();
int nLen = int(strlen(psz));
int resultChars = ::MultiByteToWideChar(codepage, 0, psz, nLen, nullptr, 0);
std::wstring result(resultChars, (wchar_t)0);
::MultiByteToWideChar(codepage, 0, psz, nLen, &result[0], resultChars);
return result;
}
是否将 char* 转换为 LPWSTR 的正确方法?
void convertcharpointerToLPWSTR(char *a)
{
int nSize = MultiByteToWideChar(CP_ACP, 0, a, -1, NULL, 0);
LPWSTR a_LPWSTR = new WCHAR[nSize];
MultiByteToWideChar(CP_ACP, 0, a, -1, a_LPWSTR, nSize);
}
您的实现要么导致内存泄漏,要么使调用者响应释放您的函数分配的内存,这始终是一个非常错误和糟糕的模式。你应该更好地 return 一个关心自己内存的对象,就像 std::wstring 那样:
inline std::wstring a2w(LPCSTR psz, UINT codepage)
{
if (!psz || *psz == 0)
return std::wstring();
int nLen = int(strlen(psz));
int resultChars = ::MultiByteToWideChar(codepage, 0, psz, nLen, nullptr, 0);
std::wstring result(resultChars, (wchar_t)0);
::MultiByteToWideChar(codepage, 0, psz, nLen, &result[0], resultChars);
return result;
}