/// <summary>
 /// 改变服务启动类型 [手动或自动]
 /// </summary>
 /// <param name="serviceName">服务名称</param>
 /// <param name="isAutoStart">是否自动启动</param>
 /// <returns>是否成功</returns>
 public bool ChangeServiceStartType(string serviceName, NativeInstaller.ServiceStartType startType)
 {
     IntPtr hSCManager = IntPtr.Zero;
     IntPtr hSCManagerLock = IntPtr.Zero;
     IntPtr hService = IntPtr.Zero;
     bool result = true;
     try
     {
         hSCManager = NativeInstaller.OpenSCManager(null, null, NativeInstaller.ServiceControlManagerType.SC_MANAGER_ALL_ACCESS);
         if (hSCManager == IntPtr.Zero) throw new Win32Exception(Marshal.GetLastWin32Error(), "不能打开服务控制台!");
         hSCManagerLock = NativeInstaller.LockServiceDatabase(hSCManager);
         if (hSCManagerLock == IntPtr.Zero) throw new Win32Exception(Marshal.GetLastWin32Error(), "无法锁定服务控制台");
         hService = NativeInstaller.OpenService(hSCManager, serviceName, NativeInstaller.ACCESS_TYPE.SERVICE_ALL_ACCESS);
         if (hService == IntPtr.Zero) throw new ApplicationException("不能打开指定服务");
         result = NativeInstaller.ChangeServiceConfig(hService, NativeInstaller.ServiceType.SERVICE_WIN32_OWN_PROCESS, startType, -1, null, null, 0, null, null, null, null);
         if (!result) throw new Win32Exception(Marshal.GetLastWin32Error(), "无法修改服务配置");
     }
     catch (Exception objExp)
     {
         Console.Error.WriteLine(objExp.Message);
         result = false;
     }
     finally
     {
         if (hService != IntPtr.Zero)
         {
             NativeInstaller.CloseServiceHandle(hService);
             hService = IntPtr.Zero;
         }
         if (hSCManagerLock != IntPtr.Zero)
         {
             NativeInstaller.UnlockServiceDatabase(hSCManagerLock);
             hSCManagerLock = IntPtr.Zero;
         }
         if (hSCManager != IntPtr.Zero)
         {
             NativeInstaller.CloseServiceHandle(hSCManager);
             hSCManager = IntPtr.Zero;
         }
     }
     return result;
 }
 /// <summary>
 /// 安装服务
 /// </summary>
 /// <param name="servicePath">服务路径</param>
 /// <param name="serviceName">服务名称</param>
 /// <param name="serviceDispName">服务友好名称</param>
 /// <param name="serviceDescription">描述</param>
 /// <param name="isAutoStart">是否自动启动</param>
 /// <returns>是否成功</returns>
 public bool InstallService(string servicePath, string serviceName, string serviceDispName, string serviceDescription, NativeInstaller.ServiceStartType startType)
 {
     IntPtr hSCManager = IntPtr.Zero;
     IntPtr hSCManagerLock = IntPtr.Zero;
     IntPtr hService = IntPtr.Zero;
     bool result = true;
     try
     {
         Console.ForegroundColor = ConsoleColor.Yellow;
         hSCManager = NativeInstaller.OpenSCManager(null, null, NativeInstaller.ServiceControlManagerType.SC_MANAGER_ALL_ACCESS);
         if (hSCManager == IntPtr.Zero) throw new Win32Exception(Marshal.GetLastWin32Error(), "不能打开服务控制台!");
         hSCManagerLock = NativeInstaller.LockServiceDatabase(hSCManager);
         if (hSCManagerLock == IntPtr.Zero) throw new Win32Exception(Marshal.GetLastWin32Error(), "无法锁定服务控制台");
         hService = NativeInstaller.CreateService(hSCManager, serviceName, serviceDispName, NativeInstaller.ServiceControlManagerType.SC_MANAGER_ALL_ACCESS, NativeInstaller.ServiceType.SERVICE_WIN32_OWN_PROCESS, startType, NativeInstaller.ServiceErrorControl.SERVICE_ERROR_NORMAL, servicePath, null, 0, null, null, null);
         if (hService == IntPtr.Zero) throw new Win32Exception(Marshal.GetLastWin32Error(), "创建服务失败!");
         NativeInstaller.SERVICE_DESCRIPTION description = new NativeInstaller.SERVICE_DESCRIPTION
         {
             lpDescription = serviceDescription
         };
         result = NativeInstaller.ChangeServiceConfig2(hService, NativeInstaller.InfoLevel.SERVICE_CONFIG_DESCRIPTION, ref description);
         if (!result) throw new Win32Exception(Marshal.GetLastWin32Error(), "无法修改服务描述");
         NativeInstaller.SERVICE_FAILURE_ACTIONS failureActions = new NativeInstaller.SERVICE_FAILURE_ACTIONS
         {
             dwResetPeriod = 600,
             lpRebootMsg = "服务启动失败! 正在重启中...",
             lpCommand = ""
         };
         NativeInstaller.SC_ACTION[] actions = new NativeInstaller.SC_ACTION[3];
         failureActions.cActions = actions.Length;
         actions[0].Delay = 30000;
         actions[0].SCActionType = NativeInstaller.SC_ACTION_TYPE.SC_ACTION_RESTART;
         actions[1].Delay = 30000;
         actions[1].SCActionType = NativeInstaller.SC_ACTION_TYPE.SC_ACTION_RESTART;
         actions[2].Delay = 30000;
         actions[2].SCActionType = NativeInstaller.SC_ACTION_TYPE.SC_ACTION_NONE;
         IntPtr ptrScActions = new IntPtr();
         ptrScActions = Marshal.AllocHGlobal((int)(Marshal.SizeOf(new NativeInstaller.SC_ACTION()) * 3));
         NativeInstaller.CopyMemory(ptrScActions, actions, Marshal.SizeOf(new NativeInstaller.SC_ACTION()) * 3);
         failureActions.lpsaActions = ptrScActions.ToInt32();
         result = NativeInstaller.ChangeServiceConfig2(hService, NativeInstaller.InfoLevel.SERVICE_CONFIG_FAILURE_ACTIONS, ref failureActions);
         if (!result) throw new Win32Exception(Marshal.GetLastWin32Error(), "无法设置服务的故障恢复模式");
     }
     catch (Exception objExp)
     {
         Console.Error.WriteLine(objExp.Message);
         result = false;
     }
     finally
     {
         if (hService != IntPtr.Zero)
         {
             NativeInstaller.CloseServiceHandle(hService);
             hService = IntPtr.Zero;
         }
         if (hSCManagerLock != IntPtr.Zero)
         {
             NativeInstaller.UnlockServiceDatabase(hSCManagerLock);
             hSCManagerLock = IntPtr.Zero;
         }
         if (hSCManager != IntPtr.Zero)
         {
             NativeInstaller.CloseServiceHandle(hSCManager);
             hSCManager = IntPtr.Zero;
         }
         Console.ResetColor();
     }
     return result;
 }