QProcess 启动win第三方程序

来源:互联网 发布:安卓内核优化修改 编辑:程序博客网 时间:2024/06/04 00:51

我不知道哪里错了

    prog = new QProcess(this);
    QString fullpath = QString("%1/%2").arg(QCoreApplication::applicationDirPath()).arg(exeName);
    qDebug() << "launch full path =" << fullpath << ", argv =" << argv;
    prog->setObjectName(fullpath);
    connect(prog, SIGNAL(finished(int,QProcess::ExitStatus)),
            this, SLOT(finishedSlot(int,QProcess::ExitStatus)));
    prog->start(fullpath, argv);
如果这样写就启动不起来。



下面写的就可以

    m_pOtherProcess = new QProcess(this);    QString t_otherExePath = QString("%1/%2").arg(QCoreApplication::applicationDirPath()).arg(otherExeName);    QString t_otherExeDirPath = QString("%1").arg(QCoreApplication::applicationDirPath());    if(!QFile::exists(t_otherExePath))    {        qDebug()<<"robin:otherExe not exist";        return -1;    }    connect(m_pOtherProcess, SIGNAL(finished(int,QProcess::ExitStatus)),            this, SLOT(finishedSlot(int,QProcess::ExitStatus)));    m_pOtherProcess->setObjectName(t_otherExePath);    m_pOtherProcess->setWorkingDirectory(t_otherExeDirPath);    m_pOtherProcess->setProgram(t_otherExePath);    m_pOtherProcess->start();


我不想找原因了,我要忙别的了。其实上面的代码之前也是可以的,但是不知道为什么不行了



下面是终极办法,肯定可以。

bool runWinExe(const QString &path)
{
Q_ASSERT(QFile::exists(path));
bool run = false;
#if defined(Q_OS_WIN)
SHELLEXECUTEINFO sei = { sizeof(SHELLEXECUTEINFO) };
sei.lpVerb = TEXT("runas");
WCHAR wfile[256];
memset(wfile, 0, sizeof(wfile));
path.toWCharArray(wfile);
sei.lpFile = wfile;//add  application  which you want to run as administrator here
sei.nShow = SW_SHOWNORMAL;//without this,the windows will be hiden
run = ShellExecuteEx(&sei);
#else
run = QProcess::startDetached(path);
#endif
return run;
}



0 0