从char* 或者const char*或者string转化成LPWSTR

利用windows的函数MultiByteToWideChar可以达到这个目的


LPWSTR Funcs::ConvertCharToLPWSTR(const char * szString)
{
 int dwLen = strlen(szString) + 1;
 int nwLen = MultiByteToWideChar(CP_ACP, 0, szString, dwLen, NULL, 0);//算出合适的长度
 LPWSTR lpszPath = new WCHAR[dwLen];
 MultiByteToWideChar(CP_ACP, 0, szString, dwLen, lpszPath, nwLen);
 return lpszPath;
}

如果想要从char*转,那就把函数参数的const 去掉。如果想要从string转,实参用c_str()函数转一下就可以了,如下所示。

string msg="你好啊";

ConvertCharToLPWSTR(msg.c_str());

利用windows的函数MultiByteToWideChar可以达到这个目的

LPWSTR Funcs::ConvertCharToLPWSTR(const char * szString)
{
 int dwLen = strlen(szString) + 1;
 int nwLen = MultiByteToWideChar(CP_ACP, 0, szString, dwLen, NULL, 0);//算出合适的长度
 LPWSTR lpszPath = new WCHAR[dwLen];
 MultiByteToWideChar(CP_ACP, 0, szString, dwLen, lpszPath, nwLen);
 return lpszPath;
}

如果想要从char*转,那就把函数参数的const 去掉。如果想要从string转,实参用c_str()函数转一下就可以了,如下所示。

string msg="你好啊";

ConvertCharToLPWSTR(msg.c_str());

猜你喜欢

转载自blog.csdn.net/a_222850215/article/details/80066861