private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs args) { if (Services.Count > 0) { CurrentPercentage = 0.0; PercentPerService = 100.0 / Services.Count; ACCESS_MASK ServiceAccessMask = SSR.GetServiceAccessMask() | ACCESS_MASK.STANDARD_RIGHTS_READ | ACCESS_MASK.SERVICE_QUERY_STATUS; using (SCM = new NativeSCManager()) { int ServiceIndex = 0; foreach (ServiceObject so in Services) { try { Trace.TraceInformation("Invoke StatusMessage '{0}', {1}", so, so.CurrentState); Invoke(new StatusMessageDelegate(StatusMessage), so, so.CurrentState); CurrentPercentage = ServiceIndex * (100.0 / Services.Count); using (NativeService ns = new NativeService(SCM, so.GetText((int)ServiceItemTypes.ServiceName), ServiceAccessMask)) { backgroundWorker1_Process(ns, so); } ++ServiceIndex; } catch (Exception e) { GSharpTools.Tools.DumpException(e, "ChangeServiceStatus.backgroundWorker1_DoWork"); } if (bCancelled) break; } } } //backgroundWorker1.ReportProgress(100); }
private void backgroundWorker1_Process(NativeService ns, ServiceObject so) { bool requestedStatusChange = false; Trace.TraceInformation("BEGIN backgroundWorker1_Process for {0}", ns.Description); using (ServiceStatus ss = new ServiceStatus(ns)) { for (int i = 0; i < 100; ++i) { if (bCancelled) break; double RelativePercentage = (PercentPerService * (double)i) / 10.0; double Percentage = CurrentPercentage + RelativePercentage; int p = (int)Percentage; if (p >= 100) p = 99; backgroundWorker1.ReportProgress(p); if (!ss.Refresh()) break; Trace.TraceInformation("Refresh #{0}: Status is {1}", i, ss.Status.CurrentState); Invoke(new StatusMessageDelegate(StatusMessage), null, ss.Status.CurrentState); if (SSR.HasSuccess(ss.Status.CurrentState)) { Trace.WriteLine("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; Trace.TraceInformation("Ask {0} to issue its status request on {1}", SSR, ss); if (!SSR.Request(ss)) break; } else if (SSR.HasFailed(ss.Status.CurrentState)) { Trace.TraceInformation("ERROR, target state is one of the failed ones :("); break; } Thread.Sleep(500); } ss.Modify(so); Trace.TraceInformation("END backgroundWorker1_Process"); } }
public void Uninstall() { using (NativeSCManager SCM = new NativeSCManager()) { using (NativeService ns = new NativeService(SCM, ServiceName, ACCESS_MASK.STANDARD_RIGHTS_REQUIRED)) { NativeServiceFunctions.DeleteService(ns.Handle); } } }
public void RefreshConfig(NativeSCManager scm) { using (NativeService ns = new NativeService(scm, ServiceName)) { Config = ns.ServiceConfig; Objects[(int)ServiceItemTypes.Start] = NativeServiceFunctions.DescribeStartType(Config.StartType); Objects[(int)ServiceItemTypes.Path] = Config.BinaryPathName; Objects[(int)ServiceItemTypes.LoadOrderGroup] = Config.LoadOrderGroup; Objects[(int)ServiceItemTypes.Type] = DescribeServiceType(Config.ServiceType); Objects[(int)ServiceItemTypes.ErrorControl] = NativeServiceFunctions.DescribeErrorControl(Config.ErrorControl); Objects[(int)ServiceItemTypes.TagId] = Config.TagId.ToString(); Objects[(int)ServiceItemTypes.Dependencies] = Config.Dependencies; Objects[(int)ServiceItemTypes.Description] = ns.Description; Objects[(int)ServiceItemTypes.User] = Config.ServiceStartName; DescriptionText = ns.Description; if (Config.StartType == SC_START_TYPE.SERVICE_DISABLED) { ForegroundColor = Color.Gray; } } }
public bool ApplyChanges() { if (!Modified) return true; bool success = true; using (NativeSCManager SCM = new NativeSCManager()) { using (NativeService ns = new NativeService(SCM, ServiceName, ACCESS_MASK.SERVICE_CHANGE_CONFIG | ACCESS_MASK.SERVICE_QUERY_STATUS)) { success = NativeServiceFunctions.ChangeServiceConfig(ns.Handle, ServiceTypeModified ? Config.ServiceType : SC_SERVICE_TYPE.SERVICE_NO_CHANGE, StartTypeModified ? Config.StartType : SC_START_TYPE.SERVICE_NO_CHANGE, ErrorControlModified ? Config.ErrorControl : SC_ERROR_CONTROL.SERVICE_NO_CHANGE, BinaryPathNameModified ? Config.BinaryPathName : null, LoadOrderGroupModified ? Config.LoadOrderGroup : null, null, null, ServiceStartNameModified ? Config.ServiceStartName : null, PasswordModified ? PasswordText : null, DisplayNameModified ? DisplayName : null); if (success) { if (DescriptionModified) { SERVICE_DESCRIPTION sd = new SERVICE_DESCRIPTION(); sd.Description = DescriptionText; const int cbBufSize = 8 * 1024; IntPtr lpMemory = Marshal.AllocHGlobal((int)cbBufSize); Marshal.StructureToPtr(sd, lpMemory, false); NativeServiceFunctions.ChangeServiceConfig2(ns.Handle, SC_SERVICE_CONFIG.SERVICE_CONFIG_DESCRIPTION, lpMemory); Marshal.FreeHGlobal(lpMemory); } } Modified = false; RefreshConfig(SCM); } } return success; }
public ServiceStatus(NativeService service) { Service = service; Memory = Marshal.AllocHGlobal(Marshal.SizeOf(Status)); }