Exemplo n.º 1
0
        /// <summary>
        /// This method stops a given service and deletes it. The SCM handle passed must have standard rights.
        /// It handles the case of service not being in started mode.
        /// </summary>
        /// <param name="hSCManager">Handle to SCM with standard rights</param>
        /// <param name="svcName">Name of the service to be deleted</param>
        public static ServiceDeleteStatus StopAndRemoveService(IntPtr hSCManager, string svcName)
        {
            AppAssert.Assert(NativeMethods.NullIntPtr != hSCManager, "Invalid SC Manager handle!");
            AppAssert.Assert(null != svcName, "Null service name passed!");

            //return success if the service doesn't exist
            if (ServiceConfigurationHandler.CheckServiceExists(hSCManager, svcName) == false)
            {
                return(ServiceDeleteStatus.ServiceDeleteSuccess);
            }

            // stop and delete the service
            try
            {
                //stop the service
                ServiceConfigurationHandler.StopService(svcName);

                ServiceConfigurationHandler.DeleteService(hSCManager, svcName);//throws ServiceConfigurationException if delete failed
            }
            catch
            {
                return(ServiceDeleteStatus.ServiceDeleteFailed);
            }

            //state: Delete succeeded
            return(ServiceDeleteStatus.ServiceDeleteSuccess);
        }
Exemplo n.º 2
0
        public static void StopAndDisableService(IntPtr hSCManager, string svcName)
        {
            ServiceConfigurationHandler.StopService(svcName);
            int returnCode = SetServiceStatus(hSCManager, svcName, NativeMethods.SERVICE_DISABLED);

            if (returnCode != 0)
            {
                throw new Exception(String.Format("Cannot stop the {0} service", svcName));
            }
        }