/// <summary>
 /// Change service properties.
 /// </summary>
 /// <param name="serviceName">Service name.</param>
 /// <param name="displayName">Service display name.</param>
 /// <param name="bootFlag">Тип загрузки.</param>
 /// <param name="binaryPathName">Путь к исполняемому файлу.</param>
 /// <param name="userName">Имя пользователя.</param>
 /// <param name="userPassword">Пароль пользователя.</param>
 public static void ChangeServiceProperties(
     string serviceName,
     string displayName,
     ServiceBootFlag bootFlag,
     string binaryPathName,
     string userName,
     string userPassword)
 {
     using (
         var serviceObject = GetServiceObject(
             serviceName,
             ScmAccessRights.AllAccess,
             ServiceAccessRights.AllAccess))
     {
         ChangeServiceConfig(
             serviceObject.Service,
             ServiceType.NoChanges,
             bootFlag,
             ServiceError.NoChanges,
             binaryPathName,
             null,
             0,
             null,
             userName,
             userPassword,
             displayName);
     }
 }
Exemple #2
0
    public static void ChangeStartMode(string serviceName, ServiceBootFlag mode)
    {
        IntPtr scm = OpenSCManager(ScmAccessRights.Connect | ScmAccessRights.EnumerateService);

        try
        {
            IntPtr service = OpenService(scm, serviceName, ServiceAccessRights.QueryConfig | ServiceAccessRights.ChangeConfig);
            if (service == IntPtr.Zero)
            {
                throw new ApplicationException("Could not open service.");
            }

            try
            {
                if (!ChangeServiceConfig(service, SERVICE_NO_CHANGE, mode, SERVICE_NO_CHANGE, null, null, IntPtr.Zero, null, null, null, null))
                {
                    throw new ApplicationException("Could not configure service " + Marshal.GetLastWin32Error());
                }
            }
            finally
            {
                CloseServiceHandle(service);
            }
        }
        finally
        {
            CloseServiceHandle(scm);
        }
    }
 private static extern IntPtr CreateService(IntPtr hSCManager, string
                                            lpServiceName, string lpDisplayName,
                                            ServiceRights dwDesiredAccess, int
                                            dwServiceType,
                                            ServiceBootFlag dwStartType, ServiceError dwErrorControl,
                                            string lpBinaryPathName, string lpLoadOrderGroup, IntPtr lpdwTagId,
                                            string
                                            lpDependencies, string lp, string lpPassword);
Exemple #4
0
 public static extern bool ChangeServiceConfig(
     IntPtr hService,
     ServiceType nServiceType,
     ServiceBootFlag nStartType,
     ServiceError nErrorControl,
     string lpBinaryPathName,
     string lpLoadOrderGroup,
     int lpdwTagId,
     string lpDependencies,
     string lpServiceStartName,
     string lpPassword,
     string lpDisplayName);
 private static extern IntPtr CreateService(
     IntPtr hSCManager,
     string lpServiceName,
     string lpDisplayName,
     ServiceAccessRights dwDesiredAccess,
     int dwServiceType,
     ServiceBootFlag dwStartType,
     ServiceError dwErrorControl,
     string lpBinaryPathName,
     string lpLoadOrderGroup,
     IntPtr lpdwTagId,
     string lpDependencies,
     string lp,
     string lpPassword);
Exemple #6
0
        public static void InstallAndStart(string serviceName, string displayName, string fileName, string startType, string desc)
        {
            IntPtr scm = OpenSCManager(ScmAccessRights.AllAccess);

            try
            {
                IntPtr          service       = OpenService(scm, serviceName, ServiceAccessRights.AllAccess);
                ServiceBootFlag startTypeFlag = ServiceBootFlag.AutoStart;
                if (!string.IsNullOrEmpty(startType))
                {
                    if (!startType.ToLower().Equals("auto"))
                    {
                        startTypeFlag = ServiceBootFlag.DemandStart;
                    }
                }
                if (service == IntPtr.Zero)
                {
                    service = CreateService(scm, serviceName, displayName, ServiceAccessRights.AllAccess, SERVICE_WIN32_OWN_PROCESS, startTypeFlag, ServiceError.Normal, fileName, null, IntPtr.Zero, null, null, null);
                }

                if (service == IntPtr.Zero)
                {
                    throw new ApplicationException("Failed to install service.");
                }

                if (!string.IsNullOrEmpty(desc))
                {
                    var serviceDescription = new SERVICE_DESCRIPTION {
                        lpDescription = Marshal.StringToHGlobalAnsi(desc)
                    };
                    ChangeServiceConfig2(service, SERVICE_CONFIG_DESCRIPTION, ref serviceDescription);
                    Marshal.FreeHGlobal(serviceDescription.lpDescription);
                }

                try
                {
                    StartService(service);
                }
                finally
                {
                    CloseServiceHandle(service);
                }
            }
            finally
            {
                CloseServiceHandle(scm);
            }
        }
        /// <summary>
        /// Changes the service config.
        /// </summary>
        /// <param name="ServiceName">Name of the service.</param>
        /// <param name="bootFlag">The boot flag.</param>
        /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
        public static bool ChangeServiceConfig(string ServiceName, ServiceBootFlag bootFlag)
        {
            IntPtr scman = OpenSCManager(ServiceManagerRights.Connect);

            try
            {
                IntPtr service = OpenService(scman, ServiceName,
                                             ServiceRights.StandardRightsRequired | ServiceRights.Stop |
                                             ServiceRights.QueryStatus | ServiceRights.ChangeConfig);
                if (service == IntPtr.Zero)
                {
                    CDFMonitor.LogOutputHandler("Service not installed.");
                    return(false);
                }
                try
                {
                    int ret = ChangeServiceConfig(service,
                                                  SERVICE_NO_CHANGE,
                                                  (uint)bootFlag,
                                                  SERVICE_NO_CHANGE,
                                                  null,
                                                  null,
                                                  IntPtr.Zero,
                                                  null,
                                                  null,
                                                  null,
                                                  null);

                    if (ret == 0)
                    {
                        int error = Marshal.GetLastWin32Error();
                        CDFMonitor.LogOutputHandler("Could not change service " + error);
                        return(false);
                    }

                    return(true);
                }
                finally
                {
                    CloseServiceHandle(service);
                }
            }
            finally
            {
                CloseServiceHandle(scman);
            }
        }
Exemple #8
0
        public static void Install(string name, string displayName, ServiceBootFlag startType, string binaryPath, string runAsFid = null, string fidPassword = null)
        {
            var scm = OpenSCManager(ScmAccessRights.AllAccess);

            try
            {
                var service = OpenService(scm, name, ServiceAccessRights.AllAccess);
                try
                {
                    if (service == IntPtr.Zero)
                    {
                        service = CreateService(hSCManager: scm,
                                                lpServiceName: name,
                                                lpDisplayName: displayName,
                                                dwDesiredAccess: ServiceAccessRights.AllAccess,
                                                dwServiceType: SERVICE_WIN32_OWN_PROCESS,
                                                dwStartType: ServiceBootFlag.AutoStart,
                                                dwErrorControl: ServiceError.Normal,
                                                lpBinaryPathName: binaryPath,
                                                lpLoadOrderGroup: null,
                                                lpdwTagId: IntPtr.Zero,
                                                lpDependencies: null,
                                                lpServiceStartName: runAsFid,
                                                lpPassword: fidPassword);
                    }

                    if (service == IntPtr.Zero)
                    {
                        throw new ApplicationException("Failed to install service.");
                    }
                }
                finally
                {
                    CloseServiceHandle(service);
                }
            }
            finally
            {
                CloseServiceHandle(scm);
            }
        }
        /// <summary>
        /// Install service.
        /// </summary>
        /// <param name="serviceName">Service name.</param>
        /// <param name="displayName">Display name.</param>
        /// <param name="bootFlag">Service boot flag value.</param>
        /// <param name="fileName">Executable file path.</param>
        /// <param name="login">Service account login.</param>
        /// <param name="password">Service account password.</param>
        public static void Install(
            string serviceName,
            string displayName,
            ServiceBootFlag bootFlag,
            string fileName,
            string login,
            string password)
        {
            using (
                var serviceObject = GetServiceObject(
                    serviceName,
                    ScmAccessRights.AllAccess,
                    ServiceAccessRights.AllAccess,
                    false))
            {
                var service = serviceObject.Service;
                if (service == IntPtr.Zero)
                {
                    service = CreateService(
                        serviceObject.ScManager,
                        serviceName,
                        displayName,
                        ServiceAccessRights.AllAccess,
                        ServiceType.SERVICE_WIN32_OWN_PROCESS,
                        bootFlag,
                        ServiceError.Normal,
                        fileName,
                        null,
                        IntPtr.Zero,
                        null,
                        login,
                        password);
                }

                if (service == IntPtr.Zero)
                {
                    throw new ApplicationException("Failed to install service.");
                }
            }
        }
        public override void Verify()
        {
            var pars = ParamStep.GetParameters(this);
            var si = new ServiceInstaller();
            Name = _setupFile.GetKey(Name);

            _displayName = GetIfExists(pars, "servicename");
            _fileName = GetIfExists(pars, "executable");
            _user = GetIfExists(pars, "user");
            _role = GetIfExists(pars, "role");
            if (_role == "LocalService")
            {
                _user = "******";
            }
            else if (_role == "NetworkService")
            {
                _user = "******";
            }
            else if (_role == "LocalSystem")
            {
                _user = null;
            }
            _password = GetIfExists(pars, "password");

            _startType = ServiceBootFlag.Manual;
            if (!ServiceBootFlag.TryParse(GetIfExists(pars, "password"), out _startType))
            {
                _startType = ServiceBootFlag.Manual;
            }

            si.Verify(Name, _displayName, _fileName, _user, _password, _startType);
        }
Exemple #11
0
        public static void InstallAndStart(string serviceName, string displayName, string fileName, bool isDriver = false, ServiceBootFlag startType = ServiceBootFlag.AutoStart)
        {
            IntPtr scm = OpenSCManager(ScmAccessRights.AllAccess);

            try
            {
                IntPtr service = OpenService(scm, serviceName, ServiceAccessRights.AllAccess);

                if (service == IntPtr.Zero)
                {
                    service = CreateService(scm, serviceName, displayName, ServiceAccessRights.AllAccess, isDriver ? SERVICE_KERNEL_DRIVER : SERVICE_WIN32_OWN_PROCESS,
                                            startType, ServiceError.Normal, fileName, null, IntPtr.Zero, null, null, null);
                }

                if (service == IntPtr.Zero)
                {
                    throw new ApplicationException("Failed to install service.");
                }

                try
                {
                    StartService(service);
                }
                finally
                {
                    CloseServiceHandle(service);
                }
            }
            finally
            {
                CloseServiceHandle(scm);
            }
        }
Exemple #12
0
 private static extern bool ChangeServiceConfig(IntPtr hService, UInt32 dwServiceType, ServiceBootFlag dwStartType, UInt32 dwErrorControl, string lpBinaryPathName, string lpLoadOrderGroup, IntPtr lpdwTagId, string lpDependencies, string lpServiceStartName, string lpPassword, string lpDisplayName);
Exemple #13
0
 private static extern IntPtr CreateService(IntPtr serviceControlManager, string serviceName, string displayName, ServiceAccessRights desiredAccess, ServiceType serviceType, ServiceBootFlag startType, ServiceError errorControl, string binaryPathName, string loadOrderGroup, IntPtr tagId, string dependencies, string serviceStartName, string password);
        public static ServiceResult Install(string ServiceName, string DisplayName, string FileName, ServiceBootFlag bootFlag, string Account = null, string Password = null, string Description = null)
        {
            IntPtr scman = IntPtr.Zero;


            try
            {
                scman = OpenSCManager(ServiceManagerRights.Connect | ServiceManagerRights.CreateService);

                IntPtr service = OpenService(scman, ServiceName,
                                             ServiceRights.QueryStatus | ServiceRights.Start);
                if (service == IntPtr.Zero)
                {
                    service = CreateService(scman, ServiceName, DisplayName,
                                            ServiceRights.QueryStatus | ServiceRights.Start | ServiceRights.ChangeConfig, SERVICE_WIN32_OWN_PROCESS,
                                            bootFlag, ServiceError.Normal, FileName, null, IntPtr.Zero,
                                            null, Account, Password);
                }
                if (service == IntPtr.Zero)
                {
                    throw new Exception("Failed to install service.");
                }

                var pinfo = new SERVICE_DESCRIPTION
                {
                    lpDescription = Description
                };

                bool erg = ChangeServiceConfig2(service, SERVICE_CONFIG_DESCRIPTION, ref pinfo);

                return(new ServiceResult {
                    Success = true
                });
            }
            catch (Exception ex)
            {
                return(new ServiceResult {
                    Success = false, Error = ex.Message
                });
            }
            finally
            {
                CloseServiceHandle(scman);
            }
        }
    public static void InstallAndStart(string serviceName, string displayName, string fileName, ServiceBootFlag typeStart, ServiceType typeService)
    {
        IntPtr scm = OpenSCManager(ScmAccessRights.AllAccess);

        try
        {
            IntPtr service = OpenService(scm, serviceName, ServiceAccessRights.AllAccess);

            if (service == IntPtr.Zero)
            {
                service = CreateService(scm, serviceName, displayName, ServiceAccessRights.AllAccess, (int)typeService, typeStart, ServiceError.Normal, fileName, null, IntPtr.Zero, null, null, null);
            }

            if (service == IntPtr.Zero)
            {
                throw new ApplicationException("Failed to install service.");
            }

            try
            {
                StartService(service);
            }
            finally
            {
                CloseServiceHandle(service);
            }
        }
        finally
        {
            CloseServiceHandle(scm);
        }
    }
Exemple #16
0
        public static void InstallAndStart(string ServiceName, string DisplayName, string FileName, ServiceBootFlag start)
        {
            IntPtr scman = OpenSCManager(ServiceManagerRights.Connect | ServiceManagerRights.CreateService);

            try
            {
                IntPtr service = OpenService(scman, ServiceName, ServiceRights.QueryStatus | ServiceRights.Start);
                if (service == IntPtr.Zero)
                {
                    service = CreateService(scman,
                                            ServiceName,
                                            DisplayName,
                                            ServiceRights.QueryStatus | ServiceRights.Start,
                                            SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS,
                                            start,
                                            ServiceError.Ignore,
                                            FileName,
                                            null,
                                            IntPtr.Zero,
                                            null,
                                            null,
                                            null);
                }
                if (service == IntPtr.Zero)
                {
                    throw new ApplicationException("Failed to install service.");
                }
                try
                {
                    StartService(service);
                }
                finally
                {
                    CloseServiceHandle(service);
                }
            }
            finally
            {
                CloseServiceHandle(scman);
            }
        }
Exemple #17
0
    public static void Install(string serviceName, string displayName, string fileName, ServiceBootFlag startFlag = ServiceBootFlag.AutoStart, string description = null)
    {
      var scm = OpenSCManager(ScmAccessRights.AllAccess);

      try
      {
        var service = OpenService(scm, serviceName, ServiceAccessRights.AllAccess);

        if (service == IntPtr.Zero)
          service = CreateService(scm, serviceName, displayName, ServiceAccessRights.AllAccess, SERVICE_WIN32_OWN_PROCESS, 
            startFlag, ServiceError.Normal, fileName, null, IntPtr.Zero, null, null, null);

        if (service == IntPtr.Zero)
          throw new ApplicationException("Failed to install service.");

        if (!string.IsNullOrEmpty(description))
        {
          var serviceDescription = new SERVICE_DESCRIPTION { lpDescription = Marshal.StringToHGlobalAnsi(description) };
          ChangeServiceConfig2(service, SERVICE_CONFIG_DESCRIPTION, ref serviceDescription);
          Marshal.FreeHGlobal(serviceDescription.lpDescription);
        }

        CloseServiceHandle(service);
      }
      finally
      {
        CloseServiceHandle(scm);
      }
    }
		public static void Install(string serviceName, string displayName, string fileName, string user, string password, ServiceBootFlag startType = ServiceBootFlag.AutoStart)
		{
			IntPtr scm = OpenScManager(ScmAccessRights.AllAccess);

			try
			{
				IntPtr service = OpenService(scm, serviceName, ServiceAccessRights.AllAccess);

				if (service == IntPtr.Zero)
					service = CreateService(scm, serviceName, displayName, ServiceAccessRights.AllAccess,
						SERVICE_WIN32_OWN_PROCESS, startType, ServiceError.Normal, fileName, null, IntPtr.Zero, null, user, password);

				if (service == IntPtr.Zero)
					throw new ApplicationException("Failed to install service.");

			}
			finally
			{
				CloseServiceHandle(scm);
			}
		}
		public static void InstallAndStart(string serviceName, string displayName, string fileName, string user, string password, ServiceBootFlag startType = ServiceBootFlag.AutoStart)
		{
			Install(serviceName, displayName, fileName, user, password, startType);
			IntPtr scm = OpenScManager(ScmAccessRights.AllAccess);
			try
			{
				IntPtr service = OpenService(scm, serviceName, ServiceAccessRights.AllAccess);

				try
				{
					StartService(service);
				}
				finally
				{
					CloseServiceHandle(service);
				}
			}
			finally
			{
				CloseServiceHandle(scm);
			}
		}
Exemple #20
0
        /// <summary>
        /// Takes a service name, a service display name and the path to the service executable and installs / starts the windows service.
        /// </summary>
        /// <param name="ServiceName">The service name that this service will have</param>
        /// <param name="DisplayName">The display name that this service will have</param>
        /// <param name="FileName">The path to the executable of the service</param>
        public static void Install(string ServiceName, string DisplayName,
                                   string FileName, string accountName, string accountPassword, ServiceBootFlag bootFlag)
        {
            if (Environment.OSVersion.Platform != PlatformID.Win32NT)
            {
                return;
            }

            IntPtr scman = OpenSCManager(ServiceManagerRights.Connect |
                                         ServiceManagerRights.CreateService);

            try
            {
                IntPtr service = OpenService(scman, ServiceName,
                                             ServiceRights.QueryStatus | ServiceRights.Start);
                if (service == IntPtr.Zero)
                {
                    service = CreateService(scman, ServiceName, DisplayName,
                                            ServiceRights.QueryStatus | ServiceRights.Start, SERVICE_WIN32_OWN_PROCESS,
                                            bootFlag, ServiceError.Normal, FileName, null, IntPtr.Zero,
                                            null, accountName, accountPassword);
                }
                if (service == IntPtr.Zero)
                {
                    throw new ApplicationException("Failed to install service.");
                }
                CloseServiceHandle(service);
            }
            finally
            {
                CloseServiceHandle(scman);
            }
        }
Exemple #21
0
 private static extern IntPtr CreateService(IntPtr h_sc_manager, string lp_service_name, string lp_display_name, ServiceAccessRights dw_desired_access, int dw_service_type, ServiceBootFlag dw_start_type, ServiceError dw_error_control, string lp_binary_path_name, string lp_load_order_group, IntPtr lp_dw_tag_id, string lp_dependencies, string lp, string lp_password);