July 2002
Intermediate to advanced
608 pages
15h 46m
English
Credit: Andy McKay
You need to find the actual name of a Windows service from the longer display name, which is all that many programs show you.
Unfortunately, a Windows service has two names: a real one (to be used in many operations) and a display name (meant for human display). Fortunately, Python helps you translate between them:
import win32api
import win32con
def GetShortName(longName):
# Looks up a service's real name from its display name
hkey = win32api.RegOpenKey(win32con.HKEY_LOCAL_MACHINE,
"SYSTEM\\CurrentControlSet\\Services", 0, win32con.KEY_ALL_ACCESS)
num = win32api.RegQueryInfoKey(hkey)[0]
# Loop through the given number of subkeys
for x in range(0, num):
# Find service name; open subkey
svc = win32api.RegEnumKey(hkey, x)
skey = win32api.RegOpenKey(win32con.HKEY_LOCAL_MACHINE,
"SYSTEM\\CurrentControlSet\\Services\\%s" % svc,
0, win32con.KEY_ALL_ACCESS)
try:
# Find short name
shortName = str(win32api.RegQueryValueEx(skey, "DisplayName")[0])
if shortName == longName:
return svc
except win32api.error:
# in case there is no key called DisplayName
pass
return None
if _ _name_ _=='_ _main_ _':
assert(GetShortName('Windows Time') == 'W32Time')
assert(GetShortName('FoobarService') == None)Many programs show only the long description (display name) of a Windows Service, such as Windows Time, but you need the actual service name to operate on the service itself (to restart it, for example). This ...
Read now
Unlock full access