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. write on QProcess powershell
Forum Updated to NodeBB v4.3 + New Features

write on QProcess powershell

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 3 Posters 1.8k 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.
  • H Online
    H Online
    hskoglund
    wrote on 9 Jul 2020, 19:32 last edited by
    #2

    Just an idea, but since cmd works fine: because you can run PowerShell from cmd, maybe you could do your PowerShell stuff this way (example):
    launch a cmd, then inside that try:
    powershell dir C:

    (after that directory listing PowerShell drops back to cmd, so you try another one)

    1 Reply Last reply
    1
    • M Offline
      M Offline
      malik boulakrachef
      wrote on 9 Jul 2020, 19:51 last edited by
      #3

      thank's a lot for your response
      i try your idea but it's not an elegant way to do that
      i try to understand what's wrong with this
      here is the code i use:

      process = new QProcess(this);
      process->start("powershell.exe");
      process->write("notepad\n");
      process->waitForBytesWritten();

      J 1 Reply Last reply 10 Jul 2020, 18:17
      0
      • H Online
        H Online
        hskoglund
        wrote on 9 Jul 2020, 20:26 last edited by
        #4

        If you only need to issue some "fire and forget" type of commands, then QProcess::startDetached() might be easier, say like this:

        QProcess::startDetached("powershell.exe",QStringList("notepad"));
        
        1 Reply Last reply
        0
        • M Offline
          M Offline
          malik boulakrachef
          wrote on 10 Jul 2020, 17:58 last edited by
          #5

          the problem with this solution you are proposing is : QProcess :: startDetached ("powershell.exe", QStringList ("notepad"));
          creates a new process each time, and after some execution it consumes a lot of memory space (RAM).
          because of that, i want to launch one process powershell and send it a sequence of command

          1 Reply Last reply
          0
          • M malik boulakrachef
            9 Jul 2020, 19:51

            thank's a lot for your response
            i try your idea but it's not an elegant way to do that
            i try to understand what's wrong with this
            here is the code i use:

            process = new QProcess(this);
            process->start("powershell.exe");
            process->write("notepad\n");
            process->waitForBytesWritten();

            J Offline
            J Offline
            JonB
            wrote on 10 Jul 2020, 18:17 last edited by
            #6

            @malik-boulakrachef
            You are aware you could achieve this via powershell.exe -Command notepad, aren't you?

            M 1 Reply Last reply 10 Jul 2020, 19:46
            1
            • J JonB
              10 Jul 2020, 18:17

              @malik-boulakrachef
              You are aware you could achieve this via powershell.exe -Command notepad, aren't you?

              M Offline
              M Offline
              malik boulakrachef
              wrote on 10 Jul 2020, 19:46 last edited by
              #7

              @JonB thank's a lot for your response
              may be i haven't i ask my questionin the correct way so, my question is:
              why this code doesn't work:
              process = new QProcess (this);
              process-> start ("powershell.exe");
              processus-> write("bloc-notes \ n");
              process-> waitForBytesWritten ();
              but if i replace "powershell.exe" by "cmd.exe" , it work perfectly

              J 1 Reply Last reply 10 Jul 2020, 22:16
              0
              • M malik boulakrachef
                10 Jul 2020, 19:46

                @JonB thank's a lot for your response
                may be i haven't i ask my questionin the correct way so, my question is:
                why this code doesn't work:
                process = new QProcess (this);
                process-> start ("powershell.exe");
                processus-> write("bloc-notes \ n");
                process-> waitForBytesWritten ();
                but if i replace "powershell.exe" by "cmd.exe" , it work perfectly

                J Offline
                J Offline
                JonB
                wrote on 10 Jul 2020, 22:16 last edited by JonB 7 Nov 2020, 09:17
                #8

                @malik-boulakrachef
                So that indicates that powershell.exe acts differently from cmd.exe. Your calling code is the same in both cases, so it's not a Qt issue.

                From a Command Prompt, compare the behaviour of these two:

                echo echo hello | cmd
                

                Does echo hello.

                echo echo hello | powershell
                

                Does not echo hello.

                This shows that you cannot drive powershell by repeatedly sending commands to its input in the way you (apparently) can for cmd.

                You can get it to read one command, execute it and exit (like for cmd) via:

                echo echo hello | powershell -Command -
                

                According to my findings, you can keep it running to repeatedly read & execute commands via:

                powershell -NoExit -Command -
                

                so your Qt code will need to be:

                process->start("powershell.exe", QStringList() << "-NoExit" << "-Command" << "-");
                

                Now you can send it commands. Note that I found (from the Command Prompt, not tested from this Qt code) that receiving end-of-file (i.e. closing process's stdin from Qt) did not seem to terminate the Poweshell. Only sending it exit as a command did that, so you may have to do same from your Qt code, you'll have to test.

                M 1 Reply Last reply 11 Jul 2020, 18:34
                1
                • J JonB
                  10 Jul 2020, 22:16

                  @malik-boulakrachef
                  So that indicates that powershell.exe acts differently from cmd.exe. Your calling code is the same in both cases, so it's not a Qt issue.

                  From a Command Prompt, compare the behaviour of these two:

                  echo echo hello | cmd
                  

                  Does echo hello.

                  echo echo hello | powershell
                  

                  Does not echo hello.

                  This shows that you cannot drive powershell by repeatedly sending commands to its input in the way you (apparently) can for cmd.

                  You can get it to read one command, execute it and exit (like for cmd) via:

                  echo echo hello | powershell -Command -
                  

                  According to my findings, you can keep it running to repeatedly read & execute commands via:

                  powershell -NoExit -Command -
                  

                  so your Qt code will need to be:

                  process->start("powershell.exe", QStringList() << "-NoExit" << "-Command" << "-");
                  

                  Now you can send it commands. Note that I found (from the Command Prompt, not tested from this Qt code) that receiving end-of-file (i.e. closing process's stdin from Qt) did not seem to terminate the Poweshell. Only sending it exit as a command did that, so you may have to do same from your Qt code, you'll have to test.

                  M Offline
                  M Offline
                  malik boulakrachef
                  wrote on 11 Jul 2020, 18:34 last edited by
                  #9

                  @JonB thank's for your response
                  wheni use this:
                  process->start("powershell.exe", QStringList() << "-NoExit" << "-Command" << "-");
                  i get the same error when i send different commands :
                  error: QProcess::start: Process is already running

                  J 1 Reply Last reply 11 Jul 2020, 18:50
                  0
                  • M malik boulakrachef
                    11 Jul 2020, 18:34

                    @JonB thank's for your response
                    wheni use this:
                    process->start("powershell.exe", QStringList() << "-NoExit" << "-Command" << "-");
                    i get the same error when i send different commands :
                    error: QProcess::start: Process is already running

                    J Offline
                    J Offline
                    JonB
                    wrote on 11 Jul 2020, 18:50 last edited by JonB 7 Nov 2020, 18:51
                    #10

                    @malik-boulakrachef
                    That is not from the command you are trying to issue, You have used that QProcess instance already to start a program, and now you are trying to start another one. Either wait & clear up, or use a different instance (recommended).

                    M 1 Reply Last reply 11 Jul 2020, 19:12
                    2
                    • J JonB
                      11 Jul 2020, 18:50

                      @malik-boulakrachef
                      That is not from the command you are trying to issue, You have used that QProcess instance already to start a program, and now you are trying to start another one. Either wait & clear up, or use a different instance (recommended).

                      M Offline
                      M Offline
                      malik boulakrachef
                      wrote on 11 Jul 2020, 19:12 last edited by
                      #11

                      @JonB
                      ok,because of you i have understand the fonctionnement of QProcess, now the code work very well, i am very grateful for you, thank you

                      1 Reply Last reply
                      0

                      11/11

                      11 Jul 2020, 19:12

                      • 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