GetPrivateProfileString Chinese garbled solution

Such as:

Reading the attribute value of the ini file (Chinese) asks about garbled code.
I call the following interface to read the relevant attribute value from the ini configuration file

CString icon_name; 

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



The content of the configuration file is as follows:
[devtree]
wendu=111111111
shidu=I love you and talk to Dashan

The shidu obtained is garbled

Solution:

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());
}

In other words, by default our newly created .ini file (that is, a text file), the storage format is utf-8.. So we need to convert

It's just like http or database operations, you need to do such an action.

Record it, I hope it will help you a little!

Guess you like

Origin blog.csdn.net/kaizi318/article/details/114937920