Example #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);
            }
        }
Example #2
0
 private void StopService(ServiceListViewItem serviceListViewItem)
 {
     try
     {
         using (ServiceControlManager scm = ServiceControlManager.Connect(Advapi32.ServiceControlManagerAccessRights.Connect))
         {
             using (ServiceHandle serviceHandle = scm.OpenService(serviceListViewItem.ServiceName, Advapi32.ServiceAccessRights.Stop))
             {
                 serviceHandle.Stop();
             }
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message, _resManager.GetString("error"), MessageBoxButton.OK, MessageBoxImage.Error);
     }
 }
Example #3
0
        private void RemoveDaemon(ServiceListViewItem serviceListViewItem)
        {
            MessageBoxResult result = MessageBox.Show(_resManager.GetString("msg_warning_delete"), _resManager.GetString("question"),
                                                      MessageBoxButton.YesNo, MessageBoxImage.Question);

            if (result != MessageBoxResult.Yes)
            {
                return;
            }

            try
            {
                using (ServiceControlManager scm = ServiceControlManager.Connect(Advapi32.ServiceControlManagerAccessRights.Connect))
                {
                    using (ServiceHandle serviceHandle = scm.OpenService(serviceListViewItem.ServiceName, Advapi32.ServiceAccessRights.AllAccess))
                    {
                        serviceHandle.DeleteService();
                    }
                }
                _processCollection.Remove(_processCollection.Single(i => i.ServiceName == serviceListViewItem.ServiceName));

                //MessageBox.Show(_resManager.GetString("the_service_deletion_was_successful"),
                //    _resManager.GetString("success"), MessageBoxButton.OK, MessageBoxImage.Information);
            }
            catch (ServiceNotStoppedException)
            {
                result = MessageBox.Show(_resManager.GetString("you_must_stop_the_service_first"),
                                         _resManager.GetString("information"), MessageBoxButton.YesNo, MessageBoxImage.Information);

                if (result == MessageBoxResult.Yes)
                {
                    StopService(serviceListViewItem);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(_resManager.GetString("the_service_deletion_was_unsuccessful") + "\n" + ex.Message, _resManager.GetString("error"), MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
Example #4
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);
            }
        }
Example #5
0
        private void StartService(ServiceListViewItem serviceListViewItem, bool inUserSession = false)
        {
            try
            {
                if (inUserSession)
                {
                    //Write username where the service should start the process
                    RegistryManagement.WriteSessionUsername(serviceListViewItem.ServiceName, WindowsIdentity.GetCurrent().Name);
                }

                using (ServiceControlManager scm = ServiceControlManager.Connect(Advapi32.ServiceControlManagerAccessRights.Connect))
                {
                    using (ServiceHandle serviceHandle = scm.OpenService(serviceListViewItem.ServiceName, Advapi32.ServiceAccessRights.Start))
                    {
                        serviceHandle.Start();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, _resManager.GetString("error"), MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
Example #6
0
 private void AddDaemon()
 {
     if (ListViewDaemons.Items.Count <= 128)
     {
         try
         {
             var  serviceEditWindow = new ServiceEditWindow(null);
             bool?dialogResult      = serviceEditWindow.ShowDialog(); //Wait until the window is closed
             if (dialogResult.HasValue && dialogResult.Value)
             {
                 _processCollection.Add(ServiceListViewItem.CreateFromServiceDefinition(serviceEditWindow.GetServiceStartInfo()));
             }
         }
         catch (Exception ex)
         {
             MessageBox.Show(_resManager.GetString("failed_to_create_a_new_service") + "\n" + ex.Message, _resManager.GetString("error"), MessageBoxButton.OK, MessageBoxImage.Error);
         }
     }
     else
     {
         MessageBox.Show(_resManager.GetString("max_limit_reached"), _resManager.GetString("warning"), MessageBoxButton.OK, MessageBoxImage.Warning);
     }
 }
Example #7
0
        private void EditDaemon()
        {
            if (ListViewDaemons.SelectedItem == null)
            {
                return;
            }

            var serviceListViewItem = (ServiceListViewItem)ListViewDaemons.SelectedItem;

            //Stop service first
            using (var serviceController = new ServiceController(serviceListViewItem.ServiceName))
            {
                if (serviceController.Status != ServiceControllerStatus.Stopped)
                {
                    MessageBoxResult result = MessageBox.Show(_resManager.GetString("you_must_stop_the_service_first"),
                                                              _resManager.GetString("information"), MessageBoxButton.YesNo, MessageBoxImage.Information);


                    if (result == MessageBoxResult.Yes)
                    {
                        StopService(serviceListViewItem);

                        try
                        {
                            serviceController.WaitForStatus(ServiceControllerStatus.Stopped, TimeSpan.FromSeconds(10));
                        }
                        catch (TimeoutException ex)
                        {
                            //TODO: Better message
                            MessageBox.Show("Cannot stop the service:\n" + ex.Message, _resManager.GetString("error"), MessageBoxButton.OK, MessageBoxImage.Error);
                            return;
                        }
                    }
                    else
                    {
                        return;
                    }
                }
            }


            try
            {
                //Open service edit window with the data from registry
                var serviceEditWindow = new ServiceEditWindow(RegistryManagement.LoadServiceStartInfosFromRegistry(serviceListViewItem.ServiceName));
                //stops until windows has been closed
                bool?dialogResult = serviceEditWindow.ShowDialog();
                //Check result
                if (dialogResult.HasValue && dialogResult.Value)
                {
                    DmServiceDefinition serviceDefinition = serviceEditWindow.GetServiceStartInfo();
                    if (string.Equals(serviceDefinition.ServiceName, serviceListViewItem.ServiceName))
                    {
                        //Update serviceListViewItem
                        _processCollection[_processCollection.IndexOf(serviceListViewItem)] = ServiceListViewItem.CreateFromServiceDefinition(serviceDefinition);
                    }
                    else
                    {
                        //Create new daemon (Import as happend with a diffrent service name => create new service with that name)
                        _processCollection.Add(ServiceListViewItem.CreateFromServiceDefinition(serviceDefinition));
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(_resManager.GetString("cannot_load_data_from_registry") + "\n" + ex.Message, _resManager.GetString("error"), MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }