[solved] How to create a service with Qt and sc.exe under windows 7
-
Hi,
I have made a service and I want to create it with sc.exe and Qt5 under windows 7.the current user is an administrator account.
I have tried :QStringList l; l.append("create"); l.append("Service01"); l.append("binPath="); l.append("\"C:/Program Files (x86)/programme01/service01.exe\""); pc.setProcessEnvironment(QProcessEnvironment::systemEnvironment()); pc.start("c:/windows/system32/sc.exe",l); ``` and it doesn't create the service in the list of services. has someone a good idea?
-
the first line of code is
QProcess pc;
-
@stephane78
It is more interesting what is happening after pc.start()
Do you do a waitForFinished?
Is there end of the method and your pc is deleted immediately? -
@koahnig
Hi, no I don't do a waitForFinished and yes it is the end of the method so pc is deleted. -
you are right I will try with waitForFinished....
-
@koahnig
Hi, waitForFinished() returns true but the service isn't still added in the list of services. -
@stephane78
I do not have enough experience with services.I assume that you have checked the syntax of your command externally and it works fine from command line. Therefore, the only I can recommend is to start your application from outside of Qt creator environment. If you have checked this already and it does not work either, I am of no help here.
-
@koahnig
HI, I change the code and used QProcess::execute and it didn't add the service and after that I have found a solution: if I execute my program "as administrator" in Windows then it add my service to the list of service and it is ok so perhaps there is another solution with win32 functions instead of QProcess that runs an external program as administrator.... -
@stephane78
So it was basically a missing admin rights issue.
AFAIK there is no other way than QProcess. Also it would be quite dangerous to be able to plant some applications with admin rights from an application with no admin rights. Any decent virus scan prevent this anyhow. -
@koahnig
it is out of subject on this forum, but I tested with the win32 function Shellexecute and "runas" as second parameter instead of qprocess and it created my service.so I mark this subject solved.thanks. -
This post is deleted!