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 fail to execute the.bat command to open .exe in QService?
Forum Updated to NodeBB v4.3 + New Features

Why does QProcess fail to execute the.bat command to open .exe in QService?

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 63 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 Offline
    M Offline
    mirro
    wrote last edited by mirro
    #1

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

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

      @mirro

      • 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 like started(), finished() & errorOccurred(), as well as reading back any and all output which might be being sent to either stdout or stderr with the appropriate QProcess 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.
      M 1 Reply Last reply
      2
      • JonBJ JonB

        @mirro

        • 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 like started(), finished() & errorOccurred(), as well as reading back any and all output which might be being sent to either stdout or stderr with the appropriate QProcess 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.
        M Offline
        M Offline
        mirro
        wrote last edited by mirro
        #3

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

        4ECC3024-2F64-4463-89EF-0274EB8C86A6.png

        jsulmJ 1 Reply Last reply
        0
        • M mirro

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

          4ECC3024-2F64-4463-89EF-0274EB8C86A6.png

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

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

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

          1 Reply Last reply
          3
          • M mirro has marked this topic as solved
          • JonBJ JonB referenced this topic

          • Login

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