Enumerates the subkeys in a registry key or hive.

    Args:

       hive (str):
            The name of the hive. Can be one of the following:

                - HKEY_LOCAL_MACHINE or HKLM
                - HKEY_CURRENT_USER or HKCU
                - HKEY_USERS or HKU
                - HKEY_CLASSES_ROOT or HKCR
                - HKEY_CURRENT_CONFIG or HKCC

        key (str):
            The key (looks like a path) to the value name. If a key is not
            passed, the keys under the hive will be returned.

        use_32bit_registry (bool):
            Accesses the 32bit portion of the registry on 64 bit installations.
            On 32bit machines this is ignored.

    Returns:
        list: A list of keys/subkeys under the hive or key.

    Usage:

        .. code-block:: python

            import salt.utils.win_reg
            winreg.list_keys(hive='HKLM', key='SOFTWARE\\Microsoft')

def list_keys(hive, key=None, use_32bit_registry=False):
    '''
    Enumerates the subkeys in a registry key or hive.

    Args:

       hive (str):
            The name of the hive. Can be one of the following:

                - HKEY_LOCAL_MACHINE or HKLM
                - HKEY_CURRENT_USER or HKCU
                - HKEY_USERS or HKU
                - HKEY_CLASSES_ROOT or HKCR
                - HKEY_CURRENT_CONFIG or HKCC

        key (str):
            The key (looks like a path) to the value name. If a key is not
            passed, the keys under the hive will be returned.

        use_32bit_registry (bool):
            Accesses the 32bit portion of the registry on 64 bit installations.
            On 32bit machines this is ignored.

    Returns:
        list: A list of keys/subkeys under the hive or key.

    Usage:

        .. code-block:: python

            import salt.utils.win_reg
            winreg.list_keys(hive='HKLM', key='SOFTWARE\\Microsoft')
    '''

    local_hive = _to_unicode(hive)
    local_key = _to_unicode(key)

    registry = Registry()
    try:
        hkey = registry.hkeys[local_hive]
    except KeyError:
        raise CommandExecutionError('Invalid Hive: {0}'.format(local_hive))
    access_mask = registry.registry_32[use_32bit_registry]

    subkeys = []
    handle = None
    try:
        handle = win32api.RegOpenKeyEx(hkey, local_key, 0, access_mask)

        for i in range(win32api.RegQueryInfoKey(handle)[0]):
            subkey = win32api.RegEnumKey(handle, i)
            if PY2:
                subkeys.append(_to_mbcs(subkey))
            else:
                subkeys.append(subkey)

    except Exception:  # pylint: disable=E0602
        log.debug(r'Cannot find key: %s\%s', hive, key, exc_info=True)
        return False, r'Cannot find key: {0}\{1}'.format(hive, key)
    finally:
        if handle:
            handle.Close()

    return subkeys