The following code works well in Python 2:
import ctypes
def test():
OpenSCManager = ctypes.windll.advapi32.OpenSCManagerA
CloseServiceHandle = ctypes.windll.advapi32.CloseServiceHandle
handle = OpenSCManager(None, None, 0)
print(hex(handle))
assert handle, ctypes.GetLastError()
assert CloseServiceHandle(handle), ctypes.GetLastError()
test()
It does not work in Python 3:
0x40d88f90
Traceback (most recent call last):
File ".\test1.py", line 12, in <module>
test()
File ".\test1.py", line 10, in test
assert CloseServiceHandle(handle), ctypes.GetLastError()
AssertionError: 6
6 means invalid handle.
It seems that in addition, the handles retrieved in Python 2 are smaller numbers, such as 0x100ffc0. It isn't something specific with CloseServiceHandle. This handle cannot be used with any service function.
Both Python versions are 64 bit native Windows Python.