示例#1
0
        /// <summary>
        /// Uninstall a Windows service. The specified service and the services
        /// that services that depend on the specified service will be stopped
        /// first, then the service is uninstalled.
        /// </summary>
        /// <param name="machineName">
        /// The name of the target computer. If the value is null or an empty
        /// string, the function connects to the SCM on the local machine.
        /// </param>
        /// <param name="serviceName">
        /// The name of the service to uninstall. The maximum string length is 256
        /// characters.
        /// </param>
        public static void UninstallService(string machineName, string serviceName)
        {
            // Try to stop the service and the services that depend on the service.
            if (machineName.IsEmpty())
            {
                machineName = ".";
            }
            ServiceController service = new ServiceController(serviceName, machineName);

            if (service.Status != ServiceControllerStatus.Stopped)
            {
                service.Stop();
                service.WaitForStatus(ServiceControllerStatus.Stopped,
                                      new TimeSpan(0, 0, 30));
            }

            SafeServiceHandle schSCManager = null;
            SafeServiceHandle schService   = null;

            try
            {
                // Get a handle to the SCM database.
                schSCManager = NativeMethods.OpenSCManager(
                    machineName,
                    null,
                    ServiceControlAccessRights.SC_MANAGER_CONNECT);
                if (schSCManager.IsInvalid)
                {
                    throw new Win32Exception();
                }

                // Open the service with the delete permission.
                schService = NativeMethods.OpenService(
                    schSCManager,
                    serviceName,
                    ServiceAccessRights.SERVICE_STOP | ServiceAccessRights.DELETE);
                if (schService.IsInvalid)
                {
                    throw new Win32Exception();
                }

                // Now remove the service by calling DeleteService.
                if (!NativeMethods.DeleteService(schService))
                {
                    throw new Win32Exception();
                }
            }
            finally
            {
                if (schSCManager != null)
                {
                    schSCManager.Close();
                }
                if (schService != null)
                {
                    schService.Close();
                }
            }
        }
示例#2
0
        /// <summary>
        /// Install a Windows service.
        /// </summary>
        /// <param name="machineName">
        /// The name of the target computer. If the value is null or an empty
        /// string, the function connects to the SCM on the local machine.
        /// </param>
        /// <param name="serviceName">
        /// The name of the service to install. The maximum string length is 256
        /// characters.
        /// </param>
        /// <param name="displayName">
        /// The display name to be used by user interface programs to identify the
        /// service. This string has a maximum length of 256 chars.
        /// </param>
        /// <param name="dwStartType">
        /// The service start options. The parameter can be one of the following
        /// values: SERVICE_AUTO_START, SERVICE_BOOT_START, SERVICE_DEMAND_START,
        /// SERVICE_DISABLED, SERVICE_SYSTEM_START.
        /// </param>
        /// <param name="binaryPath">
        /// The full path to the service binary file.
        /// </param>
        /// <param name="serviceStartName">
        /// The name of the account under which the service should run. If this
        /// parameter is NULL, CreateService uses the LocalSystem account. The
        /// parameter can also be "NT AUTHORITY\LocalService",
        /// "NT AUTHORITY\NetworkService", or in the form of "DomainName\UserName",
        /// or ".\UserName".
        /// </param>
        /// <param name="password">
        /// The password to the account name specified by the serviceStartName
        /// parameter. Specify an empty string if the account has no password or if
        /// the service runs in the LocalService, NetworkService, or LocalSystem
        /// account.
        /// </param>
        /// <returns></returns>
        public static void InstallService(string machineName, string serviceName,
                                          string displayName, ServiceStart dwStartType, string binaryPath,
                                          string serviceStartName, string password)
        {
            SafeServiceHandle schSCManager = null;
            SafeServiceHandle schService   = null;

            try
            {
                // Get a handle to the SCM database.
                schSCManager = NativeMethods.OpenSCManager(
                    machineName,
                    null,
                    ServiceControlAccessRights.SC_MANAGER_CREATE_SERVICE);
                if (schSCManager.IsInvalid)
                {
                    throw new Win32Exception();
                }

                // Create the service.
                schService = NativeMethods.CreateService(
                    schSCManager,
                    serviceName,
                    displayName,
                    ServiceAccessRights.SERVICE_ALL_ACCESS,
                    ServiceType.SERVICE_WIN32_OWN_PROCESS,
                    dwStartType,
                    ServiceError.SERVICE_ERROR_NORMAL,
                    binaryPath,
                    null,
                    null,
                    null,
                    serviceStartName,
                    password);
                if (schService.IsInvalid)
                {
                    throw new Win32Exception();
                }
            }
            finally
            {
                if (schSCManager != null)
                {
                    schSCManager.Close();
                }
                if (schService != null)
                {
                    schService.Close();
                }
            }
        }