def subkeys(key): i = 0 while True: try: subkey = _winreg.EnumKey(key, i) yield subkey i += 1 except WindowsError: # pylint: disable=E0602 break
def list_keys(hive, key=None, use_32bit_registry=False): ''' Enumerates the subkeys in a registry key or hive. :param str hive: The name of the hive. Can be one of the following - HKEY_LOCAL_MACHINE or HKLM - HKEY_CURRENT_USER or HKCU - HKEY_USER or HKU :param str key: The key (looks like a path) to the value name. If a key is not passed, the keys under the hive will be returned. :param bool use_32bit_registry: Accesses the 32bit portion of the registry on 64 bit installations. On 32bit machines this is ignored. :return: A list of keys/subkeys under the hive or key. :rtype: list CLI Example: .. code-block:: bash salt '*' reg.list_keys HKLM 'SOFTWARE' ''' if PY2: local_hive = _mbcs_to_unicode(hive) local_key = _unicode_to_mbcs(key) else: local_hive = hive local_key = key registry = Registry() hkey = registry.hkeys[local_hive] access_mask = registry.registry_32[use_32bit_registry] subkeys = [] try: handle = _winreg.OpenKey(hkey, local_key, 0, access_mask) for i in range(_winreg.QueryInfoKey(handle)[0]): subkey = _winreg.EnumKey(handle, i) if PY2: subkeys.append(_mbcs_to_unicode(subkey)) else: subkeys.append(subkey) handle.Close() except WindowsError as exc: # pylint: disable=E0602 log.debug(exc) log.debug('Cannot find key: {0}\\{1}'.format(hive, key)) return False, 'Cannot find key: {0}\\{1}'.format(hive, key) return subkeys
def _get_product_information(reg_hive, reg_key, reg_handle): # This is a list of default OS reg entries that don't seem to be installed # software and no version information exists on any of these items # Also, some MS Office updates don't register a product name which means # their information is useless. ignore_list = ['AddressBook', 'Connection Manager', 'DirectDrawEx', 'Fontcore', 'IE40', 'IE4Data', 'IE5BAKEX', 'IEData', 'MobileOptionPack', 'SchedulingAgent', 'WIC', 'Not Found'] encoding = locale.getpreferredencoding() products = {} try: i = 0 while True: product_key = None asubkey = _winreg.EnumKey(reg_handle, i) rd_uninst_key = "\\".join([reg_key, asubkey]) displayName = '' displayVersion = '' try: # these are not garuanteed to exist product_key = _open_registry_key(reg_hive, rd_uninst_key) displayName, value_type = _winreg.QueryValueEx(product_key, "DisplayName") try: displayName = displayName.decode(encoding) except Exception: pass displayVersion, value_type = _winreg.QueryValueEx(product_key, "DisplayVersion") except Exception: pass if product_key is not None: _winreg.CloseKey(product_key) i += 1 if displayName not in ignore_list: # some MS Office updates don't register a product name which means # their information is useless if displayName != '': products[displayName] = displayVersion except WindowsError: # pylint: disable=E0602 pass return products
def _subkeys(_key): ''' Enumerate keys ''' i = 0 while True: try: subkey = _winreg.EnumKey(_key, i) yield subkey i += 1 except WindowsError: # pylint: disable=E0602 break