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.
  • hskoglundH Offline
    hskoglundH Offline
    hskoglund
    wrote on 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 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();

      JonBJ 1 Reply Last reply
      0
      • hskoglundH Offline
        hskoglundH Offline
        hskoglund
        wrote on 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 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

            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();

            JonBJ Online
            JonBJ Online
            JonB
            wrote on 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
            1
            • JonBJ JonB

              @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 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

              JonBJ 1 Reply Last reply
              0
              • M malik boulakrachef

                @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

                JonBJ Online
                JonBJ Online
                JonB
                wrote on last edited by JonB
                #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
                1
                • JonBJ JonB

                  @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 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

                  JonBJ 1 Reply Last reply
                  0
                  • M malik boulakrachef

                    @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

                    JonBJ Online
                    JonBJ Online
                    JonB
                    wrote on last edited by JonB
                    #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
                    2
                    • JonBJ JonB

                      @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 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

                      • Login

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