When running cmd.exe from window 7.x to QProcess.. Not work path environment...



  • Hello.
    Look at the simple source below. please..

    < source >
    QProcess process;

    process.setProcessChannelMode(QProcess::MergedChannels);
    process.start("cmd /c .agent.sh.cmd > x.txt");
    process.waitForFinished();

    QByteArray outText = process.readAllStandardOutput();

             << The value of outText is "hostname.exe not found or not internal /external / batch command".
    

    In the shell script below, c:\windows\system32\hostname.exe runs, but not hostname.exe.
    Why is this happening when the path is well set up?

    < Script File : .agent.sh.cmd >
    @echo off
    echo %PATH%
    echo "c:\windows\system32\hostname.exe"
    c:\windows\system32\hostname.exe
    echo "hostname.exe"
    hostname.exe

    < Result File : x.txt >
    C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn;C:\Program Files\Microsoft SQL Server\100\Tools\Binn;C:\Program Files\Microsoft SQL Server\100\DTS\Binn;C:\Program Files (x86)\Windows Kits\8.1\Windows Performance Toolkit;C:\Program Files\Microsoft SQL Server\110\Tools\Binn;C:\Program Files\Bandizip\7z;C:\Qt\Qt5.5.0\5.5\msvc2013_64\bin;C:\Program Files (x86)\Microsoft Visual Studio 12.0;C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\amd64
    "c:\windows\system32\hostname.exe"
    HP430-PC
    "hostname.exe"


  • Qt Champions 2016

    Hi
    This is not so Qt related.. but
    The other hostname.exe is that Another copy in another location or
    do you mean the one in
    c:\windows\system32\hostname.exe
    ?

    --

    IT cannot find the hostname.exe you talk about there.
    If you have another, then u need path for it.



  • @mrjj Hi, Thanks for the response.

    Hostname.exe is known to be a command included in windows os.
    In addition to hostname.exe, other commands such as ipconfig.exe are not executed.

    At the windows command prompt, the shell will run fine.
    **) See bolded text below

    C:\>cmd /c .agent.sh.cmd > x.txt

    C:\>type .agent.sh.cmd
    @echo off
    echo %PATH%
    echo "c:\windows\system32\hostname.exe"
    c:\windows\system32\hostname.exe
    echo "hostname.exe"
    hostname.exe

    C:\Projects\sungwoo\netMon\x64\Debug\agent\shell>type x.txt

    C:\Program Files (x86)\Intel\iCLS Client;C:\Program Files\Intel\iCLS Client;C:
    \Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\Window
    sPowerShell\v1.0;C:\Program Files (x86)\Intel\Intel(R) Management Engine Compon
    ents\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Pro
    gram Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program File
    s\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\Microso
    ft SQL Server\100\Tools\Binn;C:\Program Files\Microsoft SQL Server\100\Tools\Bi
    nn;C:\Program Files\Microsoft SQL Server\100\DTS\Binn;C:\Program Files (x86)\W
    indows Kits\8.1\Windows Performance Toolkit;C:\Program Files\Microsoft SQL Serv
    er\110\Tools\Binn;C:\Program Files\Bandizip\7z;C:\Qt\Qt5.5.0\5.5\msvc2013_64\bi
    n
    "c:\windows\system32\hostname.exe"
    HP430-PC
    "hostname.exe"
    HP430-PC


  • Moderators

    @Daryong said in When running cmd.exe from window 7.x to QProcess.. Not work path environment...:

    Hello.
    Look at the simple source below. please..

    < source >
    QProcess process;

    process.setProcessChannelMode(QProcess::MergedChannels);
    process.start("cmd /c .agent.sh.cmd > x.txt");
    process.waitForFinished();

    First thing I noticed is you are not using QProcess properly. You need to pass arguments separately from the executable. See http://doc.qt.io/qt-5/qprocess.html.

    I don't know if this will fix your problem, but try:

    QProcess process;
    process.setProcessChannelMode(QProcess::MergedChannels);
    process.start("cmd.exe", QStringList << "/c" << ".agent.sh.cmd" << ">" << "x.txt");
    process.waitForFinished();
    

    As for your actual issue, what happens when you hit window+r and run "cmd /k .agent.sh.cmd"? Does it work there but not in QProcess?

    You can also try setting the path like so:

    // ...
    QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
    env.insert("Path", "yourpath");
    process.setProcessEnvironment(env);
    


  • @ambershark

    Hello.
    I think I have found the cause of the problem. There seems to be a bug in qt.

    The path of QProcess differs from the path of the system, but this is not a big problem.
    By the way, it seems that some paths are disabled by "(double quotation marks) in the path of QProcess::processEnvironment(). Please check..

    I set the path as shown below and it works fine.

    QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
    env.insert("Path", env.value("Path").remove("\"") );
    process.setProcessEnvironment(env);

    < System Env Path >

    C:\Users\HP430>echo %PATH%

    C:\Program Files (x86)\Intel\iCLS Client;C:\Program Files\Intel\iCLS Client;C:
    \Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\Window
    sPowerShell\v1.0;C:\Program Files (x86)\Intel\Intel(R) Management Engine Compon
    ents\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Pro
    gram Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program File
    s\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\Microso
    ft SQL Server\100\Tools\Binn;C:\Program Files\Microsoft SQL Server\100\Tools\Bi
    nn;C:\Program Files\Microsoft SQL Server\100\DTS\Binn;C:\Program Files (x86)\W
    indows Kits\8.1\Windows Performance Toolkit;C:\Program Files\Microsoft SQL Serv
    er\110\Tools\Binn;C:\Program Files\Bandizip\7z;C:\Qt\Qt5.5.0\5.5\msvc2013_64\bi
    n

    < QProcess::processEnvironment() - Path >

    C:\Qt\Qt5.5.0\5.5\msvc2013_64\bin;"C:\Qt\Qt5.5.0\5.5\msvc2013_64\bin;C:\Qt\Qt5.5.0\5.5\msvc2013_64\bin;"C:\Qt\Qt5.5.0\5.5\msvc2013_64\bin;C:\Qt\Qt5.5.0\5.5\msvc2013_64\bin;"C:\Qt\Qt5.5.0\5.5\msvc2013_64\bin;C:\Qt\Qt5.5.0\5.5\msvc2013_64\bin;"C:\Qt\Qt5.5.0\5.5\msvc2013_64\bin;C:\Qt\Qt5.5.0\5.5\msvc2013_64\bin;"C:\Qt\Qt5.5.0\5.5\msvc2013_64\bin;C:\Qt\Qt5.5.0\5.5\msvc2013_64\bin;C:\Program Files (x86)\Intel\iCLS Client;C:\Program Files\Intel\iCLS Client;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn;C:\Program Files\Microsoft SQL Server\100\Tools\Binn;C:\Program Files\Microsoft SQL Server\100\DTS\Binn;C:\Program Files (x86)\Windows Kits\8.1\Windows Performance Toolkit;C:\Program Files\Microsoft SQL Server\110\Tools\Binn;C:\Program Files\Bandizip\7z;C:\Qt\Qt5.5.0\5.5\msvc2013_64\bin;C:\Program Files (x86)\Microsoft Visual Studio 12.0;C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\amd64


  • Moderators

    @Daryong I don't know about the quote (") thing but the other part isn't a bug. Processes don't usually inherit the system path.

    The only things that use the system path are explorer and cmd. And other windows components. So having to set up the path with a QProcessEnvironment before use makes sense to me. That's why it's there. :)

    Glad things are working now, don't forget to mark this as solved for others.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.