GetPrivateProfileString 中文乱码解决

如:

读取ini文件的属性值(中文)问乱码
我调用下面的接口,从ini配置文件中读取相关的属性值

CString icon_name; 

        GetPrivateProfileString(szTypeName,szIconName,"",icon_name.GetBuffer(MAX_PATH),MAX_PATH,strIniPath);


配置文件内容如下:
[devtree]
wendu=111111111
shidu=我爱你侃大山

获取到的shidu就是乱码

解决方式

CString CProfile::GetString(LPCTSTR strSection, LPCTSTR szName, LPCTSTR szDefault)
{
	char szBuffer[MAX_PATH];
	CString szValue = _T("");
 
	GetPrivateProfileString(strSection, szName, szDefault,
		szBuffer, MAX_PATH, m_szIniName);
	int iLen = strlen(szBuffer);
	if (iLen > 0)
		szValue = szBuffer;
 
	string  strtest = UTF8ToAnsi(szBuffer);
	szValue = strtest.c_str();
	return szValue;
 
}
 
std::wstring UTF8ToUnicode(const char* strSrc)
{
	std::wstring wstrRet;
 
	if (NULL != strSrc)
	{
		int len = MultiByteToWideChar(CP_UTF8, 0, strSrc, -1, NULL, 0) * sizeof(WCHAR);
		WCHAR* strDst = new(std::nothrow) WCHAR[len + 1];
		if (NULL != strDst)
		{
			MultiByteToWideChar(CP_UTF8, 0, strSrc, -1, strDst, len);
			wstrRet = strDst;;
			delete[]strDst;
		}
	}
 
	return wstrRet;
}
std::string UnicodeToAnsi(const WCHAR* strSrc)
{
	std::string strRet;
 
	if (NULL != strSrc)
	{
		int len = WideCharToMultiByte(CP_ACP, 0, strSrc, -1, NULL, 0, NULL, NULL);
		char* strDst = new(std::nothrow) char[len + 1];
		if (NULL != strDst)
		{
			WideCharToMultiByte(CP_ACP, 0, strSrc, -1, strDst, len, NULL, NULL);
			strRet = strDst;
			delete[]strDst;
		}
	}
 
	return strRet;
}
 
std::string UTF8ToAnsi(const char* strSrc)
{
	return UnicodeToAnsi(UTF8ToUnicode(strSrc).c_str());
}

你可能感兴趣的:(java)