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. QT, C++ code not executing powershell script

QT, C++ code not executing powershell script

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 2 Posters 659 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.
  • C Offline
    C Offline
    Creat_ur_ownempire
    wrote on 20 May 2024, 12:06 last edited by Creat_ur_ownempire
    #1

    I am working on GUI C++ ,QT. where I press on the button the code inside the button should execute powershell script but it fails to execute the powershell. Your help would be appreciated.

    QDir directory;
        QString current = directory.currentPath();
        QString scriptpath = current +  "/logs.ps1";  
        qDebug() << " script path is" <<scriptpath;
           
    if (!QFile::exists(scriptpath)) {
        qDebug() << "Script file does not exist:" << scriptpath;
        return;
    }

    QStringList arguments;
    arguments << "-File" << scriptpath << "-AS";
    

    QProcess process;
        process.start("powershell.exe", arguments);

    if (!process.waitForStarted(-1)) {
        qDebug() << "Failed to start the process:" << process.errorString();
        return;
    }
    when  I run I get this error: 
    QProcess: Destroyed while process ("powershell.exe") is still running.

    I tried the same script path in powershell manually it is working
    powershell.exe -File <scriptpath> -Arguments . It is working but via code it is not working.

    J 1 Reply Last reply 20 May 2024, 12:55
    0
    • C Creat_ur_ownempire
      20 May 2024, 12:06

      I am working on GUI C++ ,QT. where I press on the button the code inside the button should execute powershell script but it fails to execute the powershell. Your help would be appreciated.

      QDir directory;
          QString current = directory.currentPath();
          QString scriptpath = current +  "/logs.ps1";  
          qDebug() << " script path is" <<scriptpath;
             
      if (!QFile::exists(scriptpath)) {
          qDebug() << "Script file does not exist:" << scriptpath;
          return;
      }

      QStringList arguments;
      arguments << "-File" << scriptpath << "-AS";
      

      QProcess process;
          process.start("powershell.exe", arguments);

      if (!process.waitForStarted(-1)) {
          qDebug() << "Failed to start the process:" << process.errorString();
          return;
      }
      when  I run I get this error: 
      QProcess: Destroyed while process ("powershell.exe") is still running.

      I tried the same script path in powershell manually it is working
      powershell.exe -File <scriptpath> -Arguments . It is working but via code it is not working.

      J Offline
      J Offline
      JonB
      wrote on 20 May 2024, 12:55 last edited by
      #2

      @Creat_ur_ownempire said in QT, C++ code not executing powershell script:

      QProcess process;

      Think about the scope/lifetime of this variable. It needs to persist throughout the running of the process. Yours does not. Hence the error message and bad behaviour.

      And please format your code with the forum's Code tags (</> button), it would be a lot easier to spot if you do.

      C 1 Reply Last reply 20 May 2024, 14:43
      1
      • J JonB
        20 May 2024, 12:55

        @Creat_ur_ownempire said in QT, C++ code not executing powershell script:

        QProcess process;

        Think about the scope/lifetime of this variable. It needs to persist throughout the running of the process. Yours does not. Hence the error message and bad behaviour.

        And please format your code with the forum's Code tags (</> button), it would be a lot easier to spot if you do.

        C Offline
        C Offline
        Creat_ur_ownempire
        wrote on 20 May 2024, 14:43 last edited by
        #3

        @JonB . Thank you for guiding me. The code runs in a function.

        </
        void func()
        {
        QProcess process;
        process.start("powershell.exe", arguments);
        }

        If I understood correctly . The scope of this variable is local to function changing this scope to global will it work.
        can you provide more info.

        J 1 Reply Last reply 20 May 2024, 14:53
        0
        • C Creat_ur_ownempire
          20 May 2024, 14:43

          @JonB . Thank you for guiding me. The code runs in a function.

          </
          void func()
          {
          QProcess process;
          process.start("powershell.exe", arguments);
          }

          If I understood correctly . The scope of this variable is local to function changing this scope to global will it work.
          can you provide more info.

          J Offline
          J Offline
          JonB
          wrote on 20 May 2024, 14:53 last edited by JonB
          #4

          @Creat_ur_ownempire
          That is the same as before. It's a C++ variable. So since it's local it gets destroyed at the end of the function. Which is no good if all you have done is started powershell, since it needs to keep running, unless you make your function wait for it to finish.

          Usually a QProcess is a class member variable or it's a newed pointer. If you want it to outlast a function.

          Or, if you want your function to run the sub-process and wait for it, you may find the static int QProcess::execute(const QString &program, const QStringList &arguments = {}) is all you need.

          1 Reply Last reply
          0
          • C Offline
            C Offline
            Creat_ur_ownempire
            wrote on 20 May 2024, 16:41 last edited by
            #5

            @JonB . if (!process.waitForFinished(-1)) I am using this one. I will update whether it works or not

            J 1 Reply Last reply 20 May 2024, 17:18
            0
            • C Creat_ur_ownempire
              20 May 2024, 16:41

              @JonB . if (!process.waitForFinished(-1)) I am using this one. I will update whether it works or not

              J Offline
              J Offline
              JonB
              wrote on 20 May 2024, 17:18 last edited by JonB
              #6

              @Creat_ur_ownempire
              Yes, that will also work since the process has finished by the time the function exits and the QProcess process variable gets destroyed.

              1 Reply Last reply
              0
              • C Offline
                C Offline
                Creat_ur_ownempire
                wrote on 21 May 2024, 05:29 last edited by
                #7

                @JonB - It's not working. QProcess: Destroyed while process ("powershell.exe") is still running.
                </
                QProcess process;
                process.start("powershell.exe", arguments);
                if (!process.waitForFinished(-1)) {
                qDebug() << "Failed to start the process:" << process.errorString();
                return;
                }

                J 1 Reply Last reply 21 May 2024, 05:34
                0
                • C Creat_ur_ownempire
                  21 May 2024, 05:29

                  @JonB - It's not working. QProcess: Destroyed while process ("powershell.exe") is still running.
                  </
                  QProcess process;
                  process.start("powershell.exe", arguments);
                  if (!process.waitForFinished(-1)) {
                  qDebug() << "Failed to start the process:" << process.errorString();
                  return;
                  }

                  J Offline
                  J Offline
                  JonB
                  wrote on 21 May 2024, 05:34 last edited by
                  #8

                  @Creat_ur_ownempire
                  I don't see why, assuming that is your actual code. Your message still says "Failed to start the process", but I presume it does now use waitForFinished() in place of waitForStarted()?

                  In any case, for this code try my earlier suggestion:

                  qDebug() << QProcess::execute("powershell.exe", arguments);
                  

                  Does that work? It should not issue "Destroyed" message.

                  1 Reply Last reply
                  0
                  • C Offline
                    C Offline
                    Creat_ur_ownempire
                    wrote on 21 May 2024, 05:41 last edited by
                    #9

                    @JonB . I will try.

                    1 Reply Last reply
                    0

                    1/9

                    20 May 2024, 12:06

                    • Login

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