Seite 1 von 1

[erledigt] Wie qtgetenv("APPDATA") mit non-latin1 characters

Verfasst: 10. Oktober 2013 14:54
von Selur
Habe hier (Win7pro 64bit , Englisch) einen User mit Namen 'Администратор'.

Würde in Qt jetzt gerne denn AppData-Path auslesen, habe da aber das Problem, dass der Name anscheinend nicht richtig decodiert wird. :(

Code: Alles auswählen

#include <QMessageBox>
#include <QTextCodec>
#include <QStringList>
#include <QString>
void testAppdata()
{
  QByteArray value = qgetenv("APPDATA");
  QStringList coding = QStringList() << "Apple Roman" << "Big5" << "Big5-HKSCS" << "CP949"
      << "EUC-JP" << "EUC-KR" << "GB18030-0" << "IBM 850" << "IBM 866" << "IBM 874" << "ISO 2022-JP"
      << "ISO 8859-1" << "ISO 8859-2" << "ISO 8859-3" << "ISO 8859-4" << "ISO 8859-5"
      << "ISO 8859-6" << "ISO 8859-7" << "ISO 8859-8" << "ISO 8859-9" << "ISO 8859-10"
      << "ISO 8859-13" << "ISO 8859-14" << "ISO 8859-15" << "ISO 8859-16" << "Iscii-Bng" << "Dev"
      << "Gjr" << "Knd" << "Mlm" << "Ori" << "Pnj" << "Tlg" << "Tml" << "JIS X 0201" << "JIS X 0208"
      << "KOI8-R" << "KOI8-U" << "MuleLao-1" << "ROMAN8" << "Shift-JIS" << "TIS-620" << "TSCII"
      << "UTF-8" << "UTF-16" << "UTF-16BE" << "UTF-16LE" << "UTF-32" << "UTF-32BE" << "UTF-32LE"
      << "Windows-1250" << "Windows-1251" << "Windows-1252" << "Windows-1253" << "Windows-1254"
      << "Windows-1255" << "Windows-1256" << "Windows-1257" << "Windows-1258" << "WINSAMI2";

  QString appData;
  QTextCodec *codec;
  foreach(QString code, coding)
  {
    QByteArray cod = code.toUtf8();
    codec = QTextCodec::codecForName(cod);
    if (codec == NULL) {
      QMessageBox::information(0, code, "failed");
      continue;
    }
    appData = codec->toUnicode(value);
    QMessageBox::information(0, code, appData);
  }
  appData = QString::fromAscii(value.constData());
  QMessageBox::information(0, "fromAsciii", appData);
  appData = QString::fromLatin1(value.constData());
  QMessageBox::information(0, "fromLatin1", appData);
  appData = QString::fromLocal8Bit(value.constData());
  QMessageBox::information(0, "fromLocal8Bit", appData);
  appData = QString::fromUtf16((ushort *) value.constData());
  QMessageBox::information(0, "fromUtf16", appData);
  appData = QString::fromUcs4((uint *) value.constData());
  QMessageBox::information(0, "fromUcs4", appData);
  appData = QString::fromUtf8(value.constData());
  QMessageBox::information(0, "fromUtf8", appData);
}
Leider krieg ich aber immer nur "C:\Users\?????????????\AppData\Roaming" als Ausgabe.

-> Hat einer eine Idee wie ich das den Pfad richtig auslesen kann?

Wenn ich mir QProcess::systemEnvironment() ausgeben lasse habe ich da auch Einträge wie:

Code: Alles auswählen

APPDATA=C:\Users\?????????????\AppData\Roaming
HOMEPATH=\Users\?????????????
LOCALAPPDATA=C:\Users\?????????????\AppData\Local
USERNAME=?????????????
USERPROFILE=C:\Users\?????????????
--------------------
Wenn man es über die native Windows-API mit _wgetenv macht, geht es ohne Probleme!

Code: Alles auswählen

  wchar_t *env = _wgetenv(L"APPDATA");
QString appdata = QString::fromWCharArray(env ? env : L"");
Cu Selur