Why does QProcess::startDetached fail to open an.exe file when executing a.bat file in the start method of QtService?
-
.bat file
@echo off net session >nul 2>&1 if %errorLevel% NEQ 0 ( powershell -Command "Start-Process '%~f0' -Verb RunAs" exit /b ) start "" "C:\Program Files\CMake\bin\cmake-gui.exe"
c++ source code
class PixelStreamServiceTalk : public QtService<QCoreApplication> { public: PixelStreamServiceTalk(int argc, char** argv) : QtService<QCoreApplication>(argc, argv, "PixelStreamingCloudServer") { const QStringList arguments = QCoreApplication::arguments(); setServiceDescription("PixelStreamServiceTalk implemented with Qt"); setServiceFlags(QtServiceBase::CanBeSuspended); } protected: void start() override { QSettings settings(QApplication::applicationDirPath() + "/config.ini", QSettings::IniFormat); QString scriptPath = settings.value("config/ServiceStart").toString(); // QProcess::startDetached("cmd.exe", { "/c", "D:/workplace/UE_2025_QingYunDianTra/trunk/package/InstallUEPlatform/CloudTaskServer/StartServer.bat" }); QFile file("C:/temp/service_running.log"); file.open(QIODevice::WriteOnly | QIODevice::Append); QTextStream stream(&file); stream << QDateTime::currentDateTime().toString() << " - Service start.\n" << scriptPath.toLocal8Bit().toStdString().c_str(); file.close(); } };
-
.bat file
@echo off net session >nul 2>&1 if %errorLevel% NEQ 0 ( powershell -Command "Start-Process '%~f0' -Verb RunAs" exit /b ) start "" "C:\Program Files\CMake\bin\cmake-gui.exe"
c++ source code
class PixelStreamServiceTalk : public QtService<QCoreApplication> { public: PixelStreamServiceTalk(int argc, char** argv) : QtService<QCoreApplication>(argc, argv, "PixelStreamingCloudServer") { const QStringList arguments = QCoreApplication::arguments(); setServiceDescription("PixelStreamServiceTalk implemented with Qt"); setServiceFlags(QtServiceBase::CanBeSuspended); } protected: void start() override { QSettings settings(QApplication::applicationDirPath() + "/config.ini", QSettings::IniFormat); QString scriptPath = settings.value("config/ServiceStart").toString(); // QProcess::startDetached("cmd.exe", { "/c", "D:/workplace/UE_2025_QingYunDianTra/trunk/package/InstallUEPlatform/CloudTaskServer/StartServer.bat" }); QFile file("C:/temp/service_running.log"); file.open(QIODevice::WriteOnly | QIODevice::Append); QTextStream stream(&file); stream << QDateTime::currentDateTime().toString() << " - Service start.\n" << scriptPath.toLocal8Bit().toStdString().c_str(); file.close(); } };
@mirro
Why do you open a brand new topic when you have already asked this and received on-going responses in your https://forum.qt.io/topic/162508/why-does-qprocess-fail-to-execute-the.bat-command-to-open-.exe-in-qservice. Please don't --- it just wastes other people's time duplicating discussion. -
@mirro
Why do you open a brand new topic when you have already asked this and received on-going responses in your https://forum.qt.io/topic/162508/why-does-qprocess-fail-to-execute-the.bat-command-to-open-.exe-in-qservice. Please don't --- it just wastes other people's time duplicating discussion. -
@JonB The methods of execution are not quite the same.
.bat was executed, start "" "C:\Program Files\CMake\bin\cmake-gui.exe".
but.exe failed to work and the cpu usage was 0 -
@mirro What did you do so far to debug the issue?
Did you check whether the script was executed?
Did you try to add error checking to see what happens? -
@jsulm .bat was executed, start "" "C:\Program Files\CMake\bin\cmake-gui.exe".
but.exe failed to work and the cpu usage was 0 -
@JonB The methods of execution are not quite the same.
.bat was executed, start "" "C:\Program Files\CMake\bin\cmake-gui.exe".
but.exe failed to work and the cpu usage was 0@mirro said in Why does QProcess::startDetached fail to open an.exe file when executing a.bat file in the start method of QtService?:
@JonB The methods of execution are not quite the same.
I would have started by trying to get the other one to work, before you introduced
startDetached()
.If you address the suggestions in the other thread you might get somewhere.
As @jsulm says, are you expecting a Windows service to have access to the desktop to display a UI? How would that work?
If you think this is a
.bat
file issue, have you tried running the.exe
it runs directly fromQProcess
instead? -
@mirro said in Why does QProcess::startDetached fail to open an.exe file when executing a.bat file in the start method of QtService?:
@JonB The methods of execution are not quite the same.
I would have started by trying to get the other one to work, before you introduced
startDetached()
.If you address the suggestions in the other thread you might get somewhere.
As @jsulm says, are you expecting a Windows service to have access to the desktop to display a UI? How would that work?
If you think this is a
.bat
file issue, have you tried running the.exe
it runs directly fromQProcess
instead?@JonB Just tested that executing a.bat file with QProcess::startDetached in the button message of the GUI has no problem.
It's quite strange that QProcess::startDetached failed to execute a.bat file in the start method of QtService.
#include <QProcess> #include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); // connect(ui->pushButtonOpen, &QPushButton::clicked, this, &MainWindow::onButtonClickedOpen); // connect(ui->pushButtonClose, &QPushButton::clicked, this, &MainWindow::onButtonClickedClose); } MainWindow::~MainWindow() { delete ui; } void MainWindow::onButtonClickedOpen() { QProcess::startDetached("cmd.exe", { "/c", "D:/workplace/UE_2025_QingYunDianTra/trunk/package/InstallUEPlatform/CloudTaskServer/StartServer.bat" }); } void MainWindow::onButtonClickedClose() { QProcess::startDetached("cmd.exe", { "/c", "D:/workplace/UE_2025_QingYunDianTra/trunk/package/InstallUEPlatform/CloudTaskServer/StopServer.bat" }); }
-
@JonB Just tested that executing a.bat file with QProcess::startDetached in the button message of the GUI has no problem.
It's quite strange that QProcess::startDetached failed to execute a.bat file in the start method of QtService.
#include <QProcess> #include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); // connect(ui->pushButtonOpen, &QPushButton::clicked, this, &MainWindow::onButtonClickedOpen); // connect(ui->pushButtonClose, &QPushButton::clicked, this, &MainWindow::onButtonClickedClose); } MainWindow::~MainWindow() { delete ui; } void MainWindow::onButtonClickedOpen() { QProcess::startDetached("cmd.exe", { "/c", "D:/workplace/UE_2025_QingYunDianTra/trunk/package/InstallUEPlatform/CloudTaskServer/StartServer.bat" }); } void MainWindow::onButtonClickedClose() { QProcess::startDetached("cmd.exe", { "/c", "D:/workplace/UE_2025_QingYunDianTra/trunk/package/InstallUEPlatform/CloudTaskServer/StopServer.bat" }); }
@mirro
You just keep repeating the code you are using, with apparently no attempt to follow any of the suggestions I (and others) have given you. So I have no more to say until you do so, up to you, you are supposed to make some effort to find out what is going on and what the problem might be. (And as also stated previously you should getstart()
working correctly before you migrate tostartDetached()
.) -
@mirro
You just keep repeating the code you are using, with apparently no attempt to follow any of the suggestions I (and others) have given you. So I have no more to say until you do so, up to you, you are supposed to make some effort to find out what is going on and what the problem might be. (And as also stated previously you should getstart()
working correctly before you migrate tostartDetached()
.) -
@JonB The test found that the.bat window could not pop up normally in the start method of QtService
@echo off net session >nul 2>&1 if %errorLevel% NEQ 0 ( powershell -Command "Start-Process '%~f0' -Verb RunAs" exit /b ) PAUSE
@mirro said in Why does QProcess::startDetached fail to open an.exe file when executing a.bat file in the start method of QtService?:
The test found that the.bat window could not pop up normally in the start method of QtService
This is what @jsulm already told you: https://forum.qt.io/topic/162519/why-does-qprocess-startdetached-fail-to-open-an.exe-file-when-executing-a.bat-file-in-the-start-method-of-qtservice/6?_=1751004577647
A service is a background program which also runs when the user is not logged in so how should it open any gui?