public struct SC_ACTION
{
public int Type;
public int Delay;
}
public struct SERVICE_FAILURE_ACTIONS
{
public int dwResetPeriod;
public string lpRebootMsg;
public string lpCommand;
public int cActions;
public IntPtr lpsaActions;
}
static void ChangeFailureActions(string ServiceName)
{
IntPtr scHandle = IntPtr.Zero;
IntPtr svcHandle = IntPtr.Zero;
string serviceName = ServiceName;
try
{
scHandle = OpenSCManager(null, null, (uint)0xF003F);
if (scHandle == IntPtr.Zero)
{
throw new Exception(String.Format("Error connecting to Service Control Manager. Error provided was: 0x{0:X}", Marshal.GetLastWin32Error()));
}
svcHandle = OpenService(scHandle, serviceName, (int)0xF01FF);
if (svcHandle == IntPtr.Zero)
{
throw new Exception(String.Format("Error opening service for modifying. Error returned was: 0x{0:X}", Marshal.GetLastWin32Error()));
}
IntPtr lpsaActions = Marshal.AllocHGlobal(Marshal.SizeOf(action)*2);
if (lpsaActions == IntPtr.Zero)
{
throw new Exception(String.Format("Unable to allocate memory for service action, error was: 0x{0:X}", Marshal.GetLastWin32Error()));
}