public void EnumDependentServices() { using (var scm = ServiceControlManager.OpenServiceControlManager(null, SCM_ACCESS.SC_MANAGER_CREATE_SERVICE | SCM_ACCESS.SC_MANAGER_ENUMERATE_SERVICE)) { // Just pick the first service to be dependency var dependentServiceNames = scm.EnumServicesStatus(SERVICE_TYPE.SERVICE_WIN32, SERVICE_STATE_FLAGS.SERVICE_STATE_ALL).Select(ss => ss.ServiceName).Take(1).ToList(); var path = typeof(DummyService).Assembly.Location; using (scm.CreateService( DummyService.DisplayName, DummyService.DisplayName, SERVICE_ACCESS.SERVICE_ALL_ACCESS, SERVICE_TYPE.SERVICE_WIN32_OWN_PROCESS, SERVICE_START_TYPE.SERVICE_AUTO_START, SERVICE_ERROR_CONTROL.SERVICE_ERROR_NORMAL, path, "", IntPtr.Zero, dependentServiceNames, null, null)) { } using (var service = scm.OpenService(dependentServiceNames.First(), SERVICE_ACCESS.SERVICE_ENUMERATE_DEPENDENTS)) { var serviceName = service.EnumDependentServices(SERVICE_STATE_FLAGS.SERVICE_STATE_ALL).Select(ss => ss.ServiceName).First(); Assert.That(serviceName, Is.EqualTo(DummyService.DisplayName)); } } }
public void OpenControlServiceManager() { // Just checking for a lack of exceptions at this stage using (ServiceControlManager.OpenServiceControlManager(null, 0)) { } }
public void CreateServiceFailure() { // Create should CreateServiceFailure() WithOperator insufficient permissions var scm = ServiceControlManager.OpenServiceControlManager(null, 0); Assert.That(() => CreateDummyService(scm), Throws.TypeOf <Win32Exception>().With.Property("NativeErrorCode").EqualTo(ERROR_ACCESS_DENIED)); }
public void DeleteService() { using (var scm = ServiceControlManager.OpenServiceControlManager(null, SCM_ACCESS.SC_MANAGER_CREATE_SERVICE)) using (var service = ServiceControlManagerTests.CreateDummyService(scm)) { service.Delete(); } }
public void CreateService() { using (var scm = ServiceControlManager.OpenServiceControlManager(null, SCM_ACCESS.SC_MANAGER_CREATE_SERVICE)) using (CreateDummyService(scm)) { // Service is cleaned up in TearDown } }
public void EnumServicesStatus() { using (var scm = ServiceControlManager.OpenServiceControlManager(null, SCM_ACCESS.SC_MANAGER_CREATE_SERVICE | SCM_ACCESS.SC_MANAGER_ENUMERATE_SERVICE)) using (CreateDummyService(scm)) { var services = scm.EnumServicesStatus(SERVICE_TYPE.SERVICE_WIN32, SERVICE_STATE_FLAGS.SERVICE_STATE_ALL); Assert.That(services.Count(s => s.ServiceName == DummyService.SvcName), Is.EqualTo(1)); } }
public void EnumServicesStatusEx() { using (var scm = ServiceControlManager.OpenServiceControlManager(null, SCM_ACCESS.SC_MANAGER_CREATE_SERVICE | SCM_ACCESS.SC_MANAGER_ENUMERATE_SERVICE)) using (CreateDummyService(scm)) { var service = scm.EnumServicesStatusEx(SERVICE_TYPE.SERVICE_WIN32, SERVICE_STATE_FLAGS.SERVICE_STATE_ALL).First(s => s.ServiceName == DummyService.SvcName); Assert.That(service.ServiceStatusProcess.currentState, Is.EqualTo(SERVICE_STATE.SERVICE_STOPPED)); } }
public void GetServiceDisplayName() { using (var scm = ServiceControlManager.OpenServiceControlManager(null, SCM_ACCESS.SC_MANAGER_CREATE_SERVICE)) { using (CreateDummyService(scm)) { } Assert.That(scm.GetServiceDisplayName(DummyService.SvcName), Is.EqualTo(DummyService.DisplayName)); } }
public void OpenControlServiceManagerFailure() { // ReSharper disable once InconsistentNaming const int RPC_S_SERVER_UNAVAILABLE = 1722; // Opening the service control manager on a server with an incorrect name (with spaces) throws quickly Assert.That(() => ServiceControlManager.OpenServiceControlManager("aa aa", 0), Throws.TypeOf <Win32Exception>() .With.Property("NativeErrorCode").EqualTo(RPC_S_SERVER_UNAVAILABLE)); }
public void EnumServicesStatusFailure() { using (var scm = ServiceControlManager.OpenServiceControlManager(null, 0)) { // ReSharper disable once AccessToDisposedClosure Assert.That(() => scm.EnumServicesStatus(SERVICE_TYPE.SERVICE_WIN32, SERVICE_STATE_FLAGS.SERVICE_STATE_ALL).ToList(), Throws.TypeOf <Win32Exception>() .With.Property("NativeErrorCode").EqualTo(ERROR_ACCESS_DENIED)); } }
public void StartWithParameters() { using (var scm = ServiceControlManager.OpenServiceControlManager(null, SCM_ACCESS.SC_MANAGER_CREATE_SERVICE)) using (var service = ServiceControlManagerTests.CreateDummyService(scm)) { service.Start(new[] { "Dummy Parameter" }); service.WaitForServiceToStart(); service.StopServiceAndWait(); } }
public void QueryConfig() { using (var scm = ServiceControlManager.OpenServiceControlManager(null, SCM_ACCESS.SC_MANAGER_CREATE_SERVICE)) using (var service = ServiceControlManagerTests.CreateDummyService(scm)) { var config = service.QueryConfig(); Assert.That(config.DisplayName, Is.EqualTo(DummyService.DisplayName)); // Service is cleaned up in TearDown } }
public static void DeleteDummyServiceIfItExists() { using (var scm = ServiceControlManager.OpenServiceControlManager(null, SCM_ACCESS.SC_MANAGER_ENUMERATE_SERVICE)) { var services = scm.EnumServicesStatus(SERVICE_TYPE.SERVICE_WIN32, SERVICE_STATE_FLAGS.SERVICE_STATE_ALL); foreach (var serviceName in services.Select(serviceStatus => serviceStatus.ServiceName).Where(name => name.StartsWith(DummyService.DisplayName) || name.StartsWith(DummyService.SvcName))) { DeleteService(scm, serviceName); } } }
public void OpenService() { // Again just checking for a lack of exceptions at this stage using (var scm = ServiceControlManager.OpenServiceControlManager(null, SCM_ACCESS.SC_MANAGER_CONNECT | SCM_ACCESS.SC_MANAGER_ENUMERATE_SERVICE)) { var serviceName = scm.EnumServicesStatus(SERVICE_TYPE.SERVICE_WIN32, SERVICE_STATE_FLAGS.SERVICE_STATE_ALL).Select(ss => ss.ServiceName).First(); using (scm.OpenService(serviceName, SERVICE_ACCESS.SERVICE_QUERY_STATUS)) { // Service is cleaned up in TearDown } } }
public void ChangeConfig2() { using (var scm = ServiceControlManager.OpenServiceControlManager(null, SCM_ACCESS.SC_MANAGER_CREATE_SERVICE | SCM_ACCESS.SC_MANAGER_ENUMERATE_SERVICE)) using (var service = ServiceControlManagerTests.CreateDummyService(scm)) { var description = new SERVICE_DESCRIPTION { Description = "A dummy service" }; service.ChangeConfig2(ref description); Assert.That(service.QueryConfig2 <SERVICE_DESCRIPTION>().Description, Is.EqualTo("A dummy service")); } }
public void QueryStatusEx() { using (var scm = ServiceControlManager.OpenServiceControlManager(null, SCM_ACCESS.SC_MANAGER_CREATE_SERVICE)) using (var service = ServiceControlManagerTests.CreateDummyService(scm)) { var status = service.QueryStatusEx(); Assert.That(status.currentState, Is.EqualTo(SERVICE_STATE.SERVICE_STOPPED)); service.Start(); service.WaitForServiceToStart(); Assert.That(service.QueryStatusEx().currentState, Is.EqualTo(SERVICE_STATE.SERVICE_RUNNING)); service.StopServiceAndWait(); } }
public void OpenServiceFailure() { // Check we get an exception if we try and open a service that doesn't exist // ReSharper disable once InconsistentNaming const int ERROR_SERVICE_DOES_NOT_EXIST = 1060; using (var scm = ServiceControlManager.OpenServiceControlManager(null, SCM_ACCESS.SC_MANAGER_CONNECT)) { // ReSharper disable once AccessToDisposedClosure Assert.That(() => scm.OpenService("Non existant service name", SERVICE_ACCESS.SERVICE_QUERY_STATUS), Throws.TypeOf <Win32Exception>() .With.Property("NativeErrorCode").EqualTo(ERROR_SERVICE_DOES_NOT_EXIST)); } }
public void ChangeConfigDependentServices() { using (var scm = ServiceControlManager.OpenServiceControlManager(null, SCM_ACCESS.SC_MANAGER_CREATE_SERVICE | SCM_ACCESS.SC_MANAGER_ENUMERATE_SERVICE)) using (var service = ServiceControlManagerTests.CreateDummyService(scm)) { // Just pick the first service to be dependency var dependentServiceNames = scm.EnumServicesStatus(SERVICE_TYPE.SERVICE_WIN32, SERVICE_STATE_FLAGS.SERVICE_STATE_ALL).Select(ss => ss.ServiceName).Take(1).ToList(); service.ChangeConfig(SERVICE_TYPE.SERVICE_NO_CHANGE, SERVICE_START_TYPE.SERVICE_NO_CHANGE, SERVICE_ERROR_CONTROL.SERVICE_NO_CHANGE, null, null, IntPtr.Zero, dependentServiceNames, null, null, null); using (var dependentService = scm.OpenService(dependentServiceNames.First(), SERVICE_ACCESS.SERVICE_ENUMERATE_DEPENDENTS)) { var serviceName = dependentService.EnumDependentServices(SERVICE_STATE_FLAGS.SERVICE_STATE_ALL).Select(ss => ss.ServiceName).First(); Assert.That(serviceName, Is.EqualTo(DummyService.SvcName)); } } }
public void Control() { using (var scm = ServiceControlManager.OpenServiceControlManager(null, SCM_ACCESS.SC_MANAGER_CREATE_SERVICE)) using (var service = ServiceControlManagerTests.CreateDummyService(scm)) { service.Start(); service.WaitForServiceToStart(); service.Control(SERVICE_CONTROL.SERVICE_CONTROL_PAUSE); service.WaitForServiceStatus(SERVICE_STATE.SERVICE_PAUSED); service.Control(SERVICE_CONTROL.SERVICE_CONTROL_CONTINUE); service.WaitForServiceStatus(SERVICE_STATE.SERVICE_RUNNING); service.StopServiceAndWait(); } }
public void ControlEx() { using (var scm = ServiceControlManager.OpenServiceControlManager(null, SCM_ACCESS.SC_MANAGER_CREATE_SERVICE)) using (var service = ServiceControlManagerTests.CreateDummyService(scm)) { service.Start(); service.WaitForServiceToStart(); var parameters = new SERVICE_CONTROL_STATUS_REASON_PARAMS(); service.ControlEx(SERVICE_CONTROL.SERVICE_CONTROL_PAUSE, ref parameters); Assert.That(parameters.serviceStatus.currentState, Is.Not.EqualTo(SERVICE_STATE.SERVICE_RUNNING)); service.WaitForServiceStatus(SERVICE_STATE.SERVICE_PAUSED); service.ControlEx(SERVICE_CONTROL.SERVICE_CONTROL_CONTINUE, ref parameters); service.WaitForServiceStatus(SERVICE_STATE.SERVICE_RUNNING); service.StopServiceAndWait(); } }
public void ChangeConfig() { using (var scm = ServiceControlManager.OpenServiceControlManager(null, SCM_ACCESS.SC_MANAGER_CREATE_SERVICE)) using (var service = ServiceControlManagerTests.CreateDummyService(scm)) { // No changes should not throw service.ChangeConfig( SERVICE_TYPE.SERVICE_NO_CHANGE, SERVICE_START_TYPE.SERVICE_NO_CHANGE, SERVICE_ERROR_CONTROL.SERVICE_NO_CHANGE, null, null, IntPtr.Zero, null, null, null, null); // Set service type to share process service.ChangeConfig( SERVICE_TYPE.SERVICE_WIN32_SHARE_PROCESS, SERVICE_START_TYPE.SERVICE_NO_CHANGE, SERVICE_ERROR_CONTROL.SERVICE_NO_CHANGE, null, null, IntPtr.Zero, null, null, null, null); Assert.That(service.QueryConfig().ServiceType, Is.EqualTo((uint)SERVICE_TYPE.SERVICE_WIN32_SHARE_PROCESS)); // Set start type to disabled service.ChangeConfig( SERVICE_TYPE.SERVICE_NO_CHANGE, SERVICE_START_TYPE.SERVICE_DISABLED, SERVICE_ERROR_CONTROL.SERVICE_NO_CHANGE, null, null, IntPtr.Zero, null, null, null, null); Assert.That(service.QueryConfig().StartType, Is.EqualTo((uint)SERVICE_START_TYPE.SERVICE_DISABLED)); // Set error control to critical service.ChangeConfig( SERVICE_TYPE.SERVICE_NO_CHANGE, SERVICE_START_TYPE.SERVICE_NO_CHANGE, SERVICE_ERROR_CONTROL.SERVICE_ERROR_CRITICAL, null, null, IntPtr.Zero, null, null, null, null); Assert.That(service.QueryConfig().ErrorControl, Is.EqualTo((uint)SERVICE_ERROR_CONTROL.SERVICE_ERROR_CRITICAL)); service.ChangeConfig( SERVICE_TYPE.SERVICE_NO_CHANGE, SERVICE_START_TYPE.SERVICE_NO_CHANGE, SERVICE_ERROR_CONTROL.SERVICE_NO_CHANGE, null, null, IntPtr.Zero, null, null, null, "New Display Name"); Assert.That(service.QueryConfig().DisplayName, Is.EqualTo("New Display Name")); } }