1. 程式人生 > >CString轉string、string轉CString

CString轉string、string轉CString

CString轉string

	// CString to String
	string CString2String(CString strIn)
	{
		int nSize = ::WideCharToMultiByte(CP_ACP, 0, strIn, -1, NULL, 0, NULL, 0);
		char* pChar = (char*)malloc((nSize + 1) * sizeof(char));
		if (NULL != pChar)
		{
			memset(pChar, 0, (nSize + 1) * sizeof(char));

			::WideCharToMultiByte(CP_ACP, 0, strIn, -1, pChar, nSize, NULL, 0);
			string strOut(pChar);
			
			free(pChar);
			pChar = NULL;
			return strOut;
		}
		return "";
	}

string轉CString

	// String to CString
	CString String2CString(string strIn)
	{
		CString strOut(strIn.c_str());
		return strOut;
	}