1 (edited by frares 2013-12-19 15:23:31)

Topic: replacing getenv function

Hi,I tried to debug QElectroTech under Windows, using QtCreator and VisualStudio compiler and linker.On function QETApp::configDir() , for instance, the function getenv produced compiler warnings, and latter, while debugging, the char * created with contents from this function refused to be cleanly deleted.

So I found a solution (the original code lines are commented out:

QString QETApp::userName() {
    QProcess * process = new QProcess();    // new line
#ifndef Q_OS_WIN32
    // return(QString(getenv("USER")));
    return ((process->processEnvironment()).value("USER", "UNKNOWN"));    // new line
#else
    // return(QString(getenv("USERNAME")));
    return((process->processEnvironment()).value("USERNAME", "UNKNOWN"));    // new line
#endif
    delete process;
}

The only other member function in the project is in this same file, and it might look like this:

QString QETApp::configDir() {
#ifdef QET_ALLOW_OVERRIDE_CD_OPTION
    if (config_dir != QString()) return(config_dir);
#endif
#ifdef Q_OS_WIN32
    // recupere l'emplacement du dossier Application Data
    // char *app_data_env = getenv("APPDATA");
    // QString app_data_str(app_data_env);
    QProcess * process = new QProcess();   // new line
    QString app_data_str = (process->processEnvironment()).value("APPDATA");   // new line
    // delete app_data_env;
    delete process;   // new line
    if (app_data_str.isEmpty()) {
        app_data_str = QDir::homePath() + "/Application Data";
    }
    return(app_data_str + "/qet/");
#else
    return(QDir::homePath() + "/.qet/");
#endif
}

That's it, hope it helps.

Best regards,
Francisco

P.S.: thanks for the good work!!!!!

Re: replacing getenv function

Hi Francisco and Welcome,

Thanks for your interest and your patch ;-)
We will test this.

Best regards,
Laurent

Edit: added  many thanks.

http://stackoverflow.com/questions/1741 … home-in-qt

Note: qgetenv() was introduced because getenv() from the standard C library was deprecated in VC2005 (and later versions). qgetenv() uses the new replacement function in VC, and calls the standard C library's implementation on all other platforms.

"Le jour où tu découvres le Libre, tu sais que tu ne pourras jamais plus revenir en arrière..."