Skip to content

Commit ffec0aa

Browse files
committed
Renamed NewService class as Service + updated doc/sample for new service API
1 parent eb131d2 commit ffec0aa

6 files changed

Lines changed: 85 additions & 25 deletions

File tree

ctypes_generation/extended_structs/_SERVICE_STATUS_PROCESS.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,10 @@ def dwServiceFlags(self):
3939
state = dwCurrentState
4040
type = dwServiceType
4141
control_accepted = dwControlsAccepted
42-
flags = dwServiceFlags
42+
flags = dwServiceFlags
43+
44+
45+
def __repr__(self):
46+
return """<{0} type={1!r} state={2!r}>""".format(type(self).__name__,
47+
self.type,
48+
self.state)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
(cmd) python service\service_demo.py
2+
Listing the first 3 services:
3+
* <Service "1394ohci" SERVICE_STOPPED(0x1)>
4+
* <Service "3ware" SERVICE_STOPPED(0x1)>
5+
* <Service "ACPI" SERVICE_RUNNING(0x4)>
6+
7+
Retriving service <TapiSrv>
8+
<Service "TapiSrv" SERVICE_STOPPED(0x1)>
9+
- name: 'TapiSrv'
10+
- description: 'Telephony'
11+
- state: SERVICE_STOPPED(0x1)
12+
- type: 48L
13+
- process: None
14+
- security-description: O:SYG:SYD:(A;;CCLCSWRPWPDTLOCRRC;;;SY)(A;;CCDCLCSWRPWPDTLOCRSDRCWDWO;;;BA)(A;;CCLCSWRPLOCRRC;;;IU)(A;;CCLCSWRPLOCRRC;;;SU)
15+
Trying to start the service
16+
Service started !
17+
<Service "TapiSrv" SERVICE_RUNNING(0x4)>
18+
- state: SERVICE_RUNNING(0x4)
19+
- process: <WinProcess "!cannot-retrieve-name" pid 14700 at 0x4a2c4f0>

docs/source/samples_output/system.txt

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,45 +3,45 @@ Basic system infos:
33
version = (10, 0)
44
bitness = 64
55
computer_name = WILLIE
6-
product_type = VER_NT_WORKSTATION(0x1L)
6+
product_type = 1
77
version_name = Windows 10
88

9-
There is 194 processes
10-
There is 2324 threads
9+
There is 331 processes
10+
There is 4010 threads
1111

1212
Dumping first logical drive:
13-
<LogicalDrive "C:\" (DRIVE_FIXED)>
14-
name = C:\
15-
type = DRIVE_FIXED(0x3L)
16-
path = \Device\HarddiskVolume2
13+
<LogicalDrive "B:\" (DRIVE_FIXED)>
14+
name = B:\
15+
type = 3
16+
path = \Device\HarddiskVolume7
1717

1818
Dumping first service:
19-
<ServiceA "1394ohci" SERVICE_STOPPED(0x1L)>
19+
<Service "1394ohci" SERVICE_STOPPED(0x1)>
2020
name = 1394ohci
2121
description = 1394 OHCI Compliant Host Controller
22-
status = ServiceStatus(type=SERVICE_KERNEL_DRIVER(0x1L), state=SERVICE_STOPPED(0x1L), control_accepted=0L, flags=0L)
22+
status = <_SERVICE_STATUS_PROCESS type=SERVICE_KERNEL_DRIVER(0x1) state=SERVICE_STOPPED(0x1)>
2323
process = None
2424

2525
Finding a service in a user process:
26-
<ServiceA "AppIDSvc" SERVICE_RUNNING(0x4L)>
26+
<Service "AppIDSvc" SERVICE_RUNNING(0x4)>
2727
name = AppIDSvc
2828
description = Application Identity
29-
status = ServiceStatus(type=48L, state=SERVICE_RUNNING(0x4L), control_accepted=5L, flags=0L)
30-
process = <WinProcess "!cannot-retrieve-name" pid 7076 at 0x631eb70>
29+
status = <_SERVICE_STATUS_PROCESS type=48L state=SERVICE_RUNNING(0x4)>
30+
process = <WinProcess "!cannot-retrieve-name" pid 6060 at 0x35851d0>
3131

3232
Enumerating handles:
33-
There are 78079 handles:
34-
First handle is: <Handle value=<0x4> in process pid=4>
33+
There are 208332 handles:
34+
First handle is: <HandleWow64 value=<0x4> in process pid=4>
3535
Enumerating handles of the current process:
36-
There are 260 handles for this process
36+
There are 275 handles for this process
3737
Looking for a File handle:
38-
Handle is <Handle value=<0x4> in process pid=10968>
38+
Handle is <HandleWow64 value=<0x4> in process pid=15236>
3939
Name is <\Device\ConDrv>
4040

4141
Dumping the first system module
42-
<SystemModuleWow64 name="\SystemRoot\system32\ntoskrnl.exe" base=0xfffff803f9697000>
42+
<SystemModuleWow64 name="\SystemRoot\system32\ntoskrnl.exe" base=0xfffff80023000000>
4343
ImageName = \SystemRoot\system32\ntoskrnl.exe
44-
Base = 0xfffff803f9697000
45-
Size = 0x8d5000
44+
Base = 0xfffff80023000000
45+
Size = 0xab7000
4646
Flags = 0x8804000
47-
LoadCount = 139
47+
LoadCount = 240

docs/source/service.rst

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Service
22
=======
33

4-
The list of services is accessible via :py:attr:`windows.system.services
4+
The services manager is accessible via :py:attr:`windows.system.services
55
<windows.winobject.system.System.services>`
66

77
.. note::
@@ -10,6 +10,18 @@ The list of services is accessible via :py:attr:`windows.system.services
1010

1111
.. module:: windows.winobject.service
1212

13-
.. autoclass:: Service
13+
14+
ServiceManager
15+
""""""""""""""
16+
17+
.. autoclass:: ServiceManager
1418
:show-inheritance:
19+
:inherited-members:
20+
:special-members: __getitem__, __iter__
21+
22+
23+
Service
24+
"""""""
25+
26+
.. autoclass:: Service
1527
:inherited-members:

windows/winobject/service.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,18 @@
3939

4040

4141
class ServiceManager(utils.AutoHandle):
42+
"""An object to query, list and explore services"""
4243
def _get_handle(self):
4344
return windows.winproxy.OpenSCManagerA(dwDesiredAccess=gdef.MAXIMUM_ALLOWED)
4445

4546
def open_service(self, name, access=gdef.MAXIMUM_ALLOWED):
4647
return windows.winproxy.OpenServiceA(self.handle, name, access) # Check service exists :)
4748

4849
def get_service(self, key, access=gdef.MAXIMUM_ALLOWED):
50+
"""Get a service by its name/index or a list of services via a slice
51+
52+
:return: :class:`Service` or [:class:`Service`] -- A :class:`Service` or list of :class:`Service`
53+
"""
4954
if isinstance(key, int_types):
5055
return self.enumerate_services()[key]
5156
if isinstance(key, slice):
@@ -59,6 +64,10 @@ def get_service(self, key, access=gdef.MAXIMUM_ALLOWED):
5964
return Service(name=key, handle=handle)
6065

6166
__getitem__ = get_service
67+
"""Get a service by its name/index or a list of services via a slice
68+
69+
:return: :class:`Service` or [:class:`Service`] -- A :class:`Service` or list of :class:`Service`
70+
"""
6271

6372
def get_service_display_name(self, name):
6473
# This API is strange..
@@ -70,6 +79,9 @@ def get_service_display_name(self, name):
7079
return result.value
7180

7281
def _enumerate_services_generator(self):
82+
"""The generator code behind __iter__.
83+
Allow to iter over the services on the system
84+
"""
7385
size_needed = gdef.DWORD()
7486
nb_services = gdef.DWORD()
7587
counter = gdef.DWORD()
@@ -93,12 +105,17 @@ def _enumerate_services_generator(self):
93105
return
94106

95107
__iter__ = _enumerate_services_generator
108+
"""Iter over the services on the system
109+
110+
:yield: :class:`Service`
111+
"""
96112

97113
def enumerate_services(self):
98114
return list(self._enumerate_services_generator())
99115

100116

101117
class Service(gdef.SC_HANDLE):
118+
"""Represent a service on the system"""
102119
def __init__(self, handle, name, description=None):
103120
super(Service, self).__init__(handle)
104121
self.name = name
@@ -149,6 +166,10 @@ def security_descriptor(self):
149166
return security.SecurityDescriptor.from_service(self.name)
150167

151168
def start(self, args=None):
169+
"""Start the service
170+
171+
:param args: a list of :class:`str`
172+
"""
152173
nbelt = 0
153174
if args is not None:
154175
if isinstance(args, windows.pycompat.anybuff):
@@ -158,6 +179,7 @@ def start(self, args=None):
158179
return windows.winproxy.StartServiceA(self, nbelt, args)
159180

160181
def stop(self):
182+
"""Stop the service"""
161183
status = SERVICE_STATUS()
162184
windows.winproxy.ControlService(self, gdef.SERVICE_CONTROL_STOP, status)
163185
return status

windows/winobject/system.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,10 @@ def logicaldrives(self):
6464

6565
@utils.fixedpropety
6666
def services(self):
67-
"""The list of services
67+
"""An object to query, list and explore services
6868
69-
:type: [:class:`~windows.winobject.service.ServiceA`] -- A list of Service"""
69+
:type: :class:`~windows.winobject.service.ServiceManager`
70+
"""
7071
return service.ServiceManager()
7172

7273
@property

0 commit comments

Comments
 (0)