Why does QProcess fail to execute the.bat command to open .exe in QService?
-
Start_UE.ps1 file
Start-Process -FilePath D:/workplace/UE_2025_QingYunDianTra/trunk/package/InstallUEPlatform/package/WindowsNoEditor/DTSDK/Binaries/Win64/DTSDK-Win64-Shipping.exe
StartServer.bat file
@echo off setlocal net session >nul 2>&1 if %errorlevel% neq 0 ( powershell -Command "Start-Process -FilePath '%~f0' -Verb RunAs" exit /b ) echo Running as Administrator... echo Starting UE... powershell -NoProfile -ExecutionPolicy Bypass -File "%~dp0Start_UE.ps1" endlocal
qtservice 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() { QSettings settings(QApplication::applicationDirPath() + "/config.ini", QSettings::IniFormat); QString scriptPath = settings.value("config/ServiceStart").toString(); // QProcess process; process.setProgram("cmd.exe"); process.setArguments({ "/c", "D:/workplace/UE_2025_QingYunDianTra/trunk/package/InstallUEPlatform/CloudTaskServer/StartServer.bat" }); process.start(); process.waitForFinished(-1); // 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(); } void stop() { QSettings settings(QApplication::applicationDirPath() + "/config.ini", QSettings::IniFormat); QString scriptPath = settings.value("config/ServiceStop").toString(); // QProcess process; process.setProgram("cmd.exe"); process.setArguments({ "/c", "D:/workplace/UE_2025_QingYunDianTra/trunk/package/InstallUEPlatform/CloudTaskServer/StopServer.bat" }); process.start(); process.waitForFinished(-1); // QFile file("C:/temp/service_running.log"); file.open(QIODevice::WriteOnly | QIODevice::Append); QTextStream stream(&file); stream << QDateTime::currentDateTime().toString() << " - Service stop.\n" << scriptPath.toLocal8Bit().toStdString().c_str(); file.close(); } private: };
-
Start_UE.ps1 file
Start-Process -FilePath D:/workplace/UE_2025_QingYunDianTra/trunk/package/InstallUEPlatform/package/WindowsNoEditor/DTSDK/Binaries/Win64/DTSDK-Win64-Shipping.exe
StartServer.bat file
@echo off setlocal net session >nul 2>&1 if %errorlevel% neq 0 ( powershell -Command "Start-Process -FilePath '%~f0' -Verb RunAs" exit /b ) echo Running as Administrator... echo Starting UE... powershell -NoProfile -ExecutionPolicy Bypass -File "%~dp0Start_UE.ps1" endlocal
qtservice 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() { QSettings settings(QApplication::applicationDirPath() + "/config.ini", QSettings::IniFormat); QString scriptPath = settings.value("config/ServiceStart").toString(); // QProcess process; process.setProgram("cmd.exe"); process.setArguments({ "/c", "D:/workplace/UE_2025_QingYunDianTra/trunk/package/InstallUEPlatform/CloudTaskServer/StartServer.bat" }); process.start(); process.waitForFinished(-1); // 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(); } void stop() { QSettings settings(QApplication::applicationDirPath() + "/config.ini", QSettings::IniFormat); QString scriptPath = settings.value("config/ServiceStop").toString(); // QProcess process; process.setProgram("cmd.exe"); process.setArguments({ "/c", "D:/workplace/UE_2025_QingYunDianTra/trunk/package/InstallUEPlatform/CloudTaskServer/StopServer.bat" }); process.start(); process.waitForFinished(-1); // QFile file("C:/temp/service_running.log"); file.open(QIODevice::WriteOnly | QIODevice::Append); QTextStream stream(&file); stream << QDateTime::currentDateTime().toString() << " - Service stop.\n" << scriptPath.toLocal8Bit().toStdString().c_str(); file.close(); } private: };
- How do you know that
cmd /c ...
definitely accepts a Windows path with forward rather then native backslashes in it? - Have you tried the exact
cmd /c D:/workplace/...
from a Command Prompt? - Have you verified that the command exits so that your
waitForFinished()
will actually return? - You should put in all error checking for your
QProcess
, including signals likestarted()
,finished()
&errorOccurred()
, as well as reading back any and all output which might be being sent to either stdout or stderr with the appropriateQProcess
methods, as this is what Windows does at least sometimes. - You might check the Windows Event Log for any messages about this service start attempt.
- How do you know that
-
- How do you know that
cmd /c ...
definitely accepts a Windows path with forward rather then native backslashes in it? - Have you tried the exact
cmd /c D:/workplace/...
from a Command Prompt? - Have you verified that the command exits so that your
waitForFinished()
will actually return? - You should put in all error checking for your
QProcess
, including signals likestarted()
,finished()
&errorOccurred()
, as well as reading back any and all output which might be being sent to either stdout or stderr with the appropriateQProcess
methods, as this is what Windows does at least sometimes. - You might check the Windows Event Log for any messages about this service start attempt.
- How do you know that
-
@JonB
The QProcess test will fail when executing .bat file in the service start method of QService,but The QProcess test will succeed when executing .bat file in the Main function.@mirro Please post code as text, not pictures.
In your code you're not waiting after calling exec() and you did not add any error handling as @JonB suggested. And did you check Windows logs as it was also suggested?
You should use startDetached instead of execute if you're not waiting for the process to finish. command variable should be a QStringList, not QString. -
-
J JonB referenced this topic