简体   繁体   English

无法使用 QProcess 执行 scp

[英]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.是 Windows 10,顺便说一句。

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. QProcess 从不以“scp”或“dir”开头(记住,它是 windows 10 host),但可以使用“cmd”或“ping”命令。 Also tried to execute "cmd" and write to stdin "scp".还尝试执行“cmd”并写入标准输入“scp”。 But command prompt reports that "scp" is an unkknown command.但是命令提示符报告“scp”是未知命令。

[EDIT] Just found that "dir" is not an external executable. [编辑] 刚刚发现“dir”不是外部可执行文件。 So, need to be execute within "cmd.exe".所以,需要在“cmd.exe”中执行。 And works.并且有效。 But "scp" still doesn't.但是“scp”仍然没有。

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.当您按名称生成进程时,它会在PATH环境变量的所有路径中进行搜索,如果不存在,它将无法工作。

You can fix it by adding scp path to PATH temporary or permanently before starting your application.您可以通过在启动应用程序之前临时或永久地将 scp 路径添加到PATH来修复它。

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:或者您可以通过 shell 生成 scp 并修改环境:

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"});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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