How to use Windows QT to call WSL cmd?
-
@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?
@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 alsoHello
inside this file. It only fails when related towsl
.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! -
@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 alsoHello
inside this file. It only fails when related towsl
.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!@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.
-
@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.
@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?
-
@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?
@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? -
@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?@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. -
@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. -
@NTU_WTY
I would connect QProcess::stateChanged() next.@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
-
@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
-
@NTU_WTY
Have you put thisconnect()
(indeed any/allconnect()
s) before yoursh->start(program, arguments);
? We want this signal connected and reporting before it attempts to start the program. -
@JonB OK, now it outputs three lines:
- QProcess::Starting
- QProcess::Running
- QProcess::NotRunning
@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
andarguments
. For examplesh->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 anls
and output whatever that outputs, providedwsl
allows you do that (I would not know if there is some issue there). -
@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
andarguments
. For examplesh->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 anls
and output whatever that outputs, providedwsl
allows you do that (I would not know if there is some issue there).@JonB Thank you so much. Do I need to set
parent
as nullptr? And thereadyReadStandardError
andreadyReadStandardOutput
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 indir_res.txt
with Qt output as:- QProcess::Starting
- QProcess::Running
- QProcess::NotRunning
- 0 QProcess::NormalExit
The
wsl
command still creates empty file inls_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
-
@JonB Thank you so much. Do I need to set
parent
as nullptr? And thereadyReadStandardError
andreadyReadStandardOutput
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 indir_res.txt
with Qt output as:- QProcess::Starting
- QProcess::Running
- QProcess::NotRunning
- 0 QProcess::NormalExit
The
wsl
command still creates empty file inls_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
@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
andreadyReadStandardOutput
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.... -
@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
andreadyReadStandardOutput
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....@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
-
@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
@NTU_WTY said in How to use Windows QT to call WSL cmd?:
The error is: Use of undeclared identifier 'readAllStandardError'
I think you copied what I had written when I first posted, I corrected that to
sh->readAll....()
soon after as you can see in my earlier post and that is what you have now, so you are seeing the output correctly.So you now show the
dir
command working just as we would expect, good. Now you must try the other two. Ah, I see you are saying withC:\Windows\System32\wsl.exe
as the command you always getQProcess::FailedToStart
, right? Well, you were the one who said this is the full path to thewsl
executable, not me! Before we revert to trying to call it viacmd /c
, could you please tell me exactly what you have inC:\Windows\System32
directory which iswsl
-something? -
@NTU_WTY said in How to use Windows QT to call WSL cmd?:
The error is: Use of undeclared identifier 'readAllStandardError'
I think you copied what I had written when I first posted, I corrected that to
sh->readAll....()
soon after as you can see in my earlier post and that is what you have now, so you are seeing the output correctly.So you now show the
dir
command working just as we would expect, good. Now you must try the other two. Ah, I see you are saying withC:\Windows\System32\wsl.exe
as the command you always getQProcess::FailedToStart
, right? Well, you were the one who said this is the full path to thewsl
executable, not me! Before we revert to trying to call it viacmd /c
, could you please tell me exactly what you have inC:\Windows\System32
directory which iswsl
-something?@JonB said in How to use Windows QT to call WSL cmd?:
could you please tell me exactly what you have in C:\Windows\System32 directory which is wsl-something?
There are 3 files containing "wsl" in my C:\Windows\System32 directory by entering
dir "wsl*" /s
in the terminal:- wsl.exe
- wslapi.dll
- wslconfig.exe
-
@JonB said in How to use Windows QT to call WSL cmd?:
could you please tell me exactly what you have in C:\Windows\System32 directory which is wsl-something?
There are 3 files containing "wsl" in my C:\Windows\System32 directory by entering
dir "wsl*" /s
in the terminal:- wsl.exe
- wslapi.dll
- wslconfig.exe
@NTU_WTY
Then I have no idea why you cannot executeC:\Windows\System32\wsl.exe
fromQProcess
, and you getQProcess::FailedToStart
.So the best we can do is run these:
sh->start("cmd", QStringList() "/c" << "C:\\Windows\\System32\\wsl.exe --help"); sh->start("cmd", QStringList() "/c" << "C:\\Windows\\System32\\wsl.exe ls");
If you have to enter just
wsl
instead ofC:\\Windows\\System32\\wsl.exe
then please do so. We want to see whether either of these send back any output. -
@JonB said in How to use Windows QT to call WSL cmd?:
could you please tell me exactly what you have in C:\Windows\System32 directory which is wsl-something?
There are 3 files containing "wsl" in my C:\Windows\System32 directory by entering
dir "wsl*" /s
in the terminal:- wsl.exe
- wslapi.dll
- wslconfig.exe
-
@NTU_WTY
BIG QUESTION (possibly): Are you compiling your Qt application as 64-bit or as 32-bit??@JonB
The full output is:20:23:41: Starting C:/Users/USER/Documents/Code/Qt/build-testQT-Desktop_Qt_5_15_2_MinGW_32_bit-Debug/debug/testQT.exe... QProcess::Starting QProcess::Running stderr: "'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" QProcess::NotRunning 1 QProcess::NormalExit 20:23:47: C:/Users/USER/Documents/Code/Qt/build-testQT-Desktop_Qt_5_15_2_MinGW_32_bit-Debug/debug/testQT.exe exited with code
If I replace the full path to just "wsl":
20:26:02: Starting C:/Users/USER/Documents/Code/Qt/build-testQT-Desktop_Qt_5_15_2_MinGW_32_bit-Debug/debug/testQT.exe... QProcess::Starting QProcess::Running stderr: "'wsl' \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" QProcess::NotRunning 1 QProcess::NormalExit 20:26:08: C:/Users/USER/Documents/Code/Qt/build-testQT-Desktop_Qt_5_15_2_MinGW_32_bit-Debug/debug/testQT.exe exited with code 0
Shall I try 64-bit?
-
@JonB
The full output is:20:23:41: Starting C:/Users/USER/Documents/Code/Qt/build-testQT-Desktop_Qt_5_15_2_MinGW_32_bit-Debug/debug/testQT.exe... QProcess::Starting QProcess::Running stderr: "'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" QProcess::NotRunning 1 QProcess::NormalExit 20:23:47: C:/Users/USER/Documents/Code/Qt/build-testQT-Desktop_Qt_5_15_2_MinGW_32_bit-Debug/debug/testQT.exe exited with code
If I replace the full path to just "wsl":
20:26:02: Starting C:/Users/USER/Documents/Code/Qt/build-testQT-Desktop_Qt_5_15_2_MinGW_32_bit-Debug/debug/testQT.exe... QProcess::Starting QProcess::Running stderr: "'wsl' \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" QProcess::NotRunning 1 QProcess::NormalExit 20:26:08: C:/Users/USER/Documents/Code/Qt/build-testQT-Desktop_Qt_5_15_2_MinGW_32_bit-Debug/debug/testQT.exe exited with code 0
Shall I try 64-bit?
@NTU_WTY
We may have some big clue here.....I have been typing an awful lot into this thread! You must try to do some common-sense things yourself, without my having to instruct you every time.
The first thing thing is we are getting some output/message here. I want you/me to be able to read it intelligibly! Try something to turn it into text for your
qDebug()
, maybe calltoStdString()
on theQByteArray
returned orQString::fromUtf8()
. Please don't ask me, I don't have Qt on Windows, I don't know. There must be examples out there.The other thing is I now see
build-testQT-Desktop_Qt_5_15_2_MinGW_32_bit-Debug
. I had no idea you were making a 32-bit executable. Now, I have a feeling this may be the cause of the problem. You are trying to run\windows\system32\wsl.exe
. I am thinking that is 64-bit. And I am guessing you cannot run that from 32-bit?In a Command Prompt do a
dir /s/b c:\wsl.exe
. That will search your whole drive. I know you'll getC:\windows\system32\wsl.exe
, but do you also get aC:\windows\SysWOW64\wsl.exe
? I am thinking you might need one of:- Change your Qt application to compile for 64-bit and then
C:\windows\system32\wsl.exe
should work? - In your current 32-bit try
C:\windows\SysWOW64\wsl.exe
if that exists? It is also possible that wsl demands 64-bit and will not work under 32-bit, did you look that up to see?
Get the commands we have discussed working --- or at least producing readable messages --- before you try to introduce redirections like
>>
--- trust me!wsl --help
should be your starting command --- that ought produce some usage output, which you should be able to capture and see successfully, before you move onto trying an actual Linux command likels
and/or output redirection. - Change your Qt application to compile for 64-bit and then
-
@NTU_WTY
We may have some big clue here.....I have been typing an awful lot into this thread! You must try to do some common-sense things yourself, without my having to instruct you every time.
The first thing thing is we are getting some output/message here. I want you/me to be able to read it intelligibly! Try something to turn it into text for your
qDebug()
, maybe calltoStdString()
on theQByteArray
returned orQString::fromUtf8()
. Please don't ask me, I don't have Qt on Windows, I don't know. There must be examples out there.The other thing is I now see
build-testQT-Desktop_Qt_5_15_2_MinGW_32_bit-Debug
. I had no idea you were making a 32-bit executable. Now, I have a feeling this may be the cause of the problem. You are trying to run\windows\system32\wsl.exe
. I am thinking that is 64-bit. And I am guessing you cannot run that from 32-bit?In a Command Prompt do a
dir /s/b c:\wsl.exe
. That will search your whole drive. I know you'll getC:\windows\system32\wsl.exe
, but do you also get aC:\windows\SysWOW64\wsl.exe
? I am thinking you might need one of:- Change your Qt application to compile for 64-bit and then
C:\windows\system32\wsl.exe
should work? - In your current 32-bit try
C:\windows\SysWOW64\wsl.exe
if that exists? It is also possible that wsl demands 64-bit and will not work under 32-bit, did you look that up to see?
Get the commands we have discussed working --- or at least producing readable messages --- before you try to introduce redirections like
>>
--- trust me!wsl --help
should be your starting command --- that ought produce some usage output, which you should be able to capture and see successfully, before you move onto trying an actual Linux command likels
and/or output redirection.- Sorry that I haven't found the way to make the qDebug output readable.
- The MinGW 64-bit running current Qprocess still shows "NotRunning". However, after I tested different combination for a while, I found that it works for my beginning way of
QProcess::execute("cmd /c C:/Windows/System32/wsl.exe ls >> C:\\Temp\\res.txt")
. Before it outputswsl is not recognized as an internal or external command
, now it can successfully redirect whatever the wsl command output to a .txt. - If I replace
C:/Windows/System32/wsl.exe
towsl.exe
orC:/Windows/WinSxS/amd64_microsoft-windows-lxss-wsl_.../wsl.exe
, they all works.
Thank you for spending so long time to solve my problem!
- Change your Qt application to compile for 64-bit and then