QProcess always returns -2 when executing a valid command

I am writing a program in qt that will execute commands on windows.

Here is the method I am using to try and get the commands to work.

bool FirmwareUpdater::RunCommand( QString& command, QStringList& args, int expectedCode )
{
    QProcess *proc = new QProcess();
    proc->setWorkingDirectory ( "C:\\windows\\" );
    int exitCode = proc->execute(command, args );
    proc->waitForFinished();
    this->stream << command << " " << exitCode << "\n";
    return ( exitCode == expectedCode );
}

      

If I run

QString command = "ping";
QStringList args;

args << "localhost";
RunCommand( command, args );

      

It works fine and returns 0;

But if I try any other Windows utility it returns -2. Right now I'm trying to get pnpUtil to work too.

QString command = qgetenv( "WINDIR" ) + "\\System32\\PnPUtil.exe";
QStringList args;

args << "-a";
args << updateDriver;

      

I have the code to print the command with arguments for me and if I run the command manually it works. But this is not the case in qt.

I may be doing something wrong. Is there any other way to do this without QProcess?

I have also tried calling static meathod

QProcess::startDetached

      

But that doesn't work either.

+3


source to share


1 answer


I believe your program is 32 bit and works under 64 bit Windows. PnPUtil.exe

is not in c:\windows\system32

when you run a 32-bit program, so it QProcess

does not start it. It is somewhere else, for example, mine is in C:\Windows\WinSxS\amd64_microsoft-windows-pnputil_31bf3856ad364e35_6.3.9600.16384_none_ee22229c907e8ce2

. You can run c:\windows\system32\PnPUtil.exe

from the command line because it cmd.exe

is a 64-bit program.

You can try solutions here or here .

UPDATE 1



Sample code that runs PnPUtil and Ping under 32-bit or 64-bit Windows.

#include <QtCore>

void run( QString command, QStringList args )
{
    QProcess *proc = new QProcess();
    //proc->setWorkingDirectory ( "C:\\windows\\" );
    qDebug() << "\n===========================================";
    qDebug() << "Running " << command << args.join(' ');
    qDebug() << (QFile::exists(command) ? "File exists: " : "File may not exist:") << command;
    int exitCode = proc->execute(command, args );
    proc->waitForFinished();
    qDebug() << "\nResult";
    qDebug() << "======";
    qDebug() << "proc->execute()    =" << exitCode;
    qDebug() << "proc->exitCode()   =" << proc->exitCode();
    qDebug() << "proc->exitStatus() =" << proc->exitStatus();    
}

int main(int argc, char *argv[])
{
    QStringList pnpUtilArg("-?");
    QStringList pingArg("google.com");

    run( qgetenv( "WINDIR" ) + "\\sysnative\\pnputil.exe", pnpUtilArg);
    run( qgetenv( "WINDIR" ) + "\\system32\\pnputil.exe", pnpUtilArg);
    run( qgetenv( "WINDIR" ) + "\\system32\\ping.exe", pingArg);
    run( "ping.exe", pingArg);

    getchar();
}

      

+4


source







All Articles