def SmartOpenService(hscm, name, access): try: return win32service.OpenService(hscm, name, access) except win32api.error as details: if details.winerror not in [winerror.ERROR_SERVICE_DOES_NOT_EXIST, winerror.ERROR_INVALID_NAME]: raise name = win32service.GetServiceKeyName(hscm, name) return win32service.OpenService(hscm, name, access)
# in case there is no key called DisplayName pass return None # Open a service given either it's long or short name. def SmartOpenService(hscm, name, access): try: return win32service.OpenService(hscm, name, access) except win32api.error, details: if details.winerror not in [ winerror.ERROR_SERVICE_DOES_NOT_EXIST, winerror.ERROR_INVALID_NAME ]: raise name = win32service.GetServiceKeyName(hscm, name) return win32service.OpenService(hscm, name, access) def LocateSpecificServiceExe(serviceName): # Given the name of a specific service, return the .EXE name _it_ uses # (which may or may not be the Python Service EXE hkey = win32api.RegOpenKey( win32con.HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Services\\%s" % (serviceName), 0, win32con.KEY_ALL_ACCESS) try: return win32api.RegQueryValueEx(hkey, "ImagePath")[0] finally: hkey.Close()