def __FindSvcDeps(findName): if isinstance(findName, pywintypes.UnicodeType): findName = str(findName) dict = {} k = win32api.RegOpenKey(win32con.HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Services") num = 0 while True: try: svc = win32api.RegEnumKey(k, num) except win32api.error: break num = num + 1 sk = win32api.RegOpenKey(k, svc) try: deps, typ = win32api.RegQueryValueEx(sk, "DependOnService") except win32api.error: deps = () for dep in deps: dep = dep.lower() dep_on = dict.get(dep, []) dep_on.append(svc) dict[dep] = dep_on return __ResolveDeps(findName, dict)
def EnumTlbs(excludeFlags=0): """Return a list of TypelibSpec objects, one for each registered library. """ key = win32api.RegOpenKey(win32con.HKEY_CLASSES_ROOT, "Typelib") iids = EnumKeys(key) results = [] for iid, crap in iids: try: key2 = win32api.RegOpenKey(key, str(iid)) except win32api.error: # A few good reasons for this, including "access denied". continue for version, tlbdesc in EnumKeys(key2): major_minor = version.split('.', 1) if len(major_minor) < 2: major_minor.append('0') # For some reason, this code used to assume the values were hex. # This seems to not be true - particularly for CDO 1.21 # *sigh* - it appears there are no rules here at all, so when we need # to know the info, we must load the tlb by filename and request it. # The Resolve() method on the TypelibSpec does this. # For this reason, keep the version numbers as strings - that # way we can't be wrong! Let code that really needs an int to work # out what to do. FWIW, http://support.microsoft.com/kb/816970 is # pretty clear that they *should* be hex. major = major_minor[0] minor = major_minor[1] key3 = win32api.RegOpenKey(key2, str(version)) try: # The "FLAGS" are at this point flags = int(win32api.RegQueryValue(key3, "FLAGS")) except (win32api.error, ValueError): flags = 0 if flags & excludeFlags == 0: for lcid, crap in EnumKeys(key3): try: lcid = int(lcid) except ValueError: # not an LCID entry continue # Only care about "{lcid}\win32" key - jump straight there. try: key4 = win32api.RegOpenKey(key3, "%s\\win32" % (lcid,)) except win32api.error: continue try: dll, typ = win32api.RegQueryValueEx(key4, None) if typ == win32con.REG_EXPAND_SZ: dll = win32api.ExpandEnvironmentStrings(dll) except win32api.error: dll = None spec = TypelibSpec(iid, lcid, major, minor, flags) spec.dll = dll spec.desc = tlbdesc spec.ver_desc = tlbdesc + " (" + version + ")" results.append(spec) return results
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()
def find_pdh_counter_localized_name(english_name, machine_name=None): if not counter_english_map: from win32 import api as win32api from win32 import constants as win32con counter_reg_value = win32api.RegQueryValueEx( win32con.HKEY_PERFORMANCE_DATA, "Counter 009") counter_list = counter_reg_value[0] for i in range(0, len(counter_list) - 1, 2): try: counter_id = int(counter_list[i]) except ValueError: continue counter_english_map[counter_list[i + 1].lower()] = counter_id return win32pdh.LookupPerfNameByIndex( machine_name, counter_english_map[english_name.lower()])
def GetServiceCustomOption(serviceName, option, defaultValue=None): # First param may also be a service class/instance. # This allows services to pass "self" try: serviceName = serviceName._svc_name_ except AttributeError: pass key = win32api.RegCreateKey( win32con.HKEY_LOCAL_MACHINE, "System\\CurrentControlSet\\Services\\%s\\Parameters" % serviceName) try: try: return win32api.RegQueryValueEx(key, option)[0] except win32api.error: # No value. return defaultValue finally: win32api.RegCloseKey(key)
def FormatMessage(eventLogRecord, logType="Application"): """Given a tuple from ReadEventLog, and optionally where the event record came from, load the message, and process message inserts. Note that this function may raise win32api.error. See also the function SafeFormatMessage which will return None if the message can not be processed. """ # From the event log source name, we know the name of the registry # key to look under for the name of the message DLL that contains # the messages we need to extract with FormatMessage. So first get # the event log source name... keyName = "SYSTEM\\CurrentControlSet\\Services\\EventLog\\%s\\%s" % ( logType, eventLogRecord.SourceName) # Now open this key and get the EventMessageFile value, which is # the name of the message DLL. handle = win32api.RegOpenKey(win32con.HKEY_LOCAL_MACHINE, keyName) try: dllNames = win32api.RegQueryValueEx(handle, "EventMessageFile")[0].split(";") # Win2k etc appear to allow multiple DLL names data = None for dllName in dllNames: try: # Expand environment variable strings in the message DLL path name, # in case any are there. dllName = win32api.ExpandEnvironmentStrings(dllName) dllHandle = win32api.LoadLibraryEx( dllName, 0, win32con.LOAD_LIBRARY_AS_DATAFILE) try: data = win32api.FormatMessageW( win32con.FORMAT_MESSAGE_FROM_HMODULE, dllHandle, eventLogRecord.EventID, langid, eventLogRecord.StringInserts) finally: win32api.FreeLibrary(dllHandle) except win32api.error: pass # Not in this DLL - try the next if data is not None: break finally: win32api.RegCloseKey(handle) return data or '' # Don't want "None" ever being returned.
def _GetServiceShortName(longName): # looks up a services name # from the display name # Thanks to Andy McKay for this code. access = win32con.KEY_READ | win32con.KEY_ENUMERATE_SUB_KEYS | win32con.KEY_QUERY_VALUE hkey = win32api.RegOpenKey(win32con.HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Services", 0, access) num = win32api.RegQueryInfoKey(hkey)[0] longName = longName.lower() # loop through number of subkeys for x in range(0, num): # find service name, open subkey svc = win32api.RegEnumKey(hkey, x) skey = win32api.RegOpenKey(hkey, svc, 0, access) try: # find display name thisName = str(win32api.RegQueryValueEx(skey, "DisplayName")[0]) if thisName.lower() == longName: return svc except win32api.error: # in case there is no key called DisplayName pass return None
def GetSubList(self): clsidstr, versionStr = self.myobject collected = [] helpPath = "" key = win32api.RegOpenKey(win32con.HKEY_CLASSES_ROOT, "TypeLib\\%s\\%s" % (clsidstr, versionStr)) win32ui.DoWaitCursor(1) try: num = 0 while True: try: subKey = win32api.RegEnumKey(key, num) except win32api.error: break hSubKey = win32api.RegOpenKey(key, subKey) try: value, typ = win32api.RegQueryValueEx(hSubKey, None) if typ == win32con.REG_EXPAND_SZ: value = win32api.ExpandEnvironmentStrings(value) except win32api.error: value = "" if subKey == "HELPDIR": helpPath = value elif subKey == "Flags": flags = value else: try: lcid = int(subKey) lcidkey = win32api.RegOpenKey(key, subKey) # Enumerate the platforms lcidnum = 0 while True: try: platform = win32api.RegEnumKey( lcidkey, lcidnum) except win32api.error: break try: hplatform = win32api.RegOpenKey( lcidkey, platform) fname, typ = win32api.RegQueryValueEx( hplatform, None) if typ == win32con.REG_EXPAND_SZ: fname = win32api.ExpandEnvironmentStrings( fname) except win32api.error: fname = "" collected.append((lcid, platform, fname)) lcidnum = lcidnum + 1 win32api.RegCloseKey(lcidkey) except ValueError: pass num = num + 1 finally: win32ui.DoWaitCursor(0) win32api.RegCloseKey(key) # Now, loop over my collected objects, adding a TypeLib and a HelpFile ret = [] # if helpPath: ret.append(browser.MakeHLI(helpPath, "Help Path")) ret.append(HLICLSID(clsidstr)) for lcid, platform, fname in collected: extraDescs = [] if platform != "win32": extraDescs.append(platform) if lcid: extraDescs.append("locale=%s" % lcid) extraDesc = "" if extraDescs: extraDesc = " (%s)" % ", ".join(extraDescs) ret.append(HLITypeLib(fname, "Type Library" + extraDesc)) ret.sort() return ret