QT, C++ code not executing powershell script
-
I am working on GUI C++ ,QT. where I press on the button the code inside the button should execute powershell script but it fails to execute the powershell. Your help would be appreciated.
QDir directory;
QString current = directory.currentPath();
QString scriptpath = current + "/logs.ps1";
qDebug() << " script path is" <<scriptpath;
if (!QFile::exists(scriptpath)) {
qDebug() << "Script file does not exist:" << scriptpath;
return;
}QStringList arguments; arguments << "-File" << scriptpath << "-AS";
QProcess process;
process.start("powershell.exe", arguments);if (!process.waitForStarted(-1)) {
qDebug() << "Failed to start the process:" << process.errorString();
return;
}
when I run I get this error:
QProcess: Destroyed while process ("powershell.exe") is still running.I tried the same script path in powershell manually it is working
powershell.exe -File <scriptpath> -Arguments . It is working but via code it is not working. -
@Creat_ur_ownempire said in QT, C++ code not executing powershell script:
QProcess process;
Think about the scope/lifetime of this variable. It needs to persist throughout the running of the process. Yours does not. Hence the error message and bad behaviour.
And please format your code with the forum's Code tags (
</>
button), it would be a lot easier to spot if you do. -
@JonB . Thank you for guiding me. The code runs in a function.
</
void func()
{
QProcess process;
process.start("powershell.exe", arguments);
}If I understood correctly . The scope of this variable is local to function changing this scope to global will it work.
can you provide more info. -
@Creat_ur_ownempire
That is the same as before. It's a C++ variable. So since it's local it gets destroyed at the end of the function. Which is no good if all you have done is started powershell, since it needs to keep running, unless you make your function wait for it to finish.Usually a
QProcess
is a class member variable or it's anew
ed pointer. If you want it to outlast a function.Or, if you want your function to run the sub-process and wait for it, you may find the
static
int QProcess::execute(const QString &program, const QStringList &arguments = {}) is all you need. -
@JonB . if (!process.waitForFinished(-1)) I am using this one. I will update whether it works or not
-
@Creat_ur_ownempire
Yes, that will also work since the process has finished by the time the function exits and theQProcess process
variable gets destroyed. -
@JonB - It's not working. QProcess: Destroyed while process ("powershell.exe") is still running.
</
QProcess process;
process.start("powershell.exe", arguments);
if (!process.waitForFinished(-1)) {
qDebug() << "Failed to start the process:" << process.errorString();
return;
} -
@Creat_ur_ownempire
I don't see why, assuming that is your actual code. Your message still says "Failed to start the process", but I presume it does now usewaitForFinished()
in place ofwaitForStarted()
?In any case, for this code try my earlier suggestion:
qDebug() << QProcess::execute("powershell.exe", arguments);
Does that work? It should not issue "Destroyed" message.
-
@JonB . I will try.