Пример #1
0
        private void OpenServiceItemMessageExecute(OpenEditServiceWindowMessage obj)
        {
            //// If it's not the view model of this view => return
            if (!_viewModel.Equals(obj.Sender))
            {
                return;
            }

            //Check if already one window with the same service is opened (from this instance) => when true show and focus it
            //var editServiceWindows = Application.Current.Windows.OfType<ServiceEditWindow>();
            var editServiceWindows = OwnedWindows.OfType <ServiceEditWindow>();
            var addWindow          = editServiceWindows.FirstOrDefault(x => x.WindowIdentifier == (obj.ServiceItem?.ServiceName ?? string.Empty));

            if (addWindow != default)
            {
                addWindow.Show();
                addWindow.Focus();
                return;
            }

            DmServiceDefinition data = obj.ServiceItem != null?RegistryManagement.LoadFromRegistry(obj.ServiceItem.ServiceName) : null;

            //TODO: MVVM also use show later...
            var window = new ServiceEditWindow(data)
            {
                Owner                 = this,
                DataContext           = new NewEditViewModel(),
                ReadOnlyMode          = obj.ReadOnlyMode,
                OriginalItem          = obj.ServiceItem,
                WindowIdentifier      = obj.ServiceItem?.ServiceName ?? string.Empty,
                WindowStartupLocation = WindowStartupLocation.CenterOwner
            };

            window.Show();
        }
Пример #2
0
        protected override void OnStart(string[] args)
        {
            base.OnStart(args);

            try
            {
                //Get the service name
                _serviceName = GetServiceName();

                //Get data from registry
                _serviceDefinition = RegistryManagement.LoadFromRegistry(_serviceName);

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

                //Setup NLOG for the service
                if (_serviceDefinition.UseEventLog)
                {
                    SetupEventLogService();
                }

                //Request additional time
                RequestAdditionalTime(_serviceDefinition.ProcessTimeoutTime + 1000);

                //Create a new DmProcess instance with reg data
                _dmProcess = new DmProcess(_serviceDefinition);
                _dmProcess.MaxRestartsReached += DmProcessOnMaxRestartsReached;
                _dmProcess.UpdateProcessPid   += DmProcessOnUpdateProcessPid;

                //Check if the service should start in a user session or in the service session
                string sessionUsername = RegistryManagement.ReadAndClearSessionUsername(_serviceName);
                if (string.IsNullOrWhiteSpace(sessionUsername))
                {
                    Logger.Info("Starting the process in service session...");
                    _dmProcess.StartProcess(null);
                }
                else
                {
                    Logger.Info("Starting the process in user session...");
                    _dmProcess.StartProcess(sessionUsername);
                }
            }
            catch (Exception ex)
            {
                Logger.Error(ex, ex.Message);
                Stop();
            }
        }
Пример #3
0
        /// <inheritdoc />
        public ServiceListViewItem Show(ServiceListViewItem item, EditWindowServiceCommand command = EditWindowServiceCommand.EditOrCreate)
        {
            //TODO: MVVM
            var dialog = new ServiceEditWindow(item != null ? RegistryManagement.LoadFromRegistry(item.ServiceName) : null)
            {
                OriginalItem = item,
                ReadOnlyMode = command == EditWindowServiceCommand.ViewOnly
            };

            dialog.Show();
            //var result = dialog.ShowDialog();
            //if (result.HasValue && result.Value)
            //{
            //    return new ServiceListViewItem(dialog.GetServiceStartInfo());
            //}

            return(null);
        }
Пример #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);
            }
        }