3
votes

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

Here is the method I am using to try to 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 it returns 0;

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

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

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

I have the code print the command with the arguments out to me and if I run the command manually it works. But in qt it doesn't.

Perhaps i'm doing something wrong. Is there any other way of doing this without QProcess?

I have also tried calling the static meathod

QProcess::startDetached

But that fails on me too.

1
I don't use QT, but could it be a permission problem? If you don't want to use QProcess, you could always use CreateProcess() and GetLastError(). - red_eight
I have messed around with those but the program doesn't even start. It just fails as if it cant find the exe. But its there. - Snhp9
Sysinternals' Process monitor utility might help you find out what's wrong - it will show the error code returned from the Windows API being used (I assume that's CreateProcess()). - Michael Burr

1 Answers

4
votes

I believe your program is 32-bit and running under 64-bit Windows. PnPUtil.exe is not in c:\windows\system32 when you are running a 32-bit program, that's why QProcess fails to start it. It's somewhere else, for example, mine is located at C:\Windows\WinSxS\amd64_microsoft-windows-pnputil_31bf3856ad364e35_6.3.9600.16384_none_ee22229c907e8ce2. You can run c:\windows\system32\PnPUtil.exe in Command Prompt because cmd.exe is a 64-bit program.

You can try the 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();
}