Exemple #1
0
        /// <summary>
        /// The constructor opens access to the service
        /// </summary>
        /// <param name="scm">The SCM instance that contains the service</param>
        /// <param name="ServiceName">Name of the service</param>
        /// <param name="am">Access rights required.</param>
        public NativeService(NativeSCManager scm, string serviceName, ACCESS_MASK am = ACCESS_MASK.STANDARD_RIGHTS_READ | ACCESS_MASK.GENERIC_READ)
        {
            ServiceName = serviceName;

            Handle = NativeServiceFunctions.OpenService(
                scm.Handle,
                serviceName,
                (uint)am);

            IsValid = (Handle.ToInt32() != 0);
            if (!IsValid)
            {
                NativeHelpers.ReportFailure("OpenService({0}, {1})", serviceName, am);
            }
        }
 private bool ApplyStartupChanges(SC_START_TYPE startupType)
 {
     using (new WaitCursor())
     {
         bool result = true;
         using (NativeSCManager scm = new NativeSCManager(MachineName))
         {
             foreach (ServiceDataObject sdo in MainListView.SelectedItems)
             {
                 if( !sdo.ApplyStartupChanges(scm, startupType) )
                 {
                     result = false;
                 }
             }
         }
         return result;
     }
 }
        public override void Refresh(ObservableCollection<DataObject> objects)
        {
            using(var manager = new RefreshManager<ServiceDataObject>(objects))
            {
                using (NativeSCManager scm = new NativeSCManager(MachineName))
                {
                    foreach (ENUM_SERVICE_STATUS_PROCESS essp in scm.Refresh(ServicesType))
                    {
                        using (NativeService ns = new NativeService(scm, essp.ServiceName))
                        {
                            ServiceDataObject sdo = null;

                            if (manager.Contains(essp.ServiceName, out sdo))
                            {
                                sdo.UpdateFrom(essp);
                            }
                            else
                            {
                                objects.Add(new ServiceDataObject(ns, essp));
                            }
                        }
                    }
                }
            }
        }
        public void OnUnstall(object sender, RoutedEventArgs e)
        {
            List<ServiceDataObject> deleteThese = new List<ServiceDataObject>();
            foreach (ServiceDataObject sdo in MainListView.SelectedItems)
            {
                deleteThese.Add(sdo);
            }

            using (NativeSCManager scm = new NativeSCManager(MachineName))
            {
                foreach (ServiceDataObject sdo in deleteThese)
                {
                    MessageBoxResult result = MessageBox.Show(
                        string.Format(Resources.IDS_SERVICE_SureToUninstall, sdo.InternalID, sdo.DisplayName),
                        Resources.IDS_CONFIRMATION,
                        MessageBoxButton.YesNoCancel,
                        MessageBoxImage.Question);
                    if (result == MessageBoxResult.Yes)
                    {
                        if (sdo.Uninstall(scm))
                        {
                            MainWindow.Items.Remove(sdo);
                        }
                    }
                    else if (result == MessageBoxResult.Cancel)
                    {
                        break;
                    }
                }
            }
        }
        public override void ApplyTemplateInfo(ActionTemplateInfo ati, BackgroundAction action)
        {
            // default implementation: do nothing
            ServiceDataObject sdo = GetServiceDataObjectByID(ati.ID);
            if (sdo == null)
            {
                Log.ErrorFormat("Unable to find object for ID {0}", ati.ID);
            }
            else
            {
                string expectedStartType = ati["StartTypeString"];
                string actualStartType = sdo.StartTypeString;
                if (expectedStartType != actualStartType)
                {
                    Log.InfoFormat("=> StartType for {0} needs to change from {1} to {2}",
                        ati,
                        actualStartType,
                        expectedStartType);

                    using (NativeSCManager scm = new NativeSCManager(MachineName))
                    {
                        SC_START_TYPE startType = ServicesLocalisation.ReverseLocalizedStartType(expectedStartType);
                        if( startType != SC_START_TYPE.SERVICE_NO_CHANGE )
                        {
                            sdo.ApplyStartupChanges(scm, startType);
                        }
                    }
                }
                else
                {
                    Log.InfoFormat("=> StartType for {0} identical, no need to change", ati);
                }
            }
        }
 public override void ApplyChanges(List<IDataObjectDetails> changedItems)
 {
     using (new WaitCursor())
     {
         using (NativeSCManager scm = new NativeSCManager(MachineName))
         {
             foreach (IDataObjectDetails dod in changedItems)
             {
                 dod.ApplyChanges(scm);
             }
         }
     }
 }
Exemple #7
0
 public bool Uninstall(NativeSCManager scm)
 {
     try
     {
         using (NativeService ns = new NativeService(scm,
                 InternalID,
                 ACCESS_MASK.SERVICE_CHANGE_CONFIG | ACCESS_MASK.SERVICE_QUERY_STATUS | ACCESS_MASK.DELETE))
         {
             return NativeServiceFunctions.DeleteService(ns.Handle);
         }
     }
     catch(Exception e)
     {
         Log.Error("DeleteService", e);
         return false;
     }
 }
Exemple #8
0
 public bool ApplyStartupChanges(NativeSCManager scm, SC_START_TYPE startupType)
 {
     bool success = true;
     if (startupType != StartType)
     {
         Log.InfoFormat("{0}: Change SC_START_TYPE from {1} to {2}", InternalID, StartType, startupType);
         using (NativeService ns = new NativeService(scm,
             InternalID,
             ACCESS_MASK.SERVICE_CHANGE_CONFIG | ACCESS_MASK.SERVICE_QUERY_STATUS))
         {
             success = NativeServiceFunctions.ChangeServiceConfig(ns.Handle,
                 StartType: startupType);
             if( success )
             {
                 StartType = startupType;
             }
         }
     }
     return success;
 }
Exemple #9
0
        public void ApplyChanges(
            NativeSCManager scm,
            SC_START_TYPE startupType,
            string displayName,
            string binaryPathName,
            string description)
        {
            using (NativeService ns = new NativeService(scm,
                InternalID,
                ACCESS_MASK.SERVICE_CHANGE_CONFIG | ACCESS_MASK.SERVICE_QUERY_STATUS))
            {
                bool success = NativeServiceFunctions.ChangeServiceConfig(ns.Handle,
                    StartType: startupType,
                    DisplayName: displayName,
                    BinaryPathName: binaryPathName);
                if (success)
                {
                    StartType = startupType;
                    if( displayName != null )
                    {
                        SetStringProperty("DisplayName", displayName);
                    }
                    if( binaryPathName != null )
                    {
                        SetStringProperty("BinaryPathName", binaryPathName);
                        NotifyPropertyChanged("InstallLocation");
                    }

                    if ((description != null) && !description.Equals(Description))
                    {
                        ns.Description = description;
                        SetStringProperty("Description", description);
                    }
                }
            }
        }
        public override void DoWork()
        {
            ACCESS_MASK ServiceAccessMask = SSR.GetServiceAccessMask() | ACCESS_MASK.STANDARD_RIGHTS_READ | ACCESS_MASK.SERVICE_QUERY_STATUS;

            ServicesDataController sdc = MainWindow.CurrentController as ServicesDataController;

            using (NativeSCManager scm = new NativeSCManager(sdc.MachineName))
            {
                int serviceIndex = 0;
                foreach (ServiceDataObject so in Services)
                {
                    ++serviceIndex;

                    try
                    {
                        SetOutputText(string.Format("Service {0}/{1}: {2} is initially in state {3}",
                                        serviceIndex,
                                        Services.Count,
                                        so.DisplayName,
                                        ServicesLocalisation.Localized(so.CurrentState)));

                        if( so.CurrentState == SC_RUNTIME_STATUS.SERVICE_STOPPED )
                        {
                            ServiceAccessMask &= ~(ACCESS_MASK.SERVICE_STOP);
                        }

                        using (NativeService ns = new NativeService(scm, so.InternalID, ServiceAccessMask))
                        {
                            bool requestedStatusChange = false;

                            Log.InfoFormat("BEGIN backgroundWorker1_Process for {0}", ns.Description);
                            using (ServiceStatus ss = new ServiceStatus(ns))
                            {
                                for (int i = 0; i < 100; ++i)
                                {
                                    if (Worker.CancellationPending)
                                        break;

                                    if (!ss.Refresh())
                                        break;

                                    SetOutputText(string.Format("Service {0}/{1}: {2} is now in state {3}",
                                        serviceIndex,
                                        Services.Count,
                                        so.DisplayName,
                                        ServicesLocalisation.Localized(ss.Status.CurrentState)));
                                    
                                    if (SSR.HasSuccess(ss.Status.CurrentState))
                                    {
                                        Log.Info("Reached target status, done...");
                                        break; // TODO: reached 100% of this service' status reqs. 
                                    }

                                    // if we haven't asked the service to change its status yet, do so now. 
                                    if (!requestedStatusChange)
                                    {
                                        requestedStatusChange = true;
                                        Log.InfoFormat("Ask {0} to issue its status request on {1}", SSR, ss);
                                        if (!SSR.Request(ss))
                                            break;
                                    }
                                    else if (SSR.HasFailed(ss.Status.CurrentState))
                                    {
                                        Log.Error("ERROR, target state is one of the failed ones :(");
                                        break;
                                    }
                                    Thread.Sleep(500);
                                }
                                so.UpdateFrom(ss.Status);
                                Log.Info("END backgroundWorker1_Process");
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Log.Error("Exception caught in PerformServiceStateRequest", ex);
                    }
                    if (Worker.CancellationPending)
                        break;
                }
            }
        }