How to start service programmatically ?

  • Thread starter Thread starter Polaris
  • Start date Start date
P

Polaris

Hi:

I wrote a service program on XP. The service can be started manually in the
Control Panel - Service program, but I could not start it programmatically;
call to StartService() returns Error message saying could not connect to the
Service Control Manager.

Appreciate any hits from you!

Thanks in Advance
Polaris
 
Could you post little Code snippet ,describing extactly how are using
StartService(...) api?
Have you tried taking valid HANDLE form OpenSCManager(...) with all
access.


--

With Regards
Alok Gupta
Visit me at http://alok.bizhat.com

"I Believe this will Help"
 
Hi Thanks for your info. Below is the code I tried. For easier reading, I
have remved error checkings. The CreateService is successful, the service
shows up in the Control Panel - Services program and can be started there
too. But the StartService (hService) did not work. The status shown in the
Control Panel - Services program is always "Starting" and just hang there
and never finish. As you can see, I did use the SERVICE_ALL_ACCESS in
CreateService.

Please let me know if you can see something wrong here.

Thanks!

Polaris

--------------------------------------------------------------------------------------------------------------------------------------------------------------------

SC_HANDLE hscm = OpenSCManager (NULL, SERVICES_ACTIVE_DATABASE,
SC_MANAGER_CREATE_SERVICE);

if (hscm)
{
SC_HANDLE hService = CreateService (hscm, g_szServiceName,
g_szDisplayName, SERVICE_ALL_ACCESS,
SERVICE_WIN32_OWN_PROCESS |
SERVICE_INTERACTIVE_PROCESS,
SERVICE_AUTO_START, SERVICE_ERROR_IGNORE,
path,
0, 0, 0, 0, 0);

StartService (hService);

if (hService)
CloseServiceHandle (hService);

CloseServiceHandle (hscm);
}
 
Hi:
I tried the code again and it worked! I do not know why, the only thing I
can think of is that I rebooted the machine and that might re-set the
service control manager.

Thanks
Polaris
 
Are you creating service every time before starting!!!!!, better use
OpenService(...) api to get valid handle of service then pass it on
StartService(...) api.


--

With Regards
Alok Gupta
Visit me at http://alok.bizhat.com

"I Believe this will Help"
 
Back
Top