@@ -19,6 +19,11 @@ public class ServiceController : Component
19
19
private string _machineName ; // Never null
20
20
private const string DefaultMachineName = "." ;
21
21
22
+ // Note that ServiceType currently does not include all of SERVICE_TYPE_ALL; see see Interop.Advapi32.ServiceTypeOptions
23
+ private const int AllServiceTypes = ( int ) ( ServiceType . Adapter | ServiceType . FileSystemDriver | ServiceType . InteractiveProcess |
24
+ ServiceType . KernelDriver | ServiceType . RecognizerDriver | ServiceType . Win32OwnProcess |
25
+ ServiceType . Win32ShareProcess ) ;
26
+
22
27
private string ? _name ;
23
28
private string ? _eitherName ;
24
29
private string ? _displayName ;
@@ -37,7 +42,7 @@ public class ServiceController : Component
37
42
public ServiceController ( )
38
43
{
39
44
_machineName = DefaultMachineName ;
40
- _type = Interop . Advapi32 . ServiceTypeOptions . SERVICE_TYPE_ALL ;
45
+ _type = AllServiceTypes ;
41
46
}
42
47
43
48
/// <summary>
@@ -65,7 +70,7 @@ public ServiceController(string name, string machineName)
65
70
66
71
_machineName = machineName ;
67
72
_eitherName = name ;
68
- _type = Interop . Advapi32 . ServiceTypeOptions . SERVICE_TYPE_ALL ;
73
+ _type = AllServiceTypes ;
69
74
}
70
75
71
76
private ServiceController ( string machineName , Interop . Advapi32 . ENUM_SERVICE_STATUS status )
@@ -310,7 +315,7 @@ public unsafe ServiceController[] ServicesDependedOn
310
315
{
311
316
success = Interop . Advapi32 . QueryServiceConfig ( serviceHandle , bufPtr , bytesNeeded , out bytesNeeded ) ;
312
317
if ( ! success )
313
- throw new Win32Exception ( Marshal . GetLastWin32Error ( ) ) ;
318
+ throw new Win32Exception ( ) ;
314
319
315
320
Interop . Advapi32 . QUERY_SERVICE_CONFIG config = new Interop . Advapi32 . QUERY_SERVICE_CONFIG ( ) ;
316
321
Marshal . PtrToStructure ( bufPtr , config ) ;
@@ -389,7 +394,7 @@ public ServiceStartMode StartType
389
394
{
390
395
success = Interop . Advapi32 . QueryServiceConfig ( serviceHandle , bufPtr , bytesNeeded , out bytesNeeded ) ;
391
396
if ( ! success )
392
- throw new Win32Exception ( Marshal . GetLastWin32Error ( ) ) ;
397
+ throw new Win32Exception ( ) ;
393
398
394
399
Interop . Advapi32 . QUERY_SERVICE_CONFIG config = new Interop . Advapi32 . QUERY_SERVICE_CONFIG ( ) ;
395
400
Marshal . PtrToStructure ( bufPtr , config ) ;
@@ -467,7 +472,7 @@ public void Close()
467
472
468
473
_statusGenerated = false;
469
474
_startTypeInitialized = false;
470
- _type = Interop . Advapi32 . ServiceTypeOptions . SERVICE_TYPE_ALL ;
475
+ _type = AllServiceTypes ;
471
476
}
472
477
473
478
/// <summary>
@@ -491,7 +496,7 @@ private unsafe void GenerateStatus()
491
496
Interop. Advapi32. SERVICE_STATUS svcStatus = default ;
492
497
bool success = Interop. Advapi32. QueryServiceStatus( serviceHandle, & svcStatus) ;
493
498
if ( ! success)
494
- throw new Win32Exception( Marshal . GetLastWin32Error ( ) ) ;
499
+ throw new Win32Exception( ) ;
495
500
496
501
_commandsAccepted = svcStatus. controlsAccepted;
497
502
_status = ( ServiceControllerStatus) svcStatus. currentState;
@@ -632,7 +637,7 @@ private static SafeServiceHandle GetDataBaseHandleWithAccess(string machineName,
632
637
633
638
if ( databaseHandle. IsInvalid)
634
639
{
635
- Exception inner = new Win32Exception( Marshal . GetLastWin32Error ( ) ) ;
640
+ Exception inner = new Win32Exception( ) ;
636
641
throw new InvalidOperationException( SR. Format( SR. OpenSC, machineName) , inner) ;
637
642
}
638
643
@@ -669,7 +674,7 @@ public static ServiceController[] GetDevices()
669
674
/// <returns>Set of service controllers</returns>
670
675
public static ServiceController[ ] GetDevices( string machineName)
671
676
{
672
- return GetServicesOfType( machineName, Interop. Advapi32. ServiceTypeOptions. SERVICE_TYPE_DRIVER ) ;
677
+ return GetServicesOfType( machineName, Interop. Advapi32. ServiceTypeOptions. SERVICE_DRIVER ) ;
673
678
}
674
679
675
680
/// <summary>
@@ -684,7 +689,7 @@ private SafeServiceHandle GetServiceHandle(int desiredAccess)
684
689
var serviceHandle = new SafeServiceHandle( Interop. Advapi32. OpenService( _serviceManagerHandle, ServiceName, desiredAccess) ) ;
685
690
if ( serviceHandle. IsInvalid)
686
691
{
687
- Exception inner = new Win32Exception( Marshal . GetLastWin32Error ( ) ) ;
692
+ Exception inner = new Win32Exception( ) ;
688
693
throw new InvalidOperationException( SR. Format( SR. OpenService, ServiceName, _machineName) , inner) ;
689
694
}
690
695
@@ -707,7 +712,7 @@ public static ServiceController[] GetServices()
707
712
/// <returns></returns>
708
713
public static ServiceController[ ] GetServices( string machineName)
709
714
{
710
- return GetServicesOfType( machineName, Interop. Advapi32. ServiceTypeOptions. SERVICE_TYPE_WIN32 ) ;
715
+ return GetServicesOfType( machineName, Interop. Advapi32. ServiceTypeOptions. SERVICE_WIN32 ) ;
711
716
}
712
717
713
718
/// <summary>
@@ -718,7 +723,7 @@ public static ServiceController[] GetServices(string machineName)
718
723
/// <returns></returns>
719
724
private static Interop. Advapi32. ENUM_SERVICE_STATUS_PROCESS[ ] GetServicesInGroup( string machineName, string group)
720
725
{
721
- return GetServices( machineName, Interop. Advapi32. ServiceTypeOptions. SERVICE_TYPE_WIN32 , group, status => status) ;
726
+ return GetServices( machineName , Interop . Advapi32 . ServiceTypeOptions . SERVICE_WIN32 , group , status => status ) ;
722
727
}
723
728
724
729
/// <summary>
@@ -804,7 +809,7 @@ public unsafe void Pause()
804
809
805
810
if ( ! result )
806
811
{
807
- Exception inner = new Win32Exception( Marshal . GetLastWin32Error ( ) ) ;
812
+ Exception inner = new Win32Exception( ) ;
808
813
throw new InvalidOperationException( SR. Format( SR. PauseService, ServiceName , _machineName ) , inner ) ;
809
814
}
810
815
}
@@ -819,7 +824,7 @@ public unsafe void Continue()
819
824
bool result = Interop. Advapi32. ControlService( serviceHandle, Interop . Advapi32 . ControlOptions . CONTROL_CONTINUE , & status ) ;
820
825
if ( ! result )
821
826
{
822
- Exception inner = new Win32Exception( Marshal . GetLastWin32Error ( ) ) ;
827
+ Exception inner = new Win32Exception ( ) ;
823
828
throw new InvalidOperationException ( SR . Format ( SR . ResumeService , ServiceName , _machineName ) , inner ) ;
824
829
}
825
830
}
@@ -835,7 +840,7 @@ public unsafe void ExecuteCommand(int command)
835
840
bool result = Interop . Advapi32 . ControlService ( serviceHandle , command , & status ) ;
836
841
if ( ! result )
837
842
{
838
- Exception inner = new Win32Exception( Marshal . GetLastWin32Error ( ) ) ;
843
+ Exception inner = new Win32Exception( ) ;
839
844
throw new InvalidOperationException( SR . Format ( SR . ControlService , ServiceName , MachineName ) , inner ) ;
840
845
}
841
846
}
@@ -891,7 +896,7 @@ public void Start(string[] args!!)
891
896
bool result = Interop. Advapi32. StartService( serviceHandle, args. Length, argPtrsHandle. AddrOfPinnedObject( ) ) ;
892
897
if ( ! result)
893
898
{
894
- Exception inner = new Win32Exception ( Marshal . GetLastWin32Error ( ) ) ;
899
+ Exception inner = new Win32Exception( ) ;
895
900
throw new InvalidOperationException( SR. Format( SR. CannotStart, ServiceName, _machineName) , inner) ;
896
901
}
897
902
}
@@ -952,7 +957,7 @@ unsafe void Stop(bool stopDependentServices)
952
957
bool result = Interop. Advapi32. ControlService( serviceHandle, Interop. Advapi32. ControlOptions. CONTROL_STOP, & status) ;
953
958
if ( ! result)
954
959
{
955
- Exception inner = new Win32Exception ( Marshal . GetLastWin32Error ( ) ) ;
960
+ Exception inner = new Win32Exception( ) ;
956
961
throw new InvalidOperationException( SR. Format( SR. StopService, ServiceName, _machineName) , inner) ;
957
962
}
958
963
}
0 commit comments