RegEnumKey(HKEY,DWORD,LPTSTR,DWORD) function [Base]

The RegEnumKey function enumerates subkeys of the specified open registry key. The function retrieves the name of one subkey each time it is called.

Note  This function is provided only for compatibility with 16-bit versions of Windows. Applications should use the RegEnumKeyEx function.

LONG RegEnumKey(
  HKEY hKey,
  DWORD dwIndex,
  LPTSTR lpName,
  DWORD cchName


[in] Handle to an open key. The key must have been opened with the KEY_ENUMERATE_SUB_KEYS access right. For more information, see Registry Key Security and Access Rights.

This handle is returned by the RegCreateKeyEx or RegOpenKeyEx function, or it can be one of the following predefined keys:


Windows Me/98/95:  This parameter can also be the following value:


[in] Index of the subkey of hKey to be retrieved. This value should be zero for the first call to the RegEnumKey function and then incremented for subsequent calls.

Because subkeys are not ordered, any new subkey will have an arbitrary index. This means that the function may return subkeys in any order.

[out] Pointer to a buffer that receives the name of the subkey, including the terminating null character. This function copies only the name of the subkey, not the full key hierarchy, to the buffer.

For more information, see Registry Element Size Limits.

[in] Size of the buffer pointed to by the lpName parameter, in TCHARs. To determine the required buffer size, use the RegQueryInfoKey function to determine the size of the largest subkey for the key identified by the hKey parameter.

Return Values

If the function succeeds, the return value is ERROR_SUCCESS.

If the function fails, the return value is a system error code. If there are no more subkeys available, the function returns ERROR_NO_MORE_ITEMS.

Windows Server 2003 and Windows XP/2000/NT:  If the lpName buffer is too small to receive the name of the key, the function returns ERROR_MORE_DATA.


To enumerate subkeys, an application should initially call the RegEnumKey function with the dwIndex parameter set to zero. The application should then increment the dwIndex parameter and call the RegEnumKey function until there are no more subkeys (meaning the function returns ERROR_NO_MORE_ITEMS).

The application can also set dwIndex to the index of the last key on the first call to the function and decrement the index until the subkey with index 0 is enumerated. To retrieve the index of the last subkey, use the RegQueryInfoKey.

While an application is using the RegEnumKey function, it should not make calls to any registration functions that might change the key being queried.


Client Requires Windows XP, Windows 2000 Professional, Windows NT Workstation, Windows Me, Windows 98, or Windows 95.
Server Requires Windows Server 2003, Windows 2000 Server, or Windows NT Server.

Declared in Winreg.h; include Windows.h.


Link to Advapi32.lib.

DLL Requires Advapi32.dll.

Implemented as RegEnumKeyW (Unicode) and RegEnumKeyA (ANSI). Note that Unicode support on Windows Me/98/95 requires Microsoft Layer for Unicode.

See Also

Registry Overview, Registry Functions, RegCloseKey, RegCreateKeyEx, RegDeleteKey, RegEnumKeyEx, RegOpenKeyEx, RegQueryInfoKey