Run .bat file using QProcess in a Windows Service.
-
wrote on 29 Jul 2019, 12:19 last edited by
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.
-
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. -
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.
@chaithubk To add to @mrjj : all parameters (including myscript.bat " + "hello world) have to be strings in a QStringList.
-
@chaithubk To add to @mrjj : all parameters (including myscript.bat " + "hello world) have to be strings in a QStringList.
-
wrote on 29 Jul 2019, 19:17 last edited by
Hi, perhaps the windows service is run as another user, like Administrator? Try specifying the complete path to your myscript.bat file.
-
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.
wrote on 30 Jul 2019, 10:16 last edited by@chaithubk
Before you start worrying about a possible permission error, where do you expect the service to pick up yourmyscript.bat
file from, compared to when you run it yourself? Chances are it's not on thePATH
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/6