tags:

views:

112

answers:

3

Using the method described in the MSDN for registering a Windows Service (ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.WIN32COM.v10.en/dllproc/base/createservice.htm) and using similar code to the supplied example:

schService = CreateService( 
    schSCManager,              // SCManager database 
    TEXT("Sample_Srv"),        // name of service 
    lpszDisplayName,           // service name to display 
    SERVICE_ALL_ACCESS,        // desired access 
    SERVICE_WIN32_OWN_PROCESS, // service type 
    SERVICE_DEMAND_START,      // start type 
    SERVICE_ERROR_NORMAL,      // error control type 
    szPath,                    // path to service's binary 
    NULL,                      // no load ordering group 
    NULL,                      // no tag identifier 
    NULL,                      // no dependencies 
    NULL,                      // LocalSystem account 
    NULL);                     // no password 

My issue is that, although the service is registered and works perfectly, in msconfig.msc the service has 'Take No Action' in the recovery options. Is there a way I can programatically change this so that upon failure it restarts?

A: 

You might be able to set this using the sc command.

sc failure "servicename" reset= 0 actions= restart/30000////

This will tell it to reset the failure counter after 0 days (never), and restart after 30 seconds on the first failure with no action for the second and later failures.

Bing
+1  A: 

Take a look at ChangeServiceConfig2 for setting those types of service options.

Dana Holt
A: 

Performed further digging in the MSDN - it wasn't particularly easy to find but it appears

ChangeServiceConfig2 (http://msdn.microsoft.com/en-us/library/ms681988(VS.85).aspx)

BOOL WINAPI ChangeServiceConfig2(
  __in      SC_HANDLE hService,
  __in      DWORD dwInfoLevel,
  __in_opt  LPVOID lpInfo
);

When param dwInfoLevel is SERVICE_CONFIG_FAILURE_ACTIONS (2) then the lpInfo parameter is a pointer to a SERVICE_FAILURE_ACTIONS structure.

SERVICE_FAILURE_ACTIONS Structure http://msdn.microsoft.com/en-us/library/ms685939(VS.85).aspx

Where you can configure the 'optional' service settings as you wish.

Konrad