Set the recovery property of Windows service (CSWindowsServiceRecoveryProperty)

The code sample demonstrates setting the recovery properties of Windows Service

C# (73.8 KB)
12,027 times
Add to favorites
E-mail Twitter Digg Facebook
  CONSOLE APPLICATION : CSWindowsServiceRecoveryProperty Project Overview


CSWindowsServiceRecoveryProperty example demonstrates how to use 
ChangeServiceConfig2 to configure the service "Recovery" properties in C#. 
This example operates all the options you can see on the service "Recovery" 
tab, including setting the "Enable actions for stops with errors" option in 
Windows Vista and later operating systems. This example also include how to 
grant the shut down privilege to the process, so that we can configure a 
special option in the "Recovery" tab - "Restart Computer Options...".


1.  The code sample must run on Windows Vista and later operating systems. 
2.  The code sample need to run as administrator.
3.  You need to install the CSWindowsService sample service before you run 
    this code sample. 


1.  Install CSWindowsService service on your system.
2.  Build this project and run it as administrator.
3.  Use services.msc command to view the services on your system and find the 
    CSWindowsService service, and then you can double click on that service 
    to open the property window.
4.  In the Recovery tab, you can find the recovery properties had been 
    configured by the sample application.

Code Logic:

1.  Open the service control manager with a high permission.
2.  Open the service with the service name and a high permission.
3.	Create three actions for the service control manager perform at these 
    three situations "First failure", "Second failure" and "Subsequent 
4.  Grant shut down privilege to the process, so that we can configure the 
    "Restart Computer Options...".
    a.  Retrieve a pseudo handle for the current process.
    b.  Open the access token associated with the process.
    c.  Retrieve the locally unique identifier (LUID) used on a specified 
        system to locally represent the specified privilege name.
    d.  Enable privileges in the specified access token.
5.  Construct a service failure actions struct with the above actions, the 
    time after which to reset the failure count to zero if there are no 
    failures, in seconds, reboot message, the program with or without the 
    command line parameters will execute with we set the failure action to 
    Run_Command after the service failed and the fail count.
6.  Enable actions for stops with errors.
7.  Call the ChangeServiceConfig2 method to set the service recovery 



Service Security and Access Rights

Privilege Constants

System Error Codes (0-499)


SC_ACTION Structure


OpenSCManager Function

OpenService Function

ChangeServiceConfig2 Function

DllImportAttribute Class


GetCurrentProcess Function

OpenProcessToken Function

LookupPrivilegeValue Function

AdjustTokenPrivileges Function

Set up Recovery Actions to Take Place When a Service Fails