public static extern int LwSmQueryServiceStatus(IntPtr phHandle, out ServiceManagerApi.LW_SERVICE_STATUS pStatus);
 public static extern bool ControlService(IntPtr hService, ServiceManagerApi.SERVICE_CONTROL dwControl, ref IntPtr lpServiceStatus);
 public static extern bool EnumServicesStatus(IntPtr hSCManager,
                     IntPtr dwServiceType,
                     ServiceManagerApi.SERVICE_STATE dwServiceState,
                     IntPtr lpServices,
                     int cbBufSize,
                     ref int pcbBytesNeeded,
                     ref int lpServicesReturned,
                     ref int lpResumeHandle);
        public static bool ApiChangeServiceConfig2(string sServicename,
                                           ServiceManagerApi.SERVICE_FAILURE_ACTIONS failureActions)
        {
            try
            {
                ApiOpenService(sServicename, ServiceManagerInteropWindows.SERVICE_CHANGE_CONFIG);

                IntPtr lpInfo = Marshal.AllocHGlobal(Marshal.SizeOf(failureActions));
                if (lpInfo == IntPtr.Zero)
                {
                    throw new Exception(String.Format("Unable to allocate memory, error was: 0x{0:X}", Marshal.GetLastWin32Error()));
                }

                Marshal.StructureToPtr(failureActions, lpInfo, false);

                if (!ServiceManagerInteropWindows.ChangeServiceConfig2(phService, (int)ServiceManagerInteropWindows.SERVICE_CONFIG_FAILURE_ACTIONS, lpInfo))
                {
                    Marshal.FreeHGlobal(lpInfo);
                    Logger.Log(String.Format("Error setting service config, error was: 0x{0:X}", Marshal.GetLastWin32Error()));
                    return false;
                }

                Marshal.FreeHGlobal(lpInfo);

                Logger.Log("MyService: Service modification completed");

                ApiCloseServiceHandle(phService);
            }
            catch (Exception ex)
            {
                Logger.LogException("ApiChangeServiceConfig2()", ex);
                return false;
            }

            return true;
        }