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

QProcess cmd & tptf



  • I'm trying to run the windows shell frommy qt program and, from there, run a tftp command ...
    I'm documenting a bit 'but I do not understand what is wrong.

    Using this command i can open the file correctly

    QProcess process;
    process.start("cmd.exe /C start C:/Users/user/Desktop/configINPUT.txt");
    process.waitForFinished();
    

    But if I try to run a tftp command instead of opening the file, nothing happens.

      QProcess process;
       process.start("cmd.exe /C start tftp -i 192.168.2.1 get configINPUT.txt");
       process.waitForFinished();
    

    What am I doing wrong? Thanks in advance for the help..



  • @Bruschetta Tryied this way too, but nothing changed

     QProcess process;
        QString program = "cmd"; //used cmd.exe too,nothing .. 
        QStringList arguments = QStringList();
    
        arguments<< "tftp";
        arguments<< "-i";
        arguments<< "192.168.2.1";
        arguments<< "get";
        arguments<< "configINPUT.txt";
        process.setWorkingDirectory(temp_path);
        process.start(program,arguments);
        qDebug()<<arguments;
        process.waitForFinished();
    


  • @Bruschetta
    Why are you using cmd at all? Do you really need to use start? How does a simple:

    process.start("tftp -i 192.168.2.1 get configINPUT.txt");
    

    or

        QString program = "tftp"; 
        QStringList arguments = QStringList();
    
        arguments<< "-i";
        ...
    

    not work right? (BTW, your existing second attempt won't work at all because you running a cmd without the /c.)

    Also, do you understand what any kind of process.waitForFinished(); will do if you do use start?? (I mean the cmd /c start ..., not the process.start.)

    process.start("cmd.exe /C start C:/Users/user/Desktop/configINPUT.txt");

    I wouldn't hold your breath that any of these OS commands are guaranteed to work when you pass a file path using the Qt internal / separators. make sure you use \s when you pass to OS. Use http://doc.qt.io/qt-5/qdir.html#toNativeSeparators if necessary on your paths.



  • @JonB
    Reading your reply i tryed to use /C param with the cmd but with no luck

     QProcess process;
        QString program = "cmd.exe";
        QStringList arguments = QStringList();
    
        arguments<< "/C";
        arguments<< "tftp";
        arguments<< "-i";
        arguments<< "192.168.2.1";
        arguments<< "get";
        arguments<< "configINPUT.txt";
        bool ret= process.startDetached(program,arguments);
        qDebug() << arguments;
        qDebug() << ret;
    

    Then i tryed to avoid using cmd at all

     QProcess process;
        QString program = "tftp.exe";
        QStringList arguments = QStringList();
    
        arguments<< "/C";
        arguments<< "-i";
        arguments<< "192.168.2.1";
        arguments<< "get";
        arguments<< "configINPUT.txt";
        bool ret= process.startDetached(program,arguments);
        qDebug() << arguments;
        qDebug() << ret;
    

    But the result it's the same,not working



  • @Bruschetta
    But in your second example you are still passing "/C" argument, this time to tftp, which does not accept it.

    What happens with a completely plain:

    process.start("tftp -i 192.168.2.1 get configINPUT.txt");
    

    ?

    What does "not working" even mean? How are you judging it's not working? What does qDebug() << ret; print? Why are you using startDetached now? Is your file argument actually, literally, character-for-character as you show here, or is it something else? You have never stated that you have tested the exact tftp outside of Qt and it's working?



  • @JonB
    Not working because the plain command in cmd is working well with the device i'm connected with.
    Another 3rdparty program is able to exchange data with the device and tftp.

    qDebug() << ret; returns TRUE if i use

    QProcess process;
        QString program = "cmd.exe";
        QStringList arguments = QStringList();
    
        arguments<< "/C";
        arguments<< "tftp";
        arguments<< "-i";
        arguments<< "192.168.2.1";
        arguments<< "get";
        arguments<< "configINPUT.txt";
        bool ret= process.startDetached(program,arguments);
        qDebug() << arguments;
        qDebug() << ret;
    

    Otherwise it returns FALSE. But true or false is not giving me "any" hints , the file is not taken from the device even if ret is TRUE.

    The /C in the second example is a typo, wrong cut and paste.
    Anyway i appreciate the time you are spending to give me answers



  • @JonB
    What happens with a completely plain:

    process.start("tftp -i 192.168.2.1 get configINPUT.txt");

    ?

    Nothing, unfortunately :(



  • @Bruschetta
    Well, if that command works as-is outside of Qt, and assuming tftp is a .exe and not a .bat file, I really reckon it will work same via process.start()....

    You still have not explained how you know "nothing" is happening....



  • @JonB
    Nothing is happening means that the plain command i launch from cmd creates the file i get from the device at the 192.168.2.1 address. The commands that are cast by the QProcess are not working because there are no new files.



  • @Bruschetta
    I'm guessing that your command line will cause the file to be gotten to the current directory when you launch it from a command-prompt --- is that right? Have you considered, for example, whether the current directory is the same when you launch (presumably) by opening a command prompt interactively and typing it in versus what it is when you spawn a QProcess from within your Qt (maybe UI) application?

    I know it's more work, but at some point to make your code robust you will need/ought to capture stdout & stderr from QProcess (it's all in that documentation), and do something with it, like maybe show it to the user. If you did so now, you would perhaps discover why "nothing happens" --- often you'll find there's an error message, e.g. from your tptf, and that would explain why you're not seeing the expected behaviour.


  • Lifetime Qt Champion

    @Bruschetta Is tftp.exe in your PATH? If not then it is simply not found.
    Did you try to connect a slot to http://doc.qt.io/qt-5/qprocess.html#errorOccurred and print the error?



  • @jsulm
    Yes, tftp.exe is in my path.
    EDIT
    I get error enum val = QProcess::ProcessError(FailedToStart) Error using
    QString program = "C:/Windows/System32/TFTP.exe"; or
    QString program = "C:\Windows\System32\TFTP.exe"; or
    QString program = "TFTP.exe"; or
    QString program = "TFTP.";

        QString path = QDir::current().absolutePath();
        QString program = "C:\\Windows\\System32\\TFTP.exe";
        QStringList arguments = QStringList();
        arguments<< "-i";
        arguments<< "192.168.2.2";
        arguments<< "get";
        arguments<< "configINPUT.txt";
        QProcess process;
    
        connect(&process, &QProcess::errorOccurred, [=](QProcess::ProcessError error)
        {
            qDebug() << "error enum val = " << error << endl;
        });
        process.setProgram(program);
        process.setArguments(arguments);
        process.setWorkingDirectory(path);
        process.start();
    


  • @Bruschetta

    1. Are you sure "C:\Windows\System32\TFTP.exe" is the right path?
    2. Is you Qt app compiled as 64-bit or 32-bit?

Log in to reply