write on QProcess powershell
-
wrote on 9 Jul 2020, 19:23 last edited by
I am working on a project with Qt5, I have a problem with the use of QProcess is as follows: I want to create a PowerShell process which has the role of intercepting a command sequence and executing them. the concern is that each time I execute a new command, I have to close the process and restart it with new arguments using the "start (processPath, arguments)" command. i have tried to write on the process with command "write" but it doesn't work, i have replace powershell with cmd and it work perfectly.
-
wrote on 9 Jul 2020, 19:32 last edited by
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)
-
wrote on 9 Jul 2020, 19:51 last edited by
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(); -
wrote on 9 Jul 2020, 20:26 last edited by
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"));
-
wrote on 10 Jul 2020, 17:58 last edited by
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 -
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();wrote on 10 Jul 2020, 18:17 last edited by@malik-boulakrachef
You are aware you could achieve this viapowershell.exe -Command notepad
, aren't you? -
@malik-boulakrachef
You are aware you could achieve this viapowershell.exe -Command notepad
, aren't you?wrote on 10 Jul 2020, 19:46 last edited by@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 -
@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 perfectlywrote on 10 Jul 2020, 22:16 last edited by JonB 7 Nov 2020, 09:17@malik-boulakrachef
So that indicates thatpowershell.exe
acts differently fromcmd.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 forcmd
.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. -
@malik-boulakrachef
So that indicates thatpowershell.exe
acts differently fromcmd.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 forcmd
.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.wrote on 11 Jul 2020, 18:34 last edited by@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 -
@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 runningwrote on 11 Jul 2020, 18:50 last edited by JonB 7 Nov 2020, 18:51@malik-boulakrachef
That is not from the command you are trying to issue, You have used thatQProcess
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). -
@malik-boulakrachef
That is not from the command you are trying to issue, You have used thatQProcess
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).wrote on 11 Jul 2020, 19:12 last edited by@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/11