Example #1
0
        public void ChangeConfig(
            SERVICE_TYPE serviceType,
            SERVICE_START_TYPE startType,
            SERVICE_ERROR_CONTROL errorControl,
            string binaryPathName,
            string loadOrderGroup,
            IntPtr tagId,
            IEnumerable <string> dependencies,
            string serviceStartName,
            string password,
            string displayName)
        {
            var dependenciesString = dependencies == null ? null : string.Join("\0", dependencies) + "\0"; // Collection of strings separated by null and terminated by double null

            if (!NativeMethods.ChangeServiceConfig(
                    handle,
                    (uint)serviceType,
                    (uint)startType,
                    (uint)errorControl,
                    binaryPathName,
                    loadOrderGroup,
                    tagId,
                    dependenciesString,
                    serviceStartName,
                    password,
                    displayName))
            {
                throw new Win32Exception();
            }
        }
            public ServiceHandle CreateService(string name, string displayName,
                SERVICE_TYPE type, SERVICE_START_TYPE startType, SERVICE_ERROR_CONTROL errorControl,
                string binaryPath, string group, string accountName, string password)
            {
                int service;

                if ((service = Win32.CreateService(this, name, displayName, SERVICE_RIGHTS.SERVICE_ALL_ACCESS,
                    type, startType, errorControl, binaryPath, group, 0, 0, accountName, password)) == 0)
                    ThrowLastWin32Error();

                return new ServiceHandle(service, true);
            }
Example #3
0
 public static extern bool ChangeServiceConfig(
     int Service,
     SERVICE_TYPE ServiceType,
     SERVICE_START_TYPE StartType,
     SERVICE_ERROR_CONTROL ErrorControl,
     string BinaryPath,
     string LoadOrderGroup,
     int TagID,
     int Dependencies,
     string StartName,
     string Password,
     string DisplayName
     );
Example #4
0
 internal static extern IntPtr CreateService(
     IntPtr hSCManager,
     string lpServiceName,
     string lpDisplayName,
     SERVICE_ACCESS dwDesiredAccess,
     SERVICE_TYPE dwServiceType,
     SERVICE_START_TYPE dwStartType,
     SERVICE_ERROR dwErrorControl,
     string lpBinaryPathName,
     string lpLoadOrderGroup,
     IntPtr lpdwTagId,
     string lpDependencies,
     string lpServiceStartName,
     string lpPassword);
Example #5
0
 public static extern int CreateService(int SCManager,
                                        string ServiceName,
                                        string DisplayName,
                                        SERVICE_RIGHTS DesiredAccess,
                                        SERVICE_TYPE ServiceType,
                                        SERVICE_START_TYPE StartType,
                                        SERVICE_ERROR_CONTROL ErrorControl,
                                        string BinaryPathName,
                                        string LoadOrderGroup,
                                        int TagID,
                                        int Dependencies,
                                        string ServiceStartName,
                                        string Password
                                        );
Example #6
0
            public ServiceHandle CreateService(string name, string displayName,
                                               SERVICE_TYPE type, SERVICE_START_TYPE startType, SERVICE_ERROR_CONTROL errorControl,
                                               string binaryPath, string group, string accountName, string password)
            {
                int service;

                if ((service = Win32.CreateService(this, name, displayName, SERVICE_RIGHTS.SERVICE_ALL_ACCESS,
                                                   type, startType, errorControl, binaryPath, group, 0, 0, accountName, password)) == 0)
                {
                    ThrowLastWin32Error();
                }

                return(new ServiceHandle(service, true));
            }
Example #7
0
        public IService CreateService(
            string serviceName,
            string displayName,
            SERVICE_ACCESS desiredAccess,
            SERVICE_TYPE serviceType,
            SERVICE_START_TYPE startType,
            SERVICE_ERROR_CONTROL errorControl,
            string binaryPathName,
            string loadOrderGroup,
            IntPtr tagId,
            ICollection <string> dependencies,
            string serviceStartName,
            string password)
        {
            string deps = (dependencies?.Any() ?? false) ? string.Join("\0", dependencies) : null;

            var serviceHandle = NativeMethods.CreateService(
                handle,
                serviceName,
                displayName,
                (uint)desiredAccess,
                (uint)serviceType,
                (uint)startType,
                (uint)errorControl,
                binaryPathName,
                loadOrderGroup,
                tagId,
                deps,
                serviceStartName,
                password
                );

            if (serviceHandle == IntPtr.Zero)
            {
                throw new Win32Exception();
            }

            return(new Service(serviceHandle));
        }
 public static extern bool ChangeServiceConfig(SafeServiceHandle handle, uint serviceType, SERVICE_START_TYPE startType, uint errorControl, string binaryPathName, string loadOrderGroup, IntPtr tagId, string dependencies, string serviceStartName, string password, string displayName);
Example #9
0
 public static extern int CreateService(int SCManager,
     string ServiceName,
     string DisplayName,
     SERVICE_RIGHTS DesiredAccess, 
     SERVICE_TYPE ServiceType,
     SERVICE_START_TYPE StartType,
     SERVICE_ERROR_CONTROL ErrorControl,
     string BinaryPathName,
     string LoadOrderGroup,
     int TagID, 
     int Dependencies,
     string ServiceStartName,
     string Password
     );
Example #10
0
 public static extern bool ChangeServiceConfig(
     int Service,
     SERVICE_TYPE ServiceType, 
     SERVICE_START_TYPE StartType,
     SERVICE_ERROR_CONTROL ErrorControl, 
     string BinaryPath, 
     string LoadOrderGroup,
     int TagID, 
     int Dependencies,
     string StartName,
     string Password,
     string DisplayName
     );
 public ServiceHandle CreateService(string name, string displayName,
     SERVICE_TYPE type, SERVICE_START_TYPE startType, string binaryPath)
 {
     return this.CreateService(name, displayName, type, startType,
         SERVICE_ERROR_CONTROL.Ignore, binaryPath, null, null, null);
 }
Example #12
0
 public ServiceHandle CreateService(string name, string displayName,
                                    SERVICE_TYPE type, SERVICE_START_TYPE startType, string binaryPath)
 {
     return(this.CreateService(name, displayName, type, startType,
                               SERVICE_ERROR_CONTROL.Ignore, binaryPath, null, null, null));
 }
        /// <summary>
        /// This method installs and runs the service in the service control manager.
        /// </summary>
        /// <param name="svcPath">The complete path of the service.</param>
        /// <param name="svcName">Name of the service.</param>
        /// <param name="svcDispName">Display name of the service.</param>
        /// <param name="svcDescription">The English description of the service.</param>
        /// <param name="svcStartType">The start type of the service (automatic, manual, etc)</param>
        /// <returns>True if the process went thro successfully. False if there was any error.</returns>
        public bool InstallService(string svcPath, string svcName, string svcDispName, string svcDescription, SERVICE_START_TYPE svcStartType, string dependencies)
        {
            var bSuccess = false;

            #region Constants declaration.

            int SERVICE_WIN32_OWN_PROCESS = 0x00000010;
            int SERVICE_INTERACTIVE_PROCESS = 0x00000100;
            int SERVICE_ERROR_NORMAL = 0x00000001;

            int STANDARD_RIGHTS_REQUIRED = 0xF0000;
            int SERVICE_QUERY_CONFIG = 0x0001;
            int SERVICE_CHANGE_CONFIG = 0x0002;
            int SERVICE_QUERY_STATUS = 0x0004;
            int SERVICE_ENUMERATE_DEPENDENTS = 0x0008;
            int SERVICE_START = 0x0010;
            int SERVICE_STOP = 0x0020;
            int SERVICE_PAUSE_CONTINUE = 0x0040;
            int SERVICE_INTERROGATE = 0x0080;
            int SERVICE_USER_DEFINED_CONTROL = 0x0100;

            int SERVICE_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED |
                SERVICE_QUERY_CONFIG |
                SERVICE_CHANGE_CONFIG |
                SERVICE_QUERY_STATUS |
                SERVICE_ENUMERATE_DEPENDENTS |
                SERVICE_START |
                SERVICE_STOP |
                SERVICE_PAUSE_CONTINUE |
                SERVICE_INTERROGATE |
                SERVICE_USER_DEFINED_CONTROL);

            #endregion Constants declaration.

            try
            {
                var hServiceCtrlMgr = OpenSCManager(null, null, SCM_ACCESS.SC_MANAGER_CREATE_SERVICE);
                var servDesc = new SERVICE_DESCRIPTION()
                    {
                        lpDescription = svcDescription
                    };

                if (hServiceCtrlMgr.ToInt32() != 0)
                {
                    IntPtr hService = CreateService(hServiceCtrlMgr, svcName, svcDispName,
                                                        SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS,
                                                        svcStartType, SERVICE_ERROR_NORMAL,
                                                        svcPath, null, 0, dependencies, null, null);

                    if (hService.ToInt32() == 0)
                    {
                        CloseServiceHandle(hServiceCtrlMgr);
                        throw new Exception("Unable to start the service due to some error.  Is the service already running?");
                    }
                    else
                    {
                        //  Add the description to the service
                        bSuccess = ChangeServiceConfig2A(hService, InfoLevel.SERVICE_CONFIG_DESCRIPTION, ref servDesc);

                        //  Now trying to start the service
                        int nStartSuccess = StartService(hService, 0, null);

                        //  If the value i is zero, then there was an error starting the service.
                        //  NOTE: error may arise if the service is already running or some other problem.
                        if (nStartSuccess != 0)
                        {
                            CloseServiceHandle(hServiceCtrlMgr);
                            bSuccess = true;
                        }
                        else
                            throw new Exception("Unable to start the service due to some error.  Is the service already running?");
                    }
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex);
            }

            return bSuccess;
        }
 public static extern IntPtr CreateService(IntPtr SC_HANDLE, string lpSvcName, string lpDisplayName,
                                     int dwDesiredAccess, int dwServiceType, SERVICE_START_TYPE dwStartType,
                                     int dwErrorControl, string lpPathName, string lpLoadOrderGroup,
                                     int lpdwTagId, string lpDependencies, string lpServiceStartName,
                                     string lpPassword);