Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. executing cmd.exe with some console application

executing cmd.exe with some console application

Scheduled Pinned Locked Moved Unsolved General and Desktop
20 Posts 4 Posters 3.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V VRonin
    20 Apr 2022, 16:37

    Did you try using startDetached instead of start?

    R Offline
    R Offline
    Robert M.
    wrote on 20 Apr 2022, 16:59 last edited by
    #11

    You are great! Thanks a lot!

    process.setProgram("C:\\Windows\\System32\\cmd.exe");
    process.setArguments({"/k", "pause"});
    qint64 pid;
    process.startDetached(&pid);
    

    displays console with pause text.

    1 Reply Last reply
    0
    • V VRonin
      20 Apr 2022, 16:37

      Did you try using startDetached instead of start?

      J Offline
      J Offline
      JonB
      wrote on 20 Apr 2022, 19:05 last edited by
      #12

      @VRonin said in executing cmd.exe with some console application:

      Did you try using startDetached instead of start?

      Interesting. But why does that allow the new console where start does not? :)

      1 Reply Last reply
      0
      • V Offline
        V Offline
        VRonin
        wrote on 20 Apr 2022, 19:21 last edited by VRonin
        #13

        start is designed explicitly to avoid popping new consoles up, see the comment block in https://code.woboq.org/qt5/qtbase/src/corelib/io/qprocess_win.cpp.html#550

        we [...] do not want console tools launched from a GUI app to create new console windows (behavior consistent with UNIX).

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        J 2 Replies Last reply 20 Apr 2022, 19:33
        2
        • V VRonin
          20 Apr 2022, 19:21

          start is designed explicitly to avoid popping new consoles up, see the comment block in https://code.woboq.org/qt5/qtbase/src/corelib/io/qprocess_win.cpp.html#550

          we [...] do not want console tools launched from a GUI app to create new console windows (behavior consistent with UNIX).

          J Offline
          J Offline
          JonB
          wrote on 20 Apr 2022, 19:33 last edited by JonB
          #14

          @VRonin said in executing cmd.exe with some console application:

          see the comment block in https://code.woboq.org/qt5/qtbase/src/corelib/io/qprocess_win.cpp.html#550

          // However, we also do not want console tools launched from a GUI app to
          // create new console windows (behavior consistent with UNIX).
          DWORD dwCreationFlags = (GetConsoleWindow() ? 0 : CREATE_NO_WINDOW);
          

          Well no wonder then :) But we don't get told this is a difference from startDetached() :) I will try to remember this for the future.

          P.S.
          Isn't just lovely to see UNIX written? :)

          1 Reply Last reply
          0
          • V VRonin
            20 Apr 2022, 19:21

            start is designed explicitly to avoid popping new consoles up, see the comment block in https://code.woboq.org/qt5/qtbase/src/corelib/io/qprocess_win.cpp.html#550

            we [...] do not want console tools launched from a GUI app to create new console windows (behavior consistent with UNIX).

            J Offline
            J Offline
            JonB
            wrote on 20 Apr 2022, 19:43 last edited by JonB
            #15

            @VRonin said in executing cmd.exe with some console application:

            start is designed explicitly to avoid popping new consoles u

            Not tested, but can't you change this in the same way in your setCreateProcessArgumentsModifier() anyway, just add the first line to whatever you want:

            args->flags &= ~CREATE_NO_WINDOW;  // switch that *off*
            args->flags |= CREATE_NEW_CONSOLE;
            ...
            

            ?

            If this works for start() like the OP says it apparently is working for startDetached() the advantage is you can still wait for sub-process completion, if desired.

            1 Reply Last reply
            2
            • V Offline
              V Offline
              VRonin
              wrote on 20 Apr 2022, 19:51 last edited by
              #16

              Indeed you can!

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              1 Reply Last reply
              1
              • R Robert M.
                20 Apr 2022, 16:10

                In Linux I call process->start(...) with xterm.

                R Offline
                R Offline
                Robert M.
                wrote on 20 Apr 2022, 19:51 last edited by VRonin
                #17

                I have one problem in Linux. I invoke xterm with dirtyphp.sh script:

                    QProcess process;
                    QString exec = QString::fromStdString(terminal);
                    QStringList params;
                    params << "-hold" << "-e"; 
                    bool p0 = true;
                    for (std::string s : explode(std::string(" "), dirtyphp_command)) {
                        if (p0) {
                            std::string s0 = str_replace("'", "", s) + ".sh";
                            params << s0.c_str();
                            p0 = false;
                        } else {
                            params << str_replace("'", "", s).c_str();
                        }
                    }
                    process.start(exec, params);
                    process.waitForFinished();
                

                In dirtyphp.sh script I am waiting for CTRL+C at the end:

                d=`dirname $0`
                if [ "$d" = "" ]
                then
                	d="."
                fi
                $d/dirtyphp $@
                echo 'Hit CTRL+C';
                while true 
                do
                	sleep 1
                done 
                

                The problem is that xterm window is closed automatically without waiting for CTRL+C and the user cannot browse output produced by program dirtyphp in xterm window. I do not understand why - the loop and the end of dirtyphp.sh script should be infinite.

                J 1 Reply Last reply 20 Apr 2022, 20:08
                0
                • R Robert M.
                  20 Apr 2022, 19:51

                  I have one problem in Linux. I invoke xterm with dirtyphp.sh script:

                      QProcess process;
                      QString exec = QString::fromStdString(terminal);
                      QStringList params;
                      params << "-hold" << "-e"; 
                      bool p0 = true;
                      for (std::string s : explode(std::string(" "), dirtyphp_command)) {
                          if (p0) {
                              std::string s0 = str_replace("'", "", s) + ".sh";
                              params << s0.c_str();
                              p0 = false;
                          } else {
                              params << str_replace("'", "", s).c_str();
                          }
                      }
                      process.start(exec, params);
                      process.waitForFinished();
                  

                  In dirtyphp.sh script I am waiting for CTRL+C at the end:

                  d=`dirname $0`
                  if [ "$d" = "" ]
                  then
                  	d="."
                  fi
                  $d/dirtyphp $@
                  echo 'Hit CTRL+C';
                  while true 
                  do
                  	sleep 1
                  done 
                  

                  The problem is that xterm window is closed automatically without waiting for CTRL+C and the user cannot browse output produced by program dirtyphp in xterm window. I do not understand why - the loop and the end of dirtyphp.sh script should be infinite.

                  J Offline
                  J Offline
                  JonB
                  wrote on 20 Apr 2022, 20:08 last edited by JonB
                  #18

                  @Robert-M
                  If you think it's even executing that script and getting as far as the while true loop (goodness knows), put some debugging into the loop, like an echo or a read.

                  Or maybe put a set -x at the start of the script so you can see what is going on?

                  1 Reply Last reply
                  0
                  • R Offline
                    R Offline
                    Robert M.
                    wrote on 21 Apr 2022, 05:30 last edited by
                    #19

                    I changed the loop to the following:
                    while true
                    do
                    echo -n _
                    done
                    I see no underscores in xterm window.
                    Qt reports:
                    QProcess: Destroyed while process ("xterm") is still running.
                    I modified waitForFinish call:
                    process.waitForFinish(-1);
                    and now I can see underscores.
                    Problem solved.

                    J 1 Reply Last reply 21 Apr 2022, 08:18
                    0
                    • R Robert M.
                      21 Apr 2022, 05:30

                      I changed the loop to the following:
                      while true
                      do
                      echo -n _
                      done
                      I see no underscores in xterm window.
                      Qt reports:
                      QProcess: Destroyed while process ("xterm") is still running.
                      I modified waitForFinish call:
                      process.waitForFinish(-1);
                      and now I can see underscores.
                      Problem solved.

                      J Offline
                      J Offline
                      JonB
                      wrote on 21 Apr 2022, 08:18 last edited by JonB
                      #20

                      @Robert-M
                      bool QProcess::waitForFinished(int msecs = 30000) means that waitForFinished() only waits up to 30 seconds, and then your QProcess process; goes out of scope and causes the error. If your sub-process takes longer that is why you wanted waitForFinished(-1).

                      1 Reply Last reply
                      0

                      20/20

                      21 Apr 2022, 08:18

                      • Login

                      • Login or register to search.
                      20 out of 20
                      • First post
                        20/20
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved