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. How to use Windows QT to call WSL cmd?
Forum Updated to NodeBB v4.3 + New Features

How to use Windows QT to call WSL cmd?

Scheduled Pinned Locked Moved Unsolved General and Desktop
37 Posts 3 Posters 5.2k 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.
  • N NTU_WTY
    29 Aug 2022, 08:26

    @JonB It's true. I also check readAllStandardError() but I'm not sure I wrote it correctly: qDebug() << sh.readAllStandardError();, and it prints:

    "'C:\\Windows\\System32\\wsl.exe' \xA4\xA3\xACO\xA4\xBA\xB3\xA1\xA9\xCE\xA5~\xB3\xA1\xA9R\xA5O\xA1""B\xA5i\xB0\xF5\xA6\xE6\xAA\xBA\xB5{\xA6\xA1\xA9\xCE\xA7\xE5\xA6\xB8\xC0\xC9\xA1""C\r\n
    
    J Offline
    J Offline
    JonB
    wrote on 29 Aug 2022, 08:35 last edited by JonB
    #11

    @NTU_WTY
    That looks worrying to me! Whatever it's supposed to be, the fact that something is even echoing back 'C:\\Windows\\System32\\wsl.exe' ... looks like some kind of error to me....

    I have asked several times what exactly you tested in Command Prompt....

    Oh, and a huge "BTW" when you do use the redirection and say res.txt is still empty: where are you looking, what do you think the current working directory of your Qt app is? Have you checked with specifying an absolute path?

    Please stop trying the redirection for a while, it only complicates. Try one of these:

    • sh.start("cmd", QStringList() << "/c" << "C:\\Windows\\System32\\wsl.exe ls");
    • sh.start("C:\\Windows\\System32\\wsl.exe", QStringList() << "ls");

    Especially the second one. I would not use waitFor..., at least while I diagnosed, I would use the signals/slots including for errors, as I wrote earlier.

    1 Reply Last reply
    0
    • J JonB
      29 Aug 2022, 08:24

      @NTU_WTY said in How to use Windows QT to call WSL cmd?:

      No error shows after writing in QStringList, not as it would show: "wsl is not recognized as an internal or external command" when writing in my original QProcess::execute way.`

      What do you mean by this? If you are saying you originally used QProcess::execute("C:\\Windows\\System32\\wsl.exe", ...) or "wsl" or "cmd /c ..." or whatever and got "wsl is not recognized as an internal or external command" then please show what exact command you did that way.

      Your latest reply does not say what returned from waitForFinished (cut off)?

      I trust you really tried cmd /c C:\Windows\System32\wsl.exe verbatim in a Command Prompt?

      If you cannot resolve this, stop using waitForFinished, hook up all the signals from QProcess and see what is happening. Like I wrote earlier, start by diagnosing/getting it working without the file redirection.

      N Offline
      N Offline
      NTU_WTY
      wrote on 29 Aug 2022, 09:13 last edited by
      #12

      No error shows after writing in QStringList, not as it would show: "wsl is not recognized as an internal or external command" when writing in my original QProcess::execute way.`

      What do you mean by this? If you are saying you originally used QProcess::execute("C:\\Windows\\System32\\wsl.exe", ...) or "wsl" or "cmd /c ..." or whatever and got "wsl is not recognized as an internal or external command" then please show what exact command you did that way.

      QProcess::execute("cmd /c wsl ls")
      QProcess::execute("cmd /c C:/Windows/System32/wsl.exe ls");
      I wrote in above ways that both not recognized wsl. But if I open the cmd prompt, power shell, or MinGW terminal, they all recognize wsl.

      About the following two lines:

      • sh.start("cmd", QStringList() << "/c" << "C:\\Windows\\System32\\wsl.exe ls");
      • sh.start("C:\\Windows\\System32\\wsl.exe", QStringList() << "ls");

      They will get false returned from waitForFinished immediately. But the readAllStandardError() shows empty. (Previous true also immediately. Not sure what does that mean...)
      Sorry I don't know how to "hook up all signals from QProcess". Can you give me more hint when you have time? Thank you very much.

      J 1 Reply Last reply 29 Aug 2022, 09:27
      0
      • N NTU_WTY
        29 Aug 2022, 09:13

        No error shows after writing in QStringList, not as it would show: "wsl is not recognized as an internal or external command" when writing in my original QProcess::execute way.`

        What do you mean by this? If you are saying you originally used QProcess::execute("C:\\Windows\\System32\\wsl.exe", ...) or "wsl" or "cmd /c ..." or whatever and got "wsl is not recognized as an internal or external command" then please show what exact command you did that way.

        QProcess::execute("cmd /c wsl ls")
        QProcess::execute("cmd /c C:/Windows/System32/wsl.exe ls");
        I wrote in above ways that both not recognized wsl. But if I open the cmd prompt, power shell, or MinGW terminal, they all recognize wsl.

        About the following two lines:

        • sh.start("cmd", QStringList() << "/c" << "C:\\Windows\\System32\\wsl.exe ls");
        • sh.start("C:\\Windows\\System32\\wsl.exe", QStringList() << "ls");

        They will get false returned from waitForFinished immediately. But the readAllStandardError() shows empty. (Previous true also immediately. Not sure what does that mean...)
        Sorry I don't know how to "hook up all signals from QProcess". Can you give me more hint when you have time? Thank you very much.

        J Offline
        J Offline
        JonB
        wrote on 29 Aug 2022, 09:27 last edited by
        #13

        @NTU_WTY said in How to use Windows QT to call WSL cmd?:

        I wrote in above ways that both not recognized wsl. But if I open the cmd prompt, power shell, or MinGW terminal, they all recognize wsl.

        Then you have a problem. This is my final time of asking: copy and paste, or show me a screenshot, of precisely what you are trying in a Command Prompt. Do not tell me "they work", show me what you are trying, character for character.

        Sorry I don't know how to "hook up all signals from QProcess"

        At minimum connect a slot to void QProcess::errorOccurred(QProcess::ProcessError error):

        • Make your QProcess sh; a class member variable, at least temporarily, so it stays in scope.
        • Start with: connect(&sh, &QProcess::errorOccurred, this, [](QProcess::ProcessError error) { qDebug() << error; }), we'll see whether we need any further ones depending on that.
        N 1 Reply Last reply 29 Aug 2022, 09:44
        2
        • J JonB
          29 Aug 2022, 09:27

          @NTU_WTY said in How to use Windows QT to call WSL cmd?:

          I wrote in above ways that both not recognized wsl. But if I open the cmd prompt, power shell, or MinGW terminal, they all recognize wsl.

          Then you have a problem. This is my final time of asking: copy and paste, or show me a screenshot, of precisely what you are trying in a Command Prompt. Do not tell me "they work", show me what you are trying, character for character.

          Sorry I don't know how to "hook up all signals from QProcess"

          At minimum connect a slot to void QProcess::errorOccurred(QProcess::ProcessError error):

          • Make your QProcess sh; a class member variable, at least temporarily, so it stays in scope.
          • Start with: connect(&sh, &QProcess::errorOccurred, this, [](QProcess::ProcessError error) { qDebug() << error; }), we'll see whether we need any further ones depending on that.
          N Offline
          N Offline
          NTU_WTY
          wrote on 29 Aug 2022, 09:44 last edited by
          #14

          @JonB said in How to use Windows QT to call WSL cmd?:

          @NTU_WTY said in How to use Windows QT to call WSL cmd?:

          I wrote in above ways that both not recognized wsl. But if I open the cmd prompt, power shell, or MinGW terminal, they all recognize wsl.

          Then you have a problem. This is my final time of asking: copy and paste, or show me a screenshot, of precisely what you are trying in a Command Prompt. Do not tell me "they work", show me what you are trying, character for character.
          26d6eb5d-82e2-406c-8a7f-b7a176c54895-image.png

          J 1 Reply Last reply 29 Aug 2022, 09:51
          0
          • N NTU_WTY
            29 Aug 2022, 09:44

            @JonB said in How to use Windows QT to call WSL cmd?:

            @NTU_WTY said in How to use Windows QT to call WSL cmd?:

            I wrote in above ways that both not recognized wsl. But if I open the cmd prompt, power shell, or MinGW terminal, they all recognize wsl.

            Then you have a problem. This is my final time of asking: copy and paste, or show me a screenshot, of precisely what you are trying in a Command Prompt. Do not tell me "they work", show me what you are trying, character for character.
            26d6eb5d-82e2-406c-8a7f-b7a176c54895-image.png

            J Offline
            J Offline
            JonB
            wrote on 29 Aug 2022, 09:51 last edited by
            #15

            @NTU_WTY
            So all you have proved is that wsl ls works from that Command Prompt. Which is not the same as the command you are trying to issue from your Qt program, is it? Maybe it's wsl.exe, maybe it's not. Maybe it's in C:\Windows\System32, maybe it's not.

            Will you please try:

            cmd /c "C:\Windows\System32\wsl.exe ls"
            

            in a Command Prompt.

            After that, try the error code I showed earlier.

            N 1 Reply Last reply 29 Aug 2022, 10:34
            0
            • J JonB
              29 Aug 2022, 09:51

              @NTU_WTY
              So all you have proved is that wsl ls works from that Command Prompt. Which is not the same as the command you are trying to issue from your Qt program, is it? Maybe it's wsl.exe, maybe it's not. Maybe it's in C:\Windows\System32, maybe it's not.

              Will you please try:

              cmd /c "C:\Windows\System32\wsl.exe ls"
              

              in a Command Prompt.

              After that, try the error code I showed earlier.

              N Offline
              N Offline
              NTU_WTY
              wrote on 29 Aug 2022, 10:34 last edited by
              #16

              @JonB
              Sorry that I choose the wrong terminal. The following is my "C:\Windows\System32\wsl.exe" result:
              d2d020db-c4dd-4b29-965c-15713891f333-image.png

              If I directly type: cmd /c "C:\Windows\System32\wsl.exe ls" in the search input, the terminal will flash out (successfully show and close). So I modified to: cmd /k "C:\Windows\System32\wsl.exe ls" , it opens the terminal and lists all the files inside.
              I also tried: cmd /k "C:\Windows\System32\wsl.exe pwd", and it returns 6073b618-8b08-4a4f-98f7-96c92f0be042-image.png,
              where /mnt/c is the correct representation of the path C:\ in wsl.

              About the slot, I think I need more time to digest, thank you~

              J 1 Reply Last reply 29 Aug 2022, 10:52
              0
              • N NTU_WTY
                29 Aug 2022, 10:34

                @JonB
                Sorry that I choose the wrong terminal. The following is my "C:\Windows\System32\wsl.exe" result:
                d2d020db-c4dd-4b29-965c-15713891f333-image.png

                If I directly type: cmd /c "C:\Windows\System32\wsl.exe ls" in the search input, the terminal will flash out (successfully show and close). So I modified to: cmd /k "C:\Windows\System32\wsl.exe ls" , it opens the terminal and lists all the files inside.
                I also tried: cmd /k "C:\Windows\System32\wsl.exe pwd", and it returns 6073b618-8b08-4a4f-98f7-96c92f0be042-image.png,
                where /mnt/c is the correct representation of the path C:\ in wsl.

                About the slot, I think I need more time to digest, thank you~

                J Offline
                J Offline
                JonB
                wrote on 29 Aug 2022, 10:52 last edited by
                #17

                @NTU_WTY said in How to use Windows QT to call WSL cmd?:

                If I directly type: cmd /c "C:\Windows\System32\wsl.exe ls" in the search input

                In what "search input"? The Windows desktop Start button area, or similar? I asked you to type into a Command Prompt...! Never mind, I get the gist, it looks like the full path does work, that in itself is not the issue.

                Yes you should try the code with signal/slot I showed earlier.

                You might also try e.g.

                sh.start("cmd", QStringList() << "/c" << "echo Hello >> C:\\Temp\\tempfile.txt");
                

                Please be sensible about the path, whatever works for you. Does this work?

                N 1 Reply Last reply 29 Aug 2022, 13:36
                0
                • J JonB
                  29 Aug 2022, 10:52

                  @NTU_WTY said in How to use Windows QT to call WSL cmd?:

                  If I directly type: cmd /c "C:\Windows\System32\wsl.exe ls" in the search input

                  In what "search input"? The Windows desktop Start button area, or similar? I asked you to type into a Command Prompt...! Never mind, I get the gist, it looks like the full path does work, that in itself is not the issue.

                  Yes you should try the code with signal/slot I showed earlier.

                  You might also try e.g.

                  sh.start("cmd", QStringList() << "/c" << "echo Hello >> C:\\Temp\\tempfile.txt");
                  

                  Please be sensible about the path, whatever works for you. Does this work?

                  N Offline
                  N Offline
                  NTU_WTY
                  wrote on 29 Aug 2022, 13:36 last edited by
                  #18

                  @JonB said in How to use Windows QT to call WSL cmd?:

                  You might also try e.g.
                  sh.start("cmd", QStringList() << "/c" << "echo Hello >> C:\Temp\tempfile.txt");

                  Please be sensible about the path, whatever works for you. Does this work?

                  Yes! It generates tempfile and also Hello inside this file. It only fails when related to wsl.

                  I try to rewrite the code by mimicking from the doc:

                  QObject *parent;
                  QString program = "cmd";
                  QStringList arguments;
                  arguments << "/c" << "C:\\Windows\\System32\\wsl.exe" << "ls"; 
                  QProcess *sh = new QProcess(parent);
                  sh->start(program, arguments);
                  
                  connect(sh, &QProcess::errorOccurred, this, [](QProcess::ProcessError error) { qDebug() << error; });
                  

                  It outputs this when the object is created:
                  21:17:42: C:/Users/USER/Documents/Code/Qt/build-testQT-Desktop_Qt_5_15_2_MinGW_32_bit-Debug/debug/testQT.exe crashed.
                  I'm not sure if I wrote it right. Thank you for your patience~ Please correct me and give me more hints!

                  J 1 Reply Last reply 29 Aug 2022, 13:42
                  0
                  • N NTU_WTY
                    29 Aug 2022, 13:36

                    @JonB said in How to use Windows QT to call WSL cmd?:

                    You might also try e.g.
                    sh.start("cmd", QStringList() << "/c" << "echo Hello >> C:\Temp\tempfile.txt");

                    Please be sensible about the path, whatever works for you. Does this work?

                    Yes! It generates tempfile and also Hello inside this file. It only fails when related to wsl.

                    I try to rewrite the code by mimicking from the doc:

                    QObject *parent;
                    QString program = "cmd";
                    QStringList arguments;
                    arguments << "/c" << "C:\\Windows\\System32\\wsl.exe" << "ls"; 
                    QProcess *sh = new QProcess(parent);
                    sh->start(program, arguments);
                    
                    connect(sh, &QProcess::errorOccurred, this, [](QProcess::ProcessError error) { qDebug() << error; });
                    

                    It outputs this when the object is created:
                    21:17:42: C:/Users/USER/Documents/Code/Qt/build-testQT-Desktop_Qt_5_15_2_MinGW_32_bit-Debug/debug/testQT.exe crashed.
                    I'm not sure if I wrote it right. Thank you for your patience~ Please correct me and give me more hints!

                    J Offline
                    J Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on 29 Aug 2022, 13:42 last edited by
                    #19

                    @NTU_WTY said in How to use Windows QT to call WSL cmd?:

                    QObject *parent;

                    This is dangling pointer!
                    If you do not need a parent then pass nullptr instead!

                    QProcess *sh = new QProcess();
                    

                    If you really want a proper parent then pass a pointer to an existing (allocated) object.

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

                    N 1 Reply Last reply 29 Aug 2022, 14:19
                    1
                    • J jsulm
                      29 Aug 2022, 13:42

                      @NTU_WTY said in How to use Windows QT to call WSL cmd?:

                      QObject *parent;

                      This is dangling pointer!
                      If you do not need a parent then pass nullptr instead!

                      QProcess *sh = new QProcess();
                      

                      If you really want a proper parent then pass a pointer to an existing (allocated) object.

                      N Offline
                      N Offline
                      NTU_WTY
                      wrote on 29 Aug 2022, 14:19 last edited by
                      #20

                      @jsulm said in How to use Windows QT to call WSL cmd?:

                      This is dangling pointer!
                      If you do not need a parent then pass nullptr instead!
                      QProcess *sh = new QProcess();

                      If you really want a proper parent then pass a pointer to an existing (allocated) object.

                      Thank you~ The code executed successfully without any messages after null ptr correction. So... what shall I do next to get some info from the process?

                      J 1 Reply Last reply 29 Aug 2022, 14:44
                      0
                      • N NTU_WTY
                        29 Aug 2022, 14:19

                        @jsulm said in How to use Windows QT to call WSL cmd?:

                        This is dangling pointer!
                        If you do not need a parent then pass nullptr instead!
                        QProcess *sh = new QProcess();

                        If you really want a proper parent then pass a pointer to an existing (allocated) object.

                        Thank you~ The code executed successfully without any messages after null ptr correction. So... what shall I do next to get some info from the process?

                        J Offline
                        J Offline
                        JonB
                        wrote on 29 Aug 2022, 14:44 last edited by JonB
                        #21

                        @NTU_WTY said in How to use Windows QT to call WSL cmd?:

                        what shall I do next to get some info from the process?

                        So connect(&sh, &QProcess::errorOccurred, this, [](QProcess::ProcessError error) { qDebug() << error; }) does not lead to any output now when you run the wsl subprocess?

                        N 1 Reply Last reply 29 Aug 2022, 14:48
                        0
                        • J JonB
                          29 Aug 2022, 14:44

                          @NTU_WTY said in How to use Windows QT to call WSL cmd?:

                          what shall I do next to get some info from the process?

                          So connect(&sh, &QProcess::errorOccurred, this, [](QProcess::ProcessError error) { qDebug() << error; }) does not lead to any output now when you run the wsl subprocess?

                          N Offline
                          N Offline
                          NTU_WTY
                          wrote on 29 Aug 2022, 14:48 last edited by NTU_WTY
                          #22

                          @JonB said in How to use Windows QT to call WSL cmd?:

                          So connect(&sh, &QProcess::errorOccurred, this, [](QProcess::ProcessError error) { qDebug() << error; }) does not lead to any output now when you run the wsl subprocess?

                          No, no output. Actually I wrote: connect(sh, &QProcess::errorOccurred, this, [](QProcess::ProcessError error) { qDebug() << error; }); since &sh will get error.

                          J 1 Reply Last reply 29 Aug 2022, 17:03
                          0
                          • N NTU_WTY
                            29 Aug 2022, 14:48

                            @JonB said in How to use Windows QT to call WSL cmd?:

                            So connect(&sh, &QProcess::errorOccurred, this, [](QProcess::ProcessError error) { qDebug() << error; }) does not lead to any output now when you run the wsl subprocess?

                            No, no output. Actually I wrote: connect(sh, &QProcess::errorOccurred, this, [](QProcess::ProcessError error) { qDebug() << error; }); since &sh will get error.

                            J Offline
                            J Offline
                            JonB
                            wrote on 29 Aug 2022, 17:03 last edited by
                            #23

                            @NTU_WTY
                            I would connect QProcess::stateChanged() next.

                            N 1 Reply Last reply 30 Aug 2022, 07:10
                            1
                            • J JonB
                              29 Aug 2022, 17:03

                              @NTU_WTY
                              I would connect QProcess::stateChanged() next.

                              N Offline
                              N Offline
                              NTU_WTY
                              wrote on 30 Aug 2022, 07:10 last edited by
                              #24

                              @JonB said in How to use Windows QT to call WSL cmd?:

                              I would connect QProcess::stateChanged() next.

                              I modified the connect line to:

                              connect(sh, &QProcess::stateChanged, this, [](QProcess::ProcessState newState) { qDebug() << newState; });
                              

                              It outputs: QProcess::NotRunning

                              J 1 Reply Last reply 30 Aug 2022, 07:13
                              0
                              • N NTU_WTY
                                30 Aug 2022, 07:10

                                @JonB said in How to use Windows QT to call WSL cmd?:

                                I would connect QProcess::stateChanged() next.

                                I modified the connect line to:

                                connect(sh, &QProcess::stateChanged, this, [](QProcess::ProcessState newState) { qDebug() << newState; });
                                

                                It outputs: QProcess::NotRunning

                                J Offline
                                J Offline
                                JonB
                                wrote on 30 Aug 2022, 07:13 last edited by
                                #25

                                @NTU_WTY
                                Have you put this connect() (indeed any/all connect()s) before your sh->start(program, arguments);? We want this signal connected and reporting before it attempts to start the program.

                                N 1 Reply Last reply 30 Aug 2022, 07:25
                                0
                                • J JonB
                                  30 Aug 2022, 07:13

                                  @NTU_WTY
                                  Have you put this connect() (indeed any/all connect()s) before your sh->start(program, arguments);? We want this signal connected and reporting before it attempts to start the program.

                                  N Offline
                                  N Offline
                                  NTU_WTY
                                  wrote on 30 Aug 2022, 07:25 last edited by
                                  #26

                                  @JonB OK, now it outputs three lines:

                                  • QProcess::Starting
                                  • QProcess::Running
                                  • QProcess::NotRunning
                                  J 1 Reply Last reply 30 Aug 2022, 08:08
                                  0
                                  • N NTU_WTY
                                    30 Aug 2022, 07:25

                                    @JonB OK, now it outputs three lines:

                                    • QProcess::Starting
                                    • QProcess::Running
                                    • QProcess::NotRunning
                                    J Offline
                                    J Offline
                                    JonB
                                    wrote on 30 Aug 2022, 08:08 last edited by JonB
                                    #27

                                    @NTU_WTY
                                    So that implies it did start, then ran and stopped running (i.e. finished). I suspect the whole thing is working, it is not evident what "proof" you have that it is not.

                                    QProcess *sh = new QProcess(parent);
                                    connect(sh, &QProcess::errorOccurred, this, [](QProcess::ProcessError error) { qDebug() << error; });
                                    connect(sh, &QProcess::stateChanged, this, [](QProcess::ProcessState newState) { qDebug() << newState; });
                                    connect(sh, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
                                        [=](int exitCode, QProcess::ExitStatus exitStatus){ qDebug() << exitCode << exitStatus; });
                                    connect(sh, &QProcess::readyReadStandardError, this, [=]() { qDebug() << "stderr:" << sh->readAllStandardError(); });
                                    connect(sh, &QProcess::readyReadStandardOutput, this, [=]() { qDebug() << "stdout:" << sh->readAllStandardOutput(); });
                                    
                                    sh->start(program, arguments);
                                    

                                    It is now up to you to test with various parameters for program and arguments. For example

                                    sh->start("cmd", QStringList() << "/c" << "dir");
                                    

                                    should test that the above code is working as you would expect.

                                    sh->start("C:\\Windows\\System32\\wsl.exe", QStringList() << "--help");
                                    

                                    should prove that wsl runs, it should report back its "usage".

                                    sh->start("C:\\Windows\\System32\\wsl.exe", QStringList() << "ls");
                                    

                                    should get your wsl to do an ls and output whatever that outputs, provided wsl allows you do that (I would not know if there is some issue there).

                                    N 1 Reply Last reply 30 Aug 2022, 09:31
                                    1
                                    • J JonB
                                      30 Aug 2022, 08:08

                                      @NTU_WTY
                                      So that implies it did start, then ran and stopped running (i.e. finished). I suspect the whole thing is working, it is not evident what "proof" you have that it is not.

                                      QProcess *sh = new QProcess(parent);
                                      connect(sh, &QProcess::errorOccurred, this, [](QProcess::ProcessError error) { qDebug() << error; });
                                      connect(sh, &QProcess::stateChanged, this, [](QProcess::ProcessState newState) { qDebug() << newState; });
                                      connect(sh, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
                                          [=](int exitCode, QProcess::ExitStatus exitStatus){ qDebug() << exitCode << exitStatus; });
                                      connect(sh, &QProcess::readyReadStandardError, this, [=]() { qDebug() << "stderr:" << sh->readAllStandardError(); });
                                      connect(sh, &QProcess::readyReadStandardOutput, this, [=]() { qDebug() << "stdout:" << sh->readAllStandardOutput(); });
                                      
                                      sh->start(program, arguments);
                                      

                                      It is now up to you to test with various parameters for program and arguments. For example

                                      sh->start("cmd", QStringList() << "/c" << "dir");
                                      

                                      should test that the above code is working as you would expect.

                                      sh->start("C:\\Windows\\System32\\wsl.exe", QStringList() << "--help");
                                      

                                      should prove that wsl runs, it should report back its "usage".

                                      sh->start("C:\\Windows\\System32\\wsl.exe", QStringList() << "ls");
                                      

                                      should get your wsl to do an ls and output whatever that outputs, provided wsl allows you do that (I would not know if there is some issue there).

                                      N Offline
                                      N Offline
                                      NTU_WTY
                                      wrote on 30 Aug 2022, 09:31 last edited by NTU_WTY
                                      #28

                                      @JonB Thank you so much. Do I need to set parent as nullptr? And the readyReadStandardError and readyReadStandardOutput would get error so I comment them first. Here's the code I slightly modified:

                                      QProcess *sh = new QProcess();
                                      connect(sh, &QProcess::errorOccurred, this, [](QProcess::ProcessError error) { qDebug() << error; });
                                      connect(sh, &QProcess::stateChanged, this, [](QProcess::ProcessState newState) { qDebug() << newState; });
                                      connect(sh, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),[=](int exitCode, QProcess::ExitStatus exitStatus){ qDebug() << exitCode << exitStatus; });
                                      //connect(sh, &QProcess::readyReadStandardError, this, [sh]() { qDebug() << "stderr:" << readAllStandardError(); });
                                      //connect(sh, &QProcess::readyReadStandardOutput, this, [sh]() { qDebug() << "stdout:" << readAllStandardOutput(); });
                                      
                                      QString program = "cmd";
                                      QStringList arguments;
                                      arguments << "/c" << "dir" << ">>" << "C:\\Temp\\dir_res.txt";
                                      //arguments << "/c" << "C:\\Windows\\System32\\wsl.exe" << "ls" << ">>" << "C:\\Temp\\ls_res.txt";
                                      sh->start(program, arguments);
                                      

                                      The dir command successfully list the files in dir_res.txt with Qt output as:

                                      • QProcess::Starting
                                      • QProcess::Running
                                      • QProcess::NotRunning
                                      • 0 QProcess::NormalExit

                                      The wsl command still creates empty file in ls_res.txt with Qt output as:

                                      • QProcess::Starting
                                      • QProcess::Running
                                      • QProcess::NotRunning
                                      • 1 QProcess::NormalExit

                                      if I change the program directly to "C:\\Windows\\System32\\wsl.exe", the output is:

                                      • QProcess::Starting
                                      • QProcess::NotRunning
                                      • QProcess::FailedToStart
                                      J 1 Reply Last reply 30 Aug 2022, 10:03
                                      0
                                      • N NTU_WTY
                                        30 Aug 2022, 09:31

                                        @JonB Thank you so much. Do I need to set parent as nullptr? And the readyReadStandardError and readyReadStandardOutput would get error so I comment them first. Here's the code I slightly modified:

                                        QProcess *sh = new QProcess();
                                        connect(sh, &QProcess::errorOccurred, this, [](QProcess::ProcessError error) { qDebug() << error; });
                                        connect(sh, &QProcess::stateChanged, this, [](QProcess::ProcessState newState) { qDebug() << newState; });
                                        connect(sh, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),[=](int exitCode, QProcess::ExitStatus exitStatus){ qDebug() << exitCode << exitStatus; });
                                        //connect(sh, &QProcess::readyReadStandardError, this, [sh]() { qDebug() << "stderr:" << readAllStandardError(); });
                                        //connect(sh, &QProcess::readyReadStandardOutput, this, [sh]() { qDebug() << "stdout:" << readAllStandardOutput(); });
                                        
                                        QString program = "cmd";
                                        QStringList arguments;
                                        arguments << "/c" << "dir" << ">>" << "C:\\Temp\\dir_res.txt";
                                        //arguments << "/c" << "C:\\Windows\\System32\\wsl.exe" << "ls" << ">>" << "C:\\Temp\\ls_res.txt";
                                        sh->start(program, arguments);
                                        

                                        The dir command successfully list the files in dir_res.txt with Qt output as:

                                        • QProcess::Starting
                                        • QProcess::Running
                                        • QProcess::NotRunning
                                        • 0 QProcess::NormalExit

                                        The wsl command still creates empty file in ls_res.txt with Qt output as:

                                        • QProcess::Starting
                                        • QProcess::Running
                                        • QProcess::NotRunning
                                        • 1 QProcess::NormalExit

                                        if I change the program directly to "C:\\Windows\\System32\\wsl.exe", the output is:

                                        • QProcess::Starting
                                        • QProcess::NotRunning
                                        • QProcess::FailedToStart
                                        J Offline
                                        J Offline
                                        JonB
                                        wrote on 30 Aug 2022, 10:03 last edited by
                                        #29

                                        @NTU_WTY said in How to use Windows QT to call WSL cmd?:

                                        Do I need to set parent as nullptr?

                                        Doesn't matter (for our purposes), this might be a better choice.

                                        And the readyReadStandardError and readyReadStandardOutput would get error so I comment them first.

                                        No, you are not supposed to comment them out, you are supposed to make them work. Preferably on your own, if you want my help please don't say they "get error", copy & paste the line and the error message. Or do you expect someone to guess what the issue is rather than you doing that?

                                        I seem to have to repeat myself. Can you please start by doing as I ask, with code working and my commands. Don't start doing your own commands till those are resolved. I have said we need to get behaviour we understand first before you introduce redirection. Can you please do the 3 commands I wrote, and report the output, before you do your own variations. And you must get the connect(sh, &QProcess::readyRead...s working for these....

                                        N 1 Reply Last reply 30 Aug 2022, 10:31
                                        0
                                        • J JonB
                                          30 Aug 2022, 10:03

                                          @NTU_WTY said in How to use Windows QT to call WSL cmd?:

                                          Do I need to set parent as nullptr?

                                          Doesn't matter (for our purposes), this might be a better choice.

                                          And the readyReadStandardError and readyReadStandardOutput would get error so I comment them first.

                                          No, you are not supposed to comment them out, you are supposed to make them work. Preferably on your own, if you want my help please don't say they "get error", copy & paste the line and the error message. Or do you expect someone to guess what the issue is rather than you doing that?

                                          I seem to have to repeat myself. Can you please start by doing as I ask, with code working and my commands. Don't start doing your own commands till those are resolved. I have said we need to get behaviour we understand first before you introduce redirection. Can you please do the 3 commands I wrote, and report the output, before you do your own variations. And you must get the connect(sh, &QProcess::readyRead...s working for these....

                                          N Offline
                                          N Offline
                                          NTU_WTY
                                          wrote on 30 Aug 2022, 10:31 last edited by
                                          #30

                                          @JonB said in How to use Windows QT to call WSL cmd?:

                                          &QProc

                                          The error is: Use of undeclared identifier 'readAllStandardError'
                                          I tried this way to make it work (please correct me if I'm wrong):

                                          connect(sh, &QProcess::readyReadStandardError, this, [sh]() { qDebug() << "stderr:" << sh->readAllStandardError(); });
                                          connect(sh, &QProcess::readyReadStandardOutput, this, [sh]() { qDebug() << "stdout:" << sh->readAllStandardOutput(); });
                                          

                                          After this modification, the output of sh->start("C:\\Windows\\System32\\wsl.exe", QStringList() << "--help"); is:

                                          • QProcess::Starting
                                          • QProcess::NotRunning
                                          • QProcess::FailedToStart

                                          same as the output of sh->start("C:\\Windows\\System32\\wsl.exe", QStringList() << "ls");

                                          The output of sh->start("cmd", QStringList() << "/c" << "dir"); is:

                                          QProcess::Starting
                                          QProcess::Running
                                          stdout: " \xBA\xCF\xBA\xD0\xB0\xCF C \xA4\xA4\xAA\xBA\xBA\xCF\xBA\xD0\xA8S\xA6\xB3\xBC\xD0\xC5\xD2\xA1""C\r\n \xBA\xCF\xBA\xD0\xB0\xCF\xA7\xC7\xB8\xB9:  4E94-6F25\r\n\r\n C:\\Users\\USER\\Documents\\Code\\Qt\\build-testQT-Desktop_Qt_5_15_2_MinGW_32_bit-Debug \xAA\xBA\xA5\xD8\xBF\xFD\r\n\r\n"
                                          stdout: "2022/08/30  \xA4U\xA4\xC8 03:26    <DIR>          .\r\n2022/08/30  \xA4U\xA4\xC8 03:26    <DIR>          ..\r\n2022/08/23  \xA4U\xA4\xC8 06:37               985 .qmake.stash\r\n2022/08/23  \xA4U\xA4\xC8 04:29    <DIR>          .qtc_clangd\r\n2022/08/30  \xA4U\xA4\xC8 06:29    <DIR>          debug\r\n2022/08/24  \xA4W\xA4\xC8 09:43            27,314 Makefile\r\n2022/08/24  \xA4W\xA4\xC8 09:43            36,629 Makefile.Debug\r\n2022/08/24  \xA4W\xA4\xC8 09:43            36,723 Makefile.Release\r\n"
                                          stdout: "2022/08/23  \xA4U\xA4\xC8 06:37    <DIR>          release\r\n2022/08/29  \xA4U\xA4\xC8 03:04                 0 res.txt\r\n2022/08/24  \xA4W\xA4\xC8 10:13    <DIR>          Test\r\n2022/08/24  \xA4W\xA4\xC8 09:43             2,976 ui_mainwindow.h\r\n2022/08/29  \xA4U\xA4\xC8 03:55                 0 usersuserappdatalocaltemptmp1ia32l\r\n2022/08/30  \xA4U\xA4\xC8 03:26                 0 usersuserappdatalocaltemptmp1raevz\r\n2022/08/29  \xA4U\xA4\xC8 04:50                 0 usersuserappdatalocaltemptmp6vdzgw\r\n2022/08/24  \xA4W\xA4\xC8 09:45                 0 usersuserappdatalocaltemptmpc2kqux\r\n2022/08/29  \xA4U\xA4\xC8 09:05                 0 usersuserappdatalocaltemptmpdaqef2\r\n2022/08/24  \xA4W\xA4\xC8 10:02                 0 usersuserappdatalocaltemptmpeilcjn\r\n2022/08/29  \xA4U\xA4\xC8 03:51                 0 usersuserappdatalocaltemptmpfppfsf\r\n2022/08/29  \xA4U\xA4\xC8 10:06                 0 usersuserappdatalocaltemptmphst0vl\r\n2022/08/29  \xA4W\xA4\xC8 11:18                 0 usersuserappdatalocaltemptmpmxin8w\r\n2022/08/26  \xA4W\xA4\xC8 08:19                 0 usersuserappdatalocaltemptmppjmq8y\r\n2022/08/29  \xA4U\xA4\xC8 10:08                 0 usersuserappdatalocaltemptmpxs1ja2\r\n2022/08/29  \xA4U\xA4\xC8 03:15                 0 usersuserappdatalocaltemptmpypomgz\r\n              18 \xAD\xD3\xC0\xC9\xAE\xD7         104,627 \xA6\xEC\xA4\xB8\xB2\xD5\r\n"
                                          stdout: "               6 \xAD\xD3\xA5\xD8\xBF\xFD  597,839,917,056 \xA6\xEC\xA4\xB8\xB2\xD5\xA5i\xA5\xCE\r\n"
                                          QProcess::NotRunning
                                          0 QProcess::NormalExit
                                          
                                          J 1 Reply Last reply 30 Aug 2022, 10:57
                                          0

                                          20/37

                                          29 Aug 2022, 14:19

                                          • Login

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