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. QProcess with powershell command still running
Forum Updated to NodeBB v4.3 + New Features

QProcess with powershell command still running

Scheduled Pinned Locked Moved Unsolved General and Desktop
qprocesspowershell
11 Posts 3 Posters 2.6k Views 1 Watching
  • 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.
  • T TomNow99
    5 May 2020, 04:46

    I have a big problem with QProcess ( I use Qt c++ ). I need to execute a command with admin permissions ( windows 7 ) - netsh - I have to add exception to my firewall.

    proc->start("powershell",QStringList()<<"-Command"<<"Start-Process"<<"powershell.exe"<<"-Verb"<<"runas"<<"-ArgumentList"<<R"('here is netsh command')");
    proc->waitForStarted();
    qInfo()<<"before";
    proc->waitForFinished(-1);
    qInfo()<<"yeah!";
    When I execute my program I see only "before". What can I do? I check my netsh command in powershell - it's good. Command is still running ( I waited 3 minutes ).

    J Offline
    J Offline
    jsulm
    Lifetime Qt Champion
    wrote on 5 May 2020, 04:50 last edited by
    #2

    @TomNow99 said in QProcess with powershell command still running:

    I check my netsh command in powershell - it's good

    Do you have to authenticate yourself as admin in this case?
    My guess is that the command is simply waiting for admin credentials.

    https://forum.qt.io/topic/113070/qt-code-of-conduct

    T 1 Reply Last reply 5 May 2020, 04:57
    0
    • J jsulm
      5 May 2020, 04:50

      @TomNow99 said in QProcess with powershell command still running:

      I check my netsh command in powershell - it's good

      Do you have to authenticate yourself as admin in this case?
      My guess is that the command is simply waiting for admin credentials.

      T Offline
      T Offline
      TomNow99
      wrote on 5 May 2020, 04:57 last edited by
      #3

      @jsulm Thank you for answer.

      When I run this code I get a system window "user account control". When I Click accept I wait.... , when I click reject I wait.... When I close that window I wait...

      J 1 Reply Last reply 5 May 2020, 04:59
      0
      • T TomNow99
        5 May 2020, 04:57

        @jsulm Thank you for answer.

        When I run this code I get a system window "user account control". When I Click accept I wait.... , when I click reject I wait.... When I close that window I wait...

        J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 5 May 2020, 04:59 last edited by
        #4

        @TomNow99 I was actually talking about executing this command manually in powershell

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        T 1 Reply Last reply 5 May 2020, 05:14
        0
        • J jsulm
          5 May 2020, 04:59

          @TomNow99 I was actually talking about executing this command manually in powershell

          T Offline
          T Offline
          TomNow99
          wrote on 5 May 2020, 05:14 last edited by
          #5

          @jsulm When I go to powershell ( no QT ) I can run this command ( netsh ). I work in my private pc, so I have only one account (main account ). When I execute this command in powershell I don't have to write password or something like that. I run this command and it's done. When I go to cmd and run this command I get message "execute as admin".

          J 1 Reply Last reply 5 May 2020, 05:16
          0
          • T TomNow99
            5 May 2020, 05:14

            @jsulm When I go to powershell ( no QT ) I can run this command ( netsh ). I work in my private pc, so I have only one account (main account ). When I execute this command in powershell I don't have to write password or something like that. I run this command and it's done. When I go to cmd and run this command I get message "execute as admin".

            J Offline
            J Offline
            jsulm
            Lifetime Qt Champion
            wrote on 5 May 2020, 05:16 last edited by
            #6

            @TomNow99 You should connect slots to https://doc.qt.io/qt-5/qprocess.html#readyReadStandardOutput and https://doc.qt.io/qt-5/qprocess.html#readyReadStandardError and print nout what you get. But to do so you have to remove your blocking

            proc->waitForFinished(-1);
            

            call!
            Also https://doc.qt.io/qt-5/qprocess.html#stateChanged and https://doc.qt.io/qt-5/qprocess.html#errorOccurred signals can provide more information.

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            T 1 Reply Last reply 5 May 2020, 05:32
            0
            • J jsulm
              5 May 2020, 05:16

              @TomNow99 You should connect slots to https://doc.qt.io/qt-5/qprocess.html#readyReadStandardOutput and https://doc.qt.io/qt-5/qprocess.html#readyReadStandardError and print nout what you get. But to do so you have to remove your blocking

              proc->waitForFinished(-1);
              

              call!
              Also https://doc.qt.io/qt-5/qprocess.html#stateChanged and https://doc.qt.io/qt-5/qprocess.html#errorOccurred signals can provide more information.

              T Offline
              T Offline
              TomNow99
              wrote on 5 May 2020, 05:32 last edited by
              #7

              @jsulm

              I delete line: "proc->waitForFinished(-1);"

              and I add slots:

              void MainWindow::finishedslot(int p)
              {
              qInfo()<<p;
              }
              void MainWindow::stateChangedslot(QProcess::ProcessState state)
              {
              qInfo()<<state;
              }

              void MainWindow::readyReadStandardErrorslot()
              {
              QString stdOutput = proc->readAllStandardError();
              qInfo()<<stdOutput;
              }

              void MainWindow::readyReadStandardOutputslot()
              {
              QString stdOutput = proc->readAllStandardOutput();
              qInfo()<<stdOutput;
              }

              void MainWindow::errorOccurredslot(QProcess::ProcessError error)
              {
              qInfo()<<error;
              }

              Now I see informations:
              QProcess::Starting
              QProcess::Running
              before
              yeah!

              And nothing more.

              J 1 Reply Last reply 5 May 2020, 05:34
              0
              • T TomNow99
                5 May 2020, 05:32

                @jsulm

                I delete line: "proc->waitForFinished(-1);"

                and I add slots:

                void MainWindow::finishedslot(int p)
                {
                qInfo()<<p;
                }
                void MainWindow::stateChangedslot(QProcess::ProcessState state)
                {
                qInfo()<<state;
                }

                void MainWindow::readyReadStandardErrorslot()
                {
                QString stdOutput = proc->readAllStandardError();
                qInfo()<<stdOutput;
                }

                void MainWindow::readyReadStandardOutputslot()
                {
                QString stdOutput = proc->readAllStandardOutput();
                qInfo()<<stdOutput;
                }

                void MainWindow::errorOccurredslot(QProcess::ProcessError error)
                {
                qInfo()<<error;
                }

                Now I see informations:
                QProcess::Starting
                QProcess::Running
                before
                yeah!

                And nothing more.

                J Offline
                J Offline
                jsulm
                Lifetime Qt Champion
                wrote on 5 May 2020, 05:34 last edited by
                #8

                @TomNow99 said in QProcess with powershell command still running:

                and I add slots

                Just to be sure: you connected all of them to the related signals?

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                T 2 Replies Last reply 5 May 2020, 05:42
                0
                • J jsulm
                  5 May 2020, 05:34

                  @TomNow99 said in QProcess with powershell command still running:

                  and I add slots

                  Just to be sure: you connected all of them to the related signals?

                  T Offline
                  T Offline
                  TomNow99
                  wrote on 5 May 2020, 05:42 last edited by
                  #9

                  @jsulm Yes. When I bad connect signal and slot I see information below in QT. Here are my connects:

                  connect(proc, SIGNAL(finished(int)), this, SLOT(finishedslot(int)));
                  connect(proc, SIGNAL(stateChanged(QProcess::ProcessState)), this, SLOT(stateChangedslot(QProcess::ProcessState)));
                  connect(proc, SIGNAL(readyReadStandardError()), this, SLOT(readyReadStandardErrorslot()));
                  connect(proc, SIGNAL(readyReadStandardOutput()), this, SLOT(readyReadStandardOutputslot()));
                  connect(proc, SIGNAL(errorOccurred(QProcess::ProcessError)), this, SLOT(errorOccurredslot(QProcess::ProcessError)));

                  I connect them before proc->start().


                  When I wrote script .bat, which have only 3 commands:

                  @echo off
                  powershell -Command "& {Start-Process powershell.exe -Verb runas -ArgumentList 'netsh command here'}"
                  exit 0

                  And I run it in QT (proc->start(script.bat)) I have the same problem. But when I delete the line with powershell command:

                  @echo off
                  exit 0

                  I get message in Qt that script is done.

                  1 Reply Last reply
                  0
                  • J jsulm
                    5 May 2020, 05:34

                    @TomNow99 said in QProcess with powershell command still running:

                    and I add slots

                    Just to be sure: you connected all of them to the related signals?

                    T Offline
                    T Offline
                    TomNow99
                    wrote on 5 May 2020, 05:57 last edited by
                    #10

                    @jsulm When I change powershell command in QT ( or in .bat script ) to:

                    ( in QT ):
                    proc->start("powershell",QStringList()<<"-Command"<<"Start-Process"<<"powershell.exe"<<"-ArgumentList"<<R"('echo 4')");

                    I still don't get information that proc is finished.

                    Can you run this code in your computer?

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      SimonSchroeder
                      wrote on 6 May 2020, 06:01 last edited by
                      #11

                      The problem might be that you have two nested calls to PowerShell without exiting them. A quick search shows people reporting the inner PowerShell call (with -Verb runas) keeps the console window open. So, instead of the netsh command you might want to provide a script that has the netsh command and an exit. Or maybe you can concatenate the two directly in the -ArgumentList (I don't have much experience with PowerShell, but in Linux I could separate commands with a semicolon).

                      1 Reply Last reply
                      1

                      11/11

                      6 May 2020, 06:01

                      • Login

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