开发者

Retrieve environment variables of a QProcess

开发者 https://www.devze.com 2023-02-11 12:18 出处:网络
I want to run an environment script within a QProcess and then read the environment (as a QStringList)开发者_JS百科 to start other scripts with this environment.

I want to run an environment script within a QProcess and then read the environment (as a QStringList)开发者_JS百科 to start other scripts with this environment.

If I start the env script and read the environment, I always get an empty QStringList. Is there a way to read out the environment of a QProcess?

I also tried to first start the environment script and the start the actual script on the same QProcess object but that did not help either.


If you are able to rewrite the script which sets the environment in C++ you can create the environment yourself and set it using
void QProcess::setProcessEnvironment ( const QProcessEnvironment & environment ) method as in the example given in the method's documenation:

 QProcess process;
 QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
 env.insert("TMPDIR", "C:\\MyApp\\temp"); // Add an environment variable
 env.insert("PATH", env.value("Path") + ";C:\\Bin");
 process.setProcessEnvironment(env);
 process.start("myapp");

UPDATE

If you can't use the above method you could try using cmd.exe like this;

#include <QtCore/QCoreApplication>
#include <QtCore/QProcess>

int main(int argc, char *argv[])
{
    QCoreApplication app(argc, argv);

    QProcess* proc = new QProcess();
    proc->start("cmd.exe /c \"call env.bat && script.bat\"");

    return app.exec();
}

Having env.bat with this contents

set abc=test

and script.bat with this contents

echo %abc% > a.txt

running the above creates a.txt file with this contents

test 


If you haven't used QProcess's setEnvironment method, then the empty QStringList is the expected output. For this case, QProcess uses the program's environment. To get this,

QStringList env(QProcess::systemEnvironment());

should work.


You can read all environment variables set in the QProcess in a nicer format the following way (to the standard debug output window). It will print each variable in new line.

QProcessEnvironment env = QProcessEnvironment::systemEnvironment();

qDebug() << "All variables";

QString env_variable;
QStringList paths_list = env.toStringList();

foreach( env_variable, paths_list )
    qDebug() << env_variable;


another way to accomplish it without parsing external files; in my case i need to execute vcvarsall.bat from different msvc versions, and need to capture the complete environment after calling them (individually):

  1. i write a .cmd script to disk which calls anything that modifies the environment;
  2. ... last line of the cmd script is simply "set" i.e.
    call ...\vc\vcvarsall.bat
    call may\be\another.cmd
    echo {5c131c2a-405b-478a-8279-9dff52c31537}
    set
    
  3. after running the script with QProcess i use readAllStandardOutput() to gather all the output.
  4. whatever the output contains, after splitting it at the guid that was used as anchor, the rest is simply the full environment that the calls before had created
  5. ... now i can parse it line by line as key=value parts and insert them into a new QProcessEnvironment (as already mentioned), and can apply it to new processes as is or with additional modifications.
0

精彩评论

暂无评论...
验证码 换一张
取 消