/******************************************************************************************
Function: ConvertLPWSTRToLPSTR
Description: LPWSTR转char*
Input: lpwszStrIn:待转化的LPWSTR类型
Return: 转化后的char*类型
*******************************************************************************************/
char* ConvertLPWSTRToLPSTR(LPWSTR lpwszStrIn)
{
LPSTR pszOut = NULL;
try
{
if (lpwszStrIn != NULL)
{
int nInputStrLen = wcslen(lpwszStrIn);
// Double NULL Termination
int nOutputStrLen = WideCharToMultiByte(CP_ACP, 0, lpwszStrIn, nInputStrLen, NULL, 0, 0, 0) + 2;
pszOut = new char[nOutputStrLen];
if (pszOut)
{
memset(pszOut, 0x00, nOutputStrLen);
WideCharToMultiByte(CP_ACP, 0, lpwszStrIn, nInputStrLen, pszOut, nOutputStrLen, 0, 0);
}
}
}
catch (std::exception e)
{
}
return pszOut;
}
博客给出了一个将LPWSTR类型转换为char*类型的函数ConvertLPWSTRToLPSTR。函数接收待转化的LPWSTR类型参数,在函数内部进行长度计算和内存分配,使用WideCharToMultiByte函数完成转换,最后返回转化后的char*类型。

1万+

被折叠的 条评论
为什么被折叠?



