Ejemplo n.º 1
0
        public static ServiceController Create(string machineName, string serviceName, string displayName,
                                               ServiceType serviceType, ServiceStartMode startType,
                                               ServiceErrorControl errorControl, string binaryPathName,
                                               string loadOrderGroup, string[] dependencies, string serviceStartName,
                                               string password)
        {
            var hScManager = OpenSCManager(machineName, null, ScmAccessRights.CreateService);
            if (hScManager == IntPtr.Zero)
            {
                throw new Win32Exception();
            }
            try
            {
                var dependencyList = dependencies == null || dependencies.Length == 0
                                         ? null
                                         : string.Join("\0", dependencies);
                var hService = CreateService(hScManager, serviceName, displayName, ServiceAccessRights.AllAccess,
                                             serviceType, startType, errorControl, binaryPathName, loadOrderGroup,
                                             IntPtr.Zero, dependencyList, serviceStartName, password);
                if (hService == IntPtr.Zero)
                {
                    throw new Win32Exception();
                }
                CloseServiceHandle(hService);

                return new ServiceController(serviceName, machineName);
            }
            finally
            {
                CloseServiceHandle(hScManager);
            }
        }
Ejemplo n.º 2
0
        public static ServiceController Create(string machineName, string serviceName, string displayName,
                                               ServiceType serviceType, ServiceStartMode startType,
                                               ServiceErrorControl errorControl, string binaryPathName,
                                               string loadOrderGroup, string[] dependencies, string serviceStartName,
                                               string password)
        {
            var hScManager = OpenSCManager(machineName, null, ScmAccessRights.CreateService);

            if (hScManager == IntPtr.Zero)
            {
                throw new Win32Exception();
            }
            try
            {
                var dependencyList = dependencies == null || dependencies.Length == 0
                                         ? null
                                         : string.Join("\0", dependencies);
                var hService = CreateService(hScManager, serviceName, displayName, ServiceAccessRights.AllAccess,
                                             serviceType, startType, errorControl, binaryPathName, loadOrderGroup,
                                             IntPtr.Zero, dependencyList, serviceStartName, password);
                if (hService == IntPtr.Zero)
                {
                    throw new Win32Exception();
                }
                CloseServiceHandle(hService);

                return(new ServiceController(serviceName, machineName));
            }
            finally
            {
                CloseServiceHandle(hScManager);
            }
        }
        /// <summary>
        /// Create a new service.
        /// </summary>
        /// <param name="name">The name of the service.</param>
        /// <param name="display_name">The display name for the service.</param>
        /// <param name="service_type">The service type.</param>
        /// <param name="start_type">The service start type.</param>
        /// <param name="error_control">Error control.</param>
        /// <param name="binary_path_name">Path to the service executable.</param>
        /// <param name="load_order_group">Load group order.</param>
        /// <param name="dependencies">List of service dependencies.</param>
        /// <param name="service_start_name">The username for the service.</param>
        /// <param name="password">Password for the username if needed.</param>
        /// <param name="throw_on_error">True to throw on error.</param>
        /// <returns>The registered service information.</returns>
        public static NtResult <RunningService> CreateService(
            string name,
            string display_name,
            ServiceType service_type,
            ServiceStartType start_type,
            ServiceErrorControl error_control,
            string binary_path_name,
            string load_order_group,
            IEnumerable <string> dependencies,
            string service_start_name,
            SecureString password,
            bool throw_on_error)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentException($"'{nameof(name)}' cannot be null or empty", nameof(name));
            }

            if (string.IsNullOrEmpty(binary_path_name))
            {
                throw new ArgumentException($"'{nameof(binary_path_name)}' cannot be null or empty", nameof(binary_path_name));
            }

            using (var scm = Win32NativeMethods.OpenSCManager(null, null,
                                                              ServiceControlManagerAccessRights.Connect | ServiceControlManagerAccessRights.CreateService))
            {
                if (scm.IsInvalid)
                {
                    return(Win32Utils.GetLastWin32Error().CreateResultFromDosError <RunningService>(throw_on_error));
                }

                IntPtr pwd = password != null?Marshal.SecureStringToBSTR(password) : IntPtr.Zero;

                try
                {
                    using (var service = Win32NativeMethods.CreateService(scm, name, display_name, ServiceAccessRights.MaximumAllowed,
                                                                          service_type, start_type, error_control, binary_path_name, load_order_group, null, dependencies.ToMultiString(),
                                                                          string.IsNullOrEmpty(service_start_name) ? null : service_start_name, pwd))
                    {
                        if (service.IsInvalid)
                        {
                            return(Win32Utils.GetLastWin32Error().CreateResultFromDosError <RunningService>(throw_on_error));
                        }
                        return(new RunningService(name, display_name ?? string.Empty, QueryStatus(service)).CreateResult());
                    }
                }
                finally
                {
                    if (pwd != IntPtr.Zero)
                    {
                        Marshal.FreeBSTR(pwd);
                    }
                }
            }
        }
Ejemplo n.º 4
0
 public static extern bool ChangeServiceConfig(
     SafeServiceHandle hService,
     ServiceType dwServiceType,
     ServiceStartType dwStartType,
     ServiceErrorControl dwErrorControl,
     string lpBinaryPathName,
     string lpLoadOrderGroup,
     int lpdwTagId,
     string lpDependencies,
     string lpServiceStartName,
     string lpPassword,
     string lpDisplayName);
Ejemplo n.º 5
0
 ///<summary>Describes a new Win32 service.</summary>
 /// <param name="Name">The name of the service used in the service database.</param>
 public ServiceAttribute(string Name)
 {
     this.name             = Name;
     this.displayName      = Name; //If no display name is specified, then make it the same as the name...
     this.description      = "";
     this.run              = true;
     this.servType         = ServiceType.Default;
     this.servAccessType   = ServiceAccessType.AllAccess;
     this.servStartType    = ServiceStartType.AutoStart;
     this.servErrorControl = ServiceErrorControl.Normal;
     this.servControls     = ServiceControls.Default;
     this.logName          = "Services";
 }
Ejemplo n.º 6
0
 ///<summary>Describes a new Win32 service.</summary>
 /// <param name="Name">The name of the service used in the service database.</param>
 /// <param name="DisplayName">The name of the service that will be displayed in the services snap-in.</param>
 public ServiceAttribute(string Name, string DisplayName)
 {
     this.name             = Name;
     this.displayName      = DisplayName;
     this.description      = "";
     this.run              = true;
     this.servType         = ServiceType.Default;
     this.servAccessType   = ServiceAccessType.AllAccess;
     this.servStartType    = ServiceStartType.AutoStart;
     this.servErrorControl = ServiceErrorControl.Normal;
     this.servControls     = ServiceControls.Default;
     this.logName          = "Services";
 }
Ejemplo n.º 7
0
 ///<summary>Describes a new Win32 service.</summary>
 /// <param name="Name">The name of the service used in the service database.</param>
 /// <param name="DisplayName">The name of the service that will be displayed in the services snap-in.</param>
 /// <param name="Description">The description of the service that will be displayed in the service snap-in.</param>
 /// <param name="Run">Indicates if you want the service to run or not on program startup.</param>
 /// <param name="ServiceType">Indicates the type of service you will be running. By default this is "Default."</param>
 /// <param name="ServiceAccessType">Access to the service. Before granting the requested access, the system checks the access token of the calling process.</param>
 /// <param name="ServiceStartType">Service start options. By default this is "AutoStart."</param>
 /// <param name="ServiceErrorControl">Severity of the error, and action taken, if this service fails to start.</param>
 public ServiceAttribute(string Name, string DisplayName, string Description, bool Run, ServiceType ServiceType, ServiceAccessType ServiceAccessType, ServiceStartType ServiceStartType, ServiceErrorControl ServiceErrorControl)
 {
     this.name             = Name;
     this.displayName      = DisplayName;
     this.description      = Description;
     this.run              = Run;
     this.servType         = ServiceType;
     this.servAccessType   = ServiceAccessType.AllAccess;
     this.servStartType    = ServiceStartType;
     this.servErrorControl = ServiceErrorControl.Normal;
     this.servControls     = ServiceControls.Default;
     this.logName          = "Services";
 }
 internal static extern bool ChangeServiceConfigW(
     /* _In_      SC_HANDLE */ [In] SafeServiceHandle hService,
     /* _In_      DWORD     */ [In] ServiceType dwServiceType,
     /* _In_      DWORD     */ [In] ServiceStartType dwStartType,
     /* _In_      DWORD     */ [In] ServiceErrorControl dwErrorControl,
     /* _In_opt_  LPCTSTR   */ [In] string lpBinaryPathName,
     /* _In_opt_  LPCTSTR   */ [In] string lpLoadOrderGroup,
     /* _Out_opt_ LPDWORD   */ [In][Out] IntPtr lpdwTagId,
     /* _In_opt_  LPCTSTR   */ [In] string lpDependencies,
     /* _In_opt_  LPCTSTR   */ [In] string lpServiceStartName,
     /* _In_opt_  LPCTSTR   */ [In] string lpPassword,
     /* _In_opt_  LPCTSTR   */ [In] string lpDisplayName
     );
Ejemplo n.º 9
0
 protected static extern NativeService CreateService(
     NativeServiceManager hSCManager,
     string lpServiceName,
     string lpDisplayName,
     ServiceRights dwDesiredAccess,
     ServiceType dwServiceType,
     ServiceStartType dwStartType,
     ServiceErrorControl dwErrorControl,
     string lpBinaryPathName,
     string lpLoadOrderGroup,
     IntPtr lpdwTagId,
     string lpDependencies,
     string lpServiceStartName,
     string lpPassword
     );
 internal static extern SafeServiceHandle CreateService(
     SafeServiceHandle hSCManager,
     string lpServiceName,
     string lpDisplayName,
     ServiceAccessRights dwDesiredAccess,
     ServiceType dwServiceType,
     ServiceStartType dwStartType,
     ServiceErrorControl dwErrorControl,
     string lpBinaryPathName,
     string lpLoadOrderGroup,
     [Out] OptionalInt32 lpdwTagId,
     string lpDependencies,
     string lpServiceStartName,
     IntPtr lpPassword
     );
 /// <summary>
 /// Create a new service.
 /// </summary>
 /// <param name="name">The name of the service.</param>
 /// <param name="display_name">The display name for the service.</param>
 /// <param name="service_type">The service type.</param>
 /// <param name="start_type">The service start type.</param>
 /// <param name="error_control">Error control.</param>
 /// <param name="binary_path_name">Path to the service executable.</param>
 /// <param name="load_order_group">Load group order.</param>
 /// <param name="dependencies">List of service dependencies.</param>
 /// <param name="service_start_name">The username for the service.</param>
 /// <param name="password">Password for the username if needed.</param>
 /// <returns>The registered service information.</returns>
 public static RunningService CreateService(
     string name,
     string display_name,
     ServiceType service_type,
     ServiceStartType start_type,
     ServiceErrorControl error_control,
     string binary_path_name,
     string load_order_group,
     IEnumerable <string> dependencies,
     string service_start_name,
     SecureString password)
 {
     return(CreateService(name, display_name, service_type,
                          start_type, error_control, binary_path_name, load_order_group,
                          dependencies, service_start_name, password, true).Result);
 }
Ejemplo n.º 12
0
 public static extern SafeServiceHandle CreateService(SafeServiceHandle hSCManager, string lpServiceName, string lpDisplayName, ServiceAccess dwDesiredAccess, ServiceType dwServiceType, ServiceStartType dwStartType, ServiceErrorControl dwErrorControl, string lpBinaryPathName, string lpLoadOrderGroup, int lpdwTagId, string lpDependencies, string lpServiceStartName, string lpPassword);
Ejemplo n.º 13
0
 public static extern bool ChangeServiceConfig(
     SafeServiceHandle hService,
     ServiceType dwServiceType,
     ServiceStartType dwStartType,
     ServiceErrorControl dwErrorControl,
     string lpBinaryPathName,
     string lpLoadOrderGroup,
     int lpdwTagId,
     string lpDependencies,
     string lpServiceStartName,
     string lpPassword,
     string lpDisplayName);
Ejemplo n.º 14
0
 protected static extern NativeService CreateService(
     NativeServiceManager hSCManager,
     string lpServiceName,
     string lpDisplayName,
     ServiceRights dwDesiredAccess,
     ServiceType dwServiceType,
     ServiceStartType dwStartType,
     ServiceErrorControl dwErrorControl,
     string lpBinaryPathName,
     string lpLoadOrderGroup,
     IntPtr lpdwTagId,
     string lpDependencies,
     string lpServiceStartName,
     string lpPassword
 );
Ejemplo n.º 15
0
        public ServiceHandle CreateService(string name, string displayName,
                                           ServiceType type, ServiceStartType startType, ServiceErrorControl errorControl,
                                           string binaryPath, string group, string accountName, string password)
        {
            IntPtr service;

            if ((service = Win32.CreateService(this, name, displayName, ServiceAccess.All,
                                               type, startType, errorControl, binaryPath, group,
                                               IntPtr.Zero, IntPtr.Zero, accountName, password)) == IntPtr.Zero)
            {
                Win32.Throw();
            }

            return(new ServiceHandle(service, true));
        }
Ejemplo n.º 16
0
 private static extern IntPtr CreateService(IntPtr hScManager, string serviceName, string displayName,
                                            ServiceAccessRights desiredAccess, ServiceType serviceType,
                                            ServiceStartMode startType, ServiceErrorControl errorControl,
                                            string binaryPathName, string loadOrderGroup, IntPtr tagId,
                                            string dependencies, string serviceStartName, string password);
Ejemplo n.º 17
0
 public static extern IntPtr CreateService(IntPtr hScm, string serviceName, string displayName, ServiceAccessMask desiredAccess,
                                           ServiceType serviceType, ServiceStartType startType, ServiceErrorControl errorControl,
                                           string imagePath, string loadOrderGroup, IntPtr tag,
                                           string dependencies = null, string serviceStartName = null, string password = null);
Ejemplo n.º 18
0
 public static extern SafeServiceHandle CreateService(SafeServiceHandle hSCManager, string lpServiceName, string lpDisplayName, ServiceAccess dwDesiredAccess, ServiceType dwServiceType, ServiceStartType dwStartType, ServiceErrorControl dwErrorControl, string lpBinaryPathName, string lpLoadOrderGroup, int lpdwTagId, string lpDependencies, string lpServiceStartName, string lpPassword);
Ejemplo n.º 19
0
 public static extern IntPtr CreateService(IntPtr hSCManager, string lpSvcName, string lpDisplayName, ServiceControlManagerType dwDesiredAccess, ServiceType dwServiceType, ServiceStartType dwStartType, ServiceErrorControl dwErrorControl, string lpPathName, string lpLoadOrderGroup, int lpdwTagId, string lpDependencies, string lpServiceStartName, string lpPassword);
Ejemplo n.º 20
0
 private static extern IntPtr CreateService(IntPtr hScManager, string serviceName, string displayName,
                                            ServiceAccessRights desiredAccess, ServiceType serviceType,
                                            ServiceStartMode startType, ServiceErrorControl errorControl,
                                            string binaryPathName, string loadOrderGroup, IntPtr tagId,
                                            string dependencies, string serviceStartName, string password);