Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

WMIC command is working on cmd but not in QProcess



  • i wrote this code to get out:

    QProcess p;
        p.setReadChannel(QProcess::StandardOutput);
        p.setReadChannelMode(QProcess::MergedChannels);
        p.start("wmic diskdrive where deviceid='\\\\.\\PHYSICALDRIVE0' get serialnumber,model,size");
        p.waitForReadyRead();
        qDebug()<<QString(p.readAllStandardOutput()).simplified().split(QRegExp("[\n\r ]"),QString::SkipEmptyParts);
        p.close();
    

    when i run my qt app, i get this error :

    ("Node", "-", "Hostname", "ERROR", ":", "Description", "=", "Request", "no", "valid")
    

    instead of:

    Model        SerialNumber  Size
    ST9500***AS  ********      500105249280
    


  • @Lemat said in WMIC command is working on cmd but not in QProcess:

    My hunch is that there is an issue in your command-line where you have deviceid='\\\\.\\PHYSICALDRIVE0'. For example, I see example from https://superuser.com/a/647711/479430 where using

    wmic DiskDrive where "DeviceID='\\\\.\\PHYSICALDRIVE<disk_index>'" Assoc /assocclass:Win32_DiskDriveToDiskPartition
    

    while yours will generate

    wmic diskdrive where deviceid='\\.\PHYSICALDRIVE0' get serialnumber,model,size
    

    They differ in the " marks and the number of \s. Start from the exact command you need to type into Windows shell (or from documentation) and adapt it to C++/argument for QProcess from there.

    @KroMignon's may work, but it will be passing deviceid='\\.\PHYSICALDRIVE0', is that what is intended?

    If your issue is over the backslash count, you might find you want to use C++ raw string literal here, e.g. R"(deviceid='\\\\.\\PHYSICALDRIVE0')" or whatever you decide the final command needs to look like backslash-wise.



  • @Lemat I think you should change the function call to use an argument list to avoid space issues:

        QProcess p;
        p.setReadChannel(QProcess::StandardOutput);
        p.setReadChannelMode(QProcess::MergedChannels);
        QStringList opt;
        opt << "diskdrive" << "where" 
            << "deviceid='\\\\.\\PHYSICALDRIVE0'"
            << "get" << "serialnumber,model,size"
        p.start("wmic", opt);
        p.waitForReadyRead();
        qDebug()<<QString(p.readAllStandardOutput()).simplified().split(QRegExp("[\n\r ]"),QString::SkipEmptyParts);
        p.close();
    


  • @Lemat said in WMIC command is working on cmd but not in QProcess:

    My hunch is that there is an issue in your command-line where you have deviceid='\\\\.\\PHYSICALDRIVE0'. For example, I see example from https://superuser.com/a/647711/479430 where using

    wmic DiskDrive where "DeviceID='\\\\.\\PHYSICALDRIVE<disk_index>'" Assoc /assocclass:Win32_DiskDriveToDiskPartition
    

    while yours will generate

    wmic diskdrive where deviceid='\\.\PHYSICALDRIVE0' get serialnumber,model,size
    

    They differ in the " marks and the number of \s. Start from the exact command you need to type into Windows shell (or from documentation) and adapt it to C++/argument for QProcess from there.

    @KroMignon's may work, but it will be passing deviceid='\\.\PHYSICALDRIVE0', is that what is intended?

    If your issue is over the backslash count, you might find you want to use C++ raw string literal here, e.g. R"(deviceid='\\\\.\\PHYSICALDRIVE0')" or whatever you decide the final command needs to look like backslash-wise.



  • @KroMignon thank for your reply. but it not work.



  • @Lemat Hi, I see the problem, you need to escape the backslashes:

        QProcess p;
        p.setReadChannel(QProcess::StandardOutput);
        p.setReadChannelMode(QProcess::MergedChannels);
        QStringList opt;
        opt << "diskdrive" << "where" 
            << "deviceid='\\\\\\\\.\\\\PHYSICALDRIVE0'"
            << "get" << "serialnumber,model,size"
        p.start("wmic", opt);
        p.waitForFinished();
        qDebug()<<QString(p.readAllStandardOutput()).simplified().split(QRegExp("[\n\r ]"),QString::SkipEmptyParts);
        p.close();
    


  • @JonB you saved my afternoon. i forgot R"()" utilization. i had used it over QFile and QByteArray for another app. thanks!! thanks!!!!!



  • @KroMignon good answer again. thanks for you time!!!. very thanks!!!.


Log in to reply