Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Why does QProcess::startDetached fail to open an.exe file when executing a.bat file in the start method of QtService?
Forum Updated to NodeBB v4.3 + New Features

Why does QProcess::startDetached fail to open an.exe file when executing a.bat file in the start method of QtService?

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 4 Posters 127 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M mirro

    alt text

    .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();
        }
    };
    
    JonBJ Offline
    JonBJ Offline
    JonB
    wrote last edited by
    #2

    @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.

    M 1 Reply Last reply
    2
    • JonBJ JonB

      @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.

      M Offline
      M Offline
      mirro
      wrote last edited by mirro
      #3

      @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

      alt text

      jsulmJ JonBJ 2 Replies Last reply
      0
      • M mirro

        @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

        alt text

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote last edited by
        #4

        @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?

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        M 1 Reply Last reply
        0
        • jsulmJ jsulm

          @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?

          M Offline
          M Offline
          mirro
          wrote last edited by mirro
          #5

          @jsulm .bat was executed, start "" "C:\Program Files\CMake\bin\cmake-gui.exe".
          but.exe failed to work and the cpu usage was 0

          alt text

          jsulmJ 1 Reply Last reply
          0
          • M mirro

            @jsulm .bat was executed, start "" "C:\Program Files\CMake\bin\cmake-gui.exe".
            but.exe failed to work and the cpu usage was 0

            alt text

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote last edited by
            #6

            @mirro I'm not sure services can actually start GUI applications

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            M 1 Reply Last reply
            2
            • M mirro

              @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

              alt text

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote last edited by
              #7

              @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 from QProcess instead?

              M 1 Reply Last reply
              1
              • JonBJ JonB

                @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 from QProcess instead?

                M Offline
                M Offline
                mirro
                wrote last edited by
                #8

                @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" });
                }
                
                JonBJ 1 Reply Last reply
                0
                • M mirro

                  @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" });
                  }
                  
                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote last edited by JonB
                  #9

                  @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 get start() working correctly before you migrate to startDetached().)

                  M 1 Reply Last reply
                  1
                  • JonBJ JonB

                    @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 get start() working correctly before you migrate to startDetached().)

                    M Offline
                    M Offline
                    mirro
                    wrote last edited by mirro
                    #10

                    @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
                    
                    Christian EhrlicherC 1 Reply Last reply
                    0
                    • M mirro

                      @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
                      
                      Christian EhrlicherC Offline
                      Christian EhrlicherC Offline
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote last edited by
                      #11

                      @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?

                      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                      Visit the Qt Academy at https://academy.qt.io/catalog

                      1 Reply Last reply
                      1
                      • jsulmJ jsulm

                        @mirro I'm not sure services can actually start GUI applications

                        M Offline
                        M Offline
                        mirro
                        wrote last edited by
                        #12

                        @jsulm After testing, the QService is unable to open other process programs

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved