简体   繁体   English

QProcess.execute()一个DOS命令

[英]QProcess.execute() a DOS command

I'am trying to call a DOS command: subst with QProcess : 我'试图调用DOS命令: substQProcess

 QProcess process;
    int returnCode=process.execute(QString("subst " + DLetter+"  "+mountPath));
    qDebug()<<"returnCode"<<returnCode;
    if (returnCode==0){
        qDebug()<<"Mount "<<QString("subst " + DLetter+"  "+mountPath)
                <<"error"<<process.errorString()<<"id"<<process.pid();
        process.waitForFinished();
        ...
    }else
        qDebug()<<" mounting folder failed  "<<process.errorString();

My questions are : Is it enough to call execute() and waitForFinished() ? 我的问题是:调用execute()waitForFinished()是否足够? am I safe with calling execute() instead of start ? 我安全地调用execute()而不是start吗? because I had issues with start() : it didn't work in all cases(it worked for mounting and didn't for dismounting folder) . 因为我遇到了start()问题:它在所有情况下都不起作用(它适用于安装而不是用于卸载文件夹)。

Any help wil be appreciated. 任何帮助将不胜感激。

From the documentation of QProcess::execute() at http://doc.qt.digia.com/qt/qprocess.html#execute 来自http://doc.qt.digia.com/qt/qprocess.html#execute的QProcess :: execute()文档

you should do this: 你应该做这个:

int returnCode=process::execute(QString("subst " + DLetter+"  "+mountPath));
qDebug()<<"returnCode"<<returnCode;
if (returnCode==0){
    qDebug()<<"Mount "<<QString("subst " + DLetter+"  "+mountPath)
            <<"error"<<process.errorString()<<"id"<<process.pid();
}else
    qDebug()<<" mounting folder failed  ";

The key is QProcess::execute is a static member function that starts the program, waits for it to finish then returns the exit code of the process. 关键是QProcess :: execute是一个启动程序的静态成员函数,等待它完成然后返回进程的退出代码。

Note: I had to remove the non static calls to process.waitForFinished() and process.errorString() since neither can work from a static QProcess::execute. 注意:我必须删除对process.waitForFinished()和process.errorString()的非静态调用,因为它们都不能从静态QProcess :: execute中工作。

If you are trying to call a DOS function, try to call it using the 如果您尝试调用DOS函数,请尝试使用该函数调用它

#include <process.h>
...
system( "dir c:\\temp > c:\\temp\\output.txt" );

but this is now outdated. 但这已经过时了。 Try using 尝试使用

int nRet= (int)ShellExecute( 0,"open","calc.exe",0,0,SW_SHOWNORMAL);
if ( nRet <= 32 )
{
        DWORD dw= GetLastError(); 
        char szMsg[250];
        FormatMessage(
            FORMAT_MESSAGE_FROM_SYSTEM, 
            0, dw, 0,
            szMsg, sizeof(szMsg),
            NULL 
        );
        MessageBox( szMsg, "Error launching Calculator" );
}

Refer http://msdn.microsoft.com/en-us/library/windows/desktop/bb762153(v=vs.85).aspx to see how to use this function. 请参阅http://msdn.microsoft.com/en-us/library/windows/desktop/bb762153(v=vs.85).aspx以了解如何使用此功能。

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

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