I am making a window service in VS2010 using c++ and want to know is there any way to restart the service after a particular time,actually i will let user to write restart time in an INI file and service should be able to restart at that time.any sample code or any api available???
Asked
Active
Viewed 1,565 times
1
-
Is there a specific reason you want the service to be restarted regularly? If you have problems like memory leaks I suggest you fix those instead. – Some programmer dude Jun 08 '12 at 07:19
-
@JoachimPileborg Sir its mine project requirement that i have to restart the service after the time that user have written in INI file – user1402643 Jun 08 '12 at 07:22
-
See this SO question for restarting from the service itself http://stackoverflow.com/questions/220382/how-can-a-windows-service-programmatically-restart-itself – Carl Winder Jun 08 '12 at 09:32
-
Listening to the administrator's requests posted to you by ControlService() is voluntary. You can ignore him and run your service code any way you want. Minus pleasing the admin of course, he'll uninstall your service if you tick him off enough. – Hans Passant Jun 08 '12 at 11:03
3 Answers
3
You can use load and execute new child process using _execl.
Here is a simple code illustrating how I did open a Notepad++ instance:
#include <process.h>
int main(){
_execl("C:\\Program Files\\Notepad++\\Notepad++.exe", "\\0");
return 0;
}
This way you can have another process that will actually stop your current process and start your process again.

Ashwin
- 1,942
- 4
- 30
- 59
1
Add a scheduled task to call a batch file that does
net stop servicename
net start servicename

kenny
- 21,522
- 8
- 49
- 87
-
sir i do start and stop the service from service.msc...is there no other way by which we can restart the service progmmatically... – user1402643 Jun 08 '12 at 07:19
-
@user1402643 see Superman's response, you can also use the API. All of what I suggested can be done from an API, but the OS allows you to do it 'programmatically'. – kenny Jun 08 '12 at 07:23
-
If the `net stop` exits with a timeout and the service state is still "stopping" when the `net start` starts, you don't restart the service. – Jun 08 '12 at 07:57
0
The StartService API can start a windows service and the ControlService API can stop a windows service.

Superman
- 3,027
- 1
- 15
- 10
-
Can this really be used here? The service is already running and I think windows would prevent it from running twice. And if the service stops itself it cannot give the command to start anymore. – Wolfgang Jun 08 '12 at 07:33
-
1You cannot start another instance. But you can stop it first and then start it. These APIs are to be used from an external program and not from the service itself. – Superman Jun 08 '12 at 08:17
-
Can you call it after the service returns from StartServiceCtrlDispatcher? At that point the process still exists obviously, but from SCM's perspective the service is no longer running. I am too lazy to try myself, but I'd be curious to know the result. – Luke Jun 08 '12 at 15:15