宽字节转换为单字节

//使用过后必须释放
char* ConvertToLPCSTR(CString swString)
{
#ifdef _UNICODE
char* szBuf = NULL;
UINT nLen = NULL;

nLen = WideCharToMultiByte(CP_ACP, 0, swString/*wszString*/, -1, NULL, 0, NULL, NULL);

szBuf = (char*)malloc(nLen + 1);

int nRet = WideCharToMultiByte(CP_ACP, 0, swString, -1, szBuf, nLen, NULL, NULL);

if (0 == nRet)
{
free(szBuf);
szBuf = NULL;
}
return szBuf;
#else
char* szBuf = NULL;
int nLen=swString.GetLength()+1;
szBuf = (char*)malloc(nLen + 1);
strcpy(szBuf,swString);
return szBuf;
#endif
}

注意:使用过后必须使用free释放

此条目发表在C++分类目录。将固定链接加入收藏夹。

发表评论