Ejemplo n.º 1
0
        private void RestartService(ServiceListViewItem serviceListViewItem)
        {
            try
            {
                using (ServiceControlManager scm = ServiceControlManager.Connect(Advapi32.ServiceControlManagerAccessRights.Connect))
                {
                    using (ServiceHandle serviceHandle = scm.OpenService(serviceListViewItem.ServiceName, Advapi32.ServiceAccessRights.QueryStatus | Advapi32.ServiceAccessRights.Start | Advapi32.ServiceAccessRights.Stop))
                    {
                        Advapi32.ServiceStatusProcess status = serviceHandle.QueryServiceStatus();

                        //Stop service (throws an exception if it is stopped)
                        serviceHandle.Stop();

                        //Wait for stop
                        serviceHandle.WaitForStatus(Advapi32.ServiceCurrentState.Stopped, TimeSpan.FromSeconds(10));

                        //Start service
                        serviceHandle.Start();
                    }
                }
            }
            catch (System.TimeoutException)
            {
                MessageBox.Show(_resManager.GetString("timeout_exception_service_restart"), _resManager.GetString("error"), MessageBoxButton.OK, MessageBoxImage.Error);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, _resManager.GetString("error"), MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
Ejemplo n.º 2
0
        private void KillService(ServiceListViewItem serviceListViewItem)
        {
            try
            {
                using (ServiceControlManager scm = ServiceControlManager.Connect(Advapi32.ServiceControlManagerAccessRights.Connect))
                {
                    using (ServiceHandle serviceHandle = scm.OpenService(serviceListViewItem.ServiceName, Advapi32.ServiceAccessRights.UserDefinedControl | Advapi32.ServiceAccessRights.QueryStatus))
                    {
                        if (serviceHandle.QueryServiceStatus().currentState == Advapi32.ServiceCurrentState.Stopped)
                        {
                            return;
                        }

                        try
                        {
                            serviceHandle.ExecuteCommand((int)ServiceCommands.ServiceKillProcessAndStop);
                            serviceHandle.WaitForStatus(Advapi32.ServiceCurrentState.Stopped, TimeSpan.FromSeconds(2));
                        }
                        catch (TimeoutException)
                        {
                            if (KillChildProcessJob.IsSupportedWindowsVersion && serviceListViewItem.ServicePid != null)
                            {
                                Process process = Process.GetProcessById((int)serviceListViewItem.ServicePid);
                                process.Kill();
                            }
                            else
                            {
                                MessageBox.Show(_resManager.GetString("cannot_kill_service", CultureInfo.CurrentUICulture), _resManager.GetString("error"), MessageBoxButton.OK, MessageBoxImage.Error);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, _resManager.GetString("error"), MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
Ejemplo n.º 3
0
        public static ActionResult DeleteAllServices(Session session)
        {
            string appFolder = session.CustomActionData["APPDIR"];

            if (string.IsNullOrWhiteSpace(appFolder) || !Directory.Exists(appFolder))
            {
                session.Log("AppFolder is null or invalid.");
                return(ActionResult.Failure);
            }

            session.Log("Beginning the uninstall of all services.");

            try
            {
                using (ServiceControlManager controlManager = ServiceControlManager.Connect(Advapi32.ServiceControlManagerAccessRights.Connect))
                {
                    //Get all services for this installation
                    List <string> serviceNameList = new List <string>();
                    using (RegistryKey mainKey = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Services\", RegistryKeyPermissionCheck.ReadSubTree))
                    {
                        if (mainKey == null)
                        {
                            return(ActionResult.Failure);
                        }

                        foreach (string serviceName in mainKey.GetSubKeyNames())
                        {
                            using (RegistryKey key = mainKey.OpenSubKey(serviceName, RegistryKeyPermissionCheck.ReadSubTree))
                            {
                                //If the key invalid, skip this service
                                if (key == null)
                                {
                                    continue;
                                }

                                //Get the exe path of the service to determine later if its a service from DaemonMaster
                                string serviceExePath = Convert.ToString(key.GetValue("ImagePath") ?? string.Empty);


                                if (string.IsNullOrWhiteSpace(serviceExePath) || !DaemonMasterUtils.ComparePaths(serviceExePath, appFolder + ServiceControlManager.DmServiceFileName)) //Not possible to use ServiceControlManager.DmServiceExe because the AppDomain changed here => so that we need to rebuilt the path.
                                {
                                    continue;
                                }

                                serviceNameList.Add(serviceName);
                            }
                        }
                    }

                    foreach (string serviceName in serviceNameList)
                    {
                        using (ServiceHandle service = controlManager.OpenService(serviceName, Advapi32.ServiceAccessRights.AllAccess))
                        {
                            session.Log("Try to delete " + serviceName);

                            try
                            {
                                Advapi32.ServiceStatusProcess serviceStatus = service.QueryServiceStatus();

                                if (serviceStatus.currentState != Advapi32.ServiceCurrentState.Stopped)
                                {
                                    session.Log("Try killing " + serviceName + " service.");
                                    service.ExecuteCommand((int)ServiceCommands.ServiceKillProcessAndStop);

                                    try
                                    {
                                        service.WaitForStatus(Advapi32.ServiceCurrentState.Stopped, TimeSpan.FromSeconds(5));
                                    }
                                    catch (TimeoutException)
                                    {
                                        session.Log("Terminate " + serviceName + " service.");
                                        Process process = Process.GetProcessById((int)serviceStatus.processId);
                                        process.Kill();
                                    }
                                }

                                service.DeleteService();
                                session.Log("Deleted " + serviceName + " service successful.");
                            }
                            catch (Exception e)
                            {
                                session.Log("Deletion of " + serviceName + " failed.\n" + e.Message + "\n" + e.StackTrace);
                                //continue
                            }
                        }
                    }
                }

                session.Log("Uninstall of all services was successful.");
                return(ActionResult.Success);
            }
            catch (Exception e)
            {
                session.Log(e.Message + "\n" + e.StackTrace);
                return(ActionResult.Failure);
            }
        }
Ejemplo n.º 4
0
        private static int RunEditReturnExitCode(EditOptions opts)
        {
            //Check Admin right
            if (!DaemonMasterUtils.IsElevated())
            {
                Console.WriteLine("You must start the program with admin rights.");
                return(1);
            }

            //------------------------

            DmServiceDefinition serviceDefinition;

            try
            {
                if (string.IsNullOrWhiteSpace(opts.ServiceName))
                {
                    Console.WriteLine("The given service name is invalid.");
                    return(-1);
                }

                string realServiceName = opts.ServiceName;
                if (!RegistryManagement.IsDaemonMasterService(realServiceName))
                {
                    realServiceName = "DaemonMaster_" + realServiceName; //Check for the name of the old system TODO: remove later
                    if (!RegistryManagement.IsDaemonMasterService(realServiceName))
                    {
                        Console.WriteLine("Cannot found a DaemonMaster service with the given name.");
                        return(-1);
                    }
                }

                //Load data from registry
                serviceDefinition = RegistryManagement.LoadFromRegistry(realServiceName);
            }
            catch (Exception)
            {
                Console.WriteLine("Cannot found a service with the given service name."); //"\n" + e.Message + "\n StackTrace: " + e.StackTrace);
                return(1);
            }

            try
            {
                CheckAndSetCommonArguments(ref serviceDefinition, opts);

                //Edit service
                using (ServiceControlManager scm = ServiceControlManager.Connect(Advapi32.ServiceControlManagerAccessRights.Connect))
                {
                    using (ServiceHandle service = scm.OpenService(serviceDefinition.ServiceName, Advapi32.ServiceAccessRights.AllAccess))
                    {
                        if (service.QueryServiceStatus().currentState != Advapi32.ServiceCurrentState.Stopped)
                        {
                            Console.WriteLine("Service is not stopped, please stop it first.");
                            return(1);
                        }

                        service.ChangeConfig(serviceDefinition);
                    }
                }

                //Save arguments in registry
                RegistryManagement.SaveInRegistry(serviceDefinition);

                Console.WriteLine("Successful!");
                return(0);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return(1);
            }
        }