简体   繁体   中英

Cannot execute scp with QProcess

I'm trying to get-modidy-send a file from/to a remote host. It's Windows 10, By the way.

I've reduced code to be easier:

#include <QCoreApplication>
#include <QProcess>

bool getFile(QStringList &logTextList)
{
    QProcess scp;

    scp.start( "scp", QStringList() );
    if( scp.waitForStarted(1000) )
    {
        if( scp.waitForFinished(10000) == true )
        {
            if( scp.exitCode() == 0 )
            {
                logTextList.append(scp.readAllStandardError());
                return true;
            }
        }
        else
            logTextList.append("Not finished");
    }
    else
        logTextList.append( "Not Started" );
    logTextList.append( scp.readAllStandardError() );
    logTextList.append( scp.readAllStandardOutput() );
    logTextList.append( QString("Exitcode = %1\n").arg(scp.exitCode()) );
    return false;
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    QStringList logTextList;

    getFile(logTextList);

    return a.exec();
}

QProcess never starts with "scp" or even "dir" (remember, it's windows 10 host) But with "cmd" or "ping" command works. Also tried to execute "cmd" and write to stdin "scp". But command prompt reports that "scp" is an unkknown command.

[EDIT] Just found that "dir" is not an external executable. So, need to be execute within "cmd.exe". And works. But "scp" still doesn't.

Any clue?

Thanks in advance.

When you spawn process by name it is searched in all paths in PATH environment variable, if it's not there it wont work.

You can fix it by adding scp path to PATH temporary or permanently before starting your application.

Or you can spawn process using full path:

QString command = "scp";
#ifdef Q_OS_WIN
command = "C:\\Program Files\\Git\\usr\\bin\\scp.exe";
#endif
scp.start(command, {});

Or you can spawn scp through shell with modified environment:

QProcess scp;
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
QString PATH = env.value("PATH") + ";C:\\msys64\\usr\\bin;C:\\Program Files\\Git\\usr\\bin";
env.insert("PATH", PATH);
scp.setProcessEnvironment(env);
scp.start("cmd",{"/c","scp"});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM