Merci
Pour une de mes fonctions j'avais besoin de convertir un chaine de caracteres ANSI en UNICODE. J'ai trouvé cela particulièrement compliqué pour ce que c'est. J'ai crée la fonction StringToWstring(string strString).
J'aimerais votre avis sur :
Est il obligé de faire si compliquer et long.
D'autre suggestion d'amelioration.
Pourquoi si je rajoute "delete [] lpszString" avant le return, j'ai un message d'erreur.
Merci
//---------------------------------------------------------------------------
//
// FUNCTION: wstring StringToWstring(string strString)
//
// PURPOSE: Conversion des string en wstring
//
// COMMENTS:
// Convertit un string (ANSI) en un wstring (UNICODE)
// Renvoie 0 si erreur
//
//
//
wstring StringToWstring(string strString)
{
int iResult;
PCHAR cString = new char [strString.size()+1];
PWCHAR lpszString = new WCHAR[strString.size()+1];
std::wstring wstrString;
strcpy(cString, strString.c_str());
iResult = MultiByteToWideChar(CP_ACP, 0, cString, strString.size()+1, (LPWSTR) lpszString, sizeof(strString));
if (iResult == 0)
{
Error_Msg(GetLastError(), "LPWSTR StringToLPWSTR(string strString)", __FILE__, __LINE__);
return 0;
}
delete [] cString;
wstrString.assign(lpszString);
return wstrString;
}