winapi - Obtaining Windows service handle in Python -
i need work services on windows (10) python (version 3). before starting , stopping them, need obtaing service handle. first, obtain scmanager handle (successful), , after trying obtain service handle via openservice
(in example "dhcp" service). code:
from ctypes import * ctypes.wintypes import * openscmanager = windll.advapi32.openscmanagera openservice = windll.advapi32.openservicea getlasterror = windll.kernel32.getlasterror sc_manager_all_access = dword(0xf003f) service_all_access = dword(0xf01ff) name = "dhcp" sc_manager_h = openscmanager(none, none, sc_manager_all_access) if sc_manager_h == 0: raise exception(getlasterror()) else: print(sc_manager_h) # <-- ok, no exception, printed number service_h = openservice(sc_manager_h, name.encode("ascii"), service_all_access) if service_h == 0: raise exception(getlasterror()) # <-- here raised exception, getlasterror 6 (error_invalid_handle) else: print(service_h) print(getlasterror())
user (i ran under administrator), runs code, have right (i tried run under user without rights, , openscmanager
raised correct error). i'm sure not problem name or service asscess constant, there getlasterror
codes that. service such name ("dhcp" in case) exist. seems somehow pass scmanager handle openservice
in wrong way, don't know how else.
documentation on openservice
: https://msdn.microsoft.com/en-us/library/windows/desktop/ms684330(v=vs.85).aspx
update 1
as @conio suggested, tried equivalent c code, , works well. tried run code above on next machines\interpreters:
- windows xp, python 3.4.3: works well,
service_h
not 0, getlasterror 0; - windows 7, python 3.4.3: works well,
service_h
not 0, getlasterror 0; - windows 7 (same machine previous), python 3.6.2: works wrong,
service_h
not 0, getlasterror 6, ,service_h
can used other service functions,startservice
- windows 10, python 3.4.3: works well,
service_h
not 0, getlasterror 0; - windows 10 (same machine previous), python 3.6.2: works wrong,
service_h
0, getlasterror 6, exception raised.
summarize tests:
- python 3.4.3 works well;
- python 3.6.2 works wrong, set getlasterror 6, return 0
openservice
, error raises in code above, , return correct handle, can used in other service functionsstartservicea
, , functions works (at least tried start service , starts).
Comments
Post a Comment