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. Run .bat file using QProcess in a Windows Service.
QtWS25 Last Chance

Run .bat file using QProcess in a Windows Service.

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 5 Posters 1.8k 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.
  • C Offline
    C Offline
    chaithubk
    wrote on 29 Jul 2019, 12:19 last edited by
    #1

    I am developing a Qt based Windows Process which is intended to execute a .bat file.

    // command to execute the batch script
    QString command = "myscript.bat " + "hello world";
    
    QProcess process;
    process.setProgram("cmd.exe");
    process.setArguments({ "/C", command });
    process.startDetached();
    

    This is the simple code I wrote to execute the batch script that takes a string as argument. I have created an Qt executable and the above program does the job perfectly.

    When I put the same piece of code inside a windows service, I see that the service started successfully but the batch script did not run. Is there any restricted privileges from the Windows that blocks the .bat file to be executed from the service ?

    Is there any other method that Qt supports which basically needs to execute a .bat file or set of cmd commands inside a windows service.

    J J 2 Replies Last reply 29 Jul 2019, 12:52
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 29 Jul 2019, 12:31 last edited by mrjj
      #2

      Hi
      I would hook up QProcess errors singals and see what it says about the situation.
      Might a rights issue. or it simply dont find the file.

      1 Reply Last reply
      4
      • C chaithubk
        29 Jul 2019, 12:19

        I am developing a Qt based Windows Process which is intended to execute a .bat file.

        // command to execute the batch script
        QString command = "myscript.bat " + "hello world";
        
        QProcess process;
        process.setProgram("cmd.exe");
        process.setArguments({ "/C", command });
        process.startDetached();
        

        This is the simple code I wrote to execute the batch script that takes a string as argument. I have created an Qt executable and the above program does the job perfectly.

        When I put the same piece of code inside a windows service, I see that the service started successfully but the batch script did not run. Is there any restricted privileges from the Windows that blocks the .bat file to be executed from the service ?

        Is there any other method that Qt supports which basically needs to execute a .bat file or set of cmd commands inside a windows service.

        J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 29 Jul 2019, 12:52 last edited by
        #3

        @chaithubk To add to @mrjj : all parameters (including myscript.bat " + "hello world) have to be strings in a QStringList.

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

        C 1 Reply Last reply 29 Jul 2019, 13:27
        1
        • J jsulm
          29 Jul 2019, 12:52

          @chaithubk To add to @mrjj : all parameters (including myscript.bat " + "hello world) have to be strings in a QStringList.

          C Offline
          C Offline
          chaithubk
          wrote on 29 Jul 2019, 13:27 last edited by
          #4

          @jsulm The same program when I built as an executable it works. If I use it inside a windows service worker method there is a problem.

          1 Reply Last reply
          0
          • H Offline
            H Offline
            hskoglund
            wrote on 29 Jul 2019, 19:17 last edited by
            #5

            Hi, perhaps the windows service is run as another user, like Administrator? Try specifying the complete path to your myscript.bat file.

            1 Reply Last reply
            4
            • C chaithubk
              29 Jul 2019, 12:19

              I am developing a Qt based Windows Process which is intended to execute a .bat file.

              // command to execute the batch script
              QString command = "myscript.bat " + "hello world";
              
              QProcess process;
              process.setProgram("cmd.exe");
              process.setArguments({ "/C", command });
              process.startDetached();
              

              This is the simple code I wrote to execute the batch script that takes a string as argument. I have created an Qt executable and the above program does the job perfectly.

              When I put the same piece of code inside a windows service, I see that the service started successfully but the batch script did not run. Is there any restricted privileges from the Windows that blocks the .bat file to be executed from the service ?

              Is there any other method that Qt supports which basically needs to execute a .bat file or set of cmd commands inside a windows service.

              J Offline
              J Offline
              JonB
              wrote on 30 Jul 2019, 10:16 last edited by
              #6

              @chaithubk
              Before you start worrying about a possible permission error, where do you expect the service to pick up your myscript.bat file from, compared to when you run it yourself? Chances are it's not on the PATH being used by the service nor its current directory. As @hskoglund says, first thing try full path to that file. And as @mrjj says, you really need to hook up stdout & stderr to see if anything reported. Plus, how do you know your batch script is not being run, given that running as a service/detached its output will not go anywhere, you had better not be relying on that to see anything.

              1 Reply Last reply
              2

              1/6

              29 Jul 2019, 12:19

              • Login

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