11 out of 15 rated this helpful - Rate this topic

RegEnumKeyEx function

Applies to: desktop apps only

Enumerates the subkeys of the specified open registry key. The function retrieves information about one subkey each time it is called.

Syntax

LONG WINAPI RegEnumKeyEx(
  __in         HKEY hKey,
  __in         DWORD dwIndex,
  __out        LPTSTR lpName,
  __inout      LPDWORD lpcName,
  __reserved   LPDWORD lpReserved,
  __inout      LPTSTR lpClass,
  __inout_opt  LPDWORD lpcClass,
  __out_opt    PFILETIME lpftLastWriteTime
);

Parameters

hKey [in]

A handle to an open registry 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, RegCreateKeyTransacted, RegOpenKeyEx, or RegOpenKeyTransacted function. It can also be one of the following predefined keys:

HKEY_CLASSES_ROOT
HKEY_CURRENT_CONFIG
HKEY_CURRENT_USER
HKEY_LOCAL_MACHINE
HKEY_PERFORMANCE_DATA
HKEY_USERS
dwIndex [in]

The index of the subkey to retrieve. This parameter should be zero for the first call to the RegEnumKeyEx 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.

lpName [out]

A pointer to a buffer that receives the name of the subkey, including the terminating null character. The function copies only the name of the subkey, not the full key hierarchy, to the buffer. If the function fails, no information is copied to this buffer.

For more information, see Registry Element Size Limits.

lpcName [in, out]

A pointer to a variable that specifies the size of the buffer specified by the lpName parameter, in characters. This size should include the terminating null character. If the function succeeds, the variable pointed to by lpcName contains the number of characters stored in the buffer, not including the terminating null character.

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.

lpReserved

This parameter is reserved and must be NULL.

lpClass [in, out]

A pointer to a buffer that receives the user-defined class of the enumerated subkey. This parameter can be NULL.

lpcClass [in, out, optional]

A pointer to a variable that specifies the size of the buffer specified by the lpClass parameter, in characters. The size should include the terminating null character. If the function succeeds, lpcClass contains the number of characters stored in the buffer, not including the terminating null character. This parameter can be NULL only if lpClass is NULL.

lpftLastWriteTime [out, optional]

A pointer to FILETIME structure that receives the time at which the enumerated subkey was last written. This parameter can be NULL.

Return value

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.

If the lpName buffer is too small to receive the name of the key, the function returns ERROR_MORE_DATA.

Remarks

To enumerate subkeys, an application should initially call the RegEnumKeyEx function with the dwIndex parameter set to zero. The application should then increment the dwIndex parameter and call RegEnumKeyEx 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 subkey on the first call to the function and decrement the index until the subkey with the index 0 is enumerated. To retrieve the index of the last subkey, use the RegQueryInfoKey function.

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

Note that operations that access certain registry keys are redirected. For more information, see Registry Virtualization and 32-bit and 64-bit Application Data in the Registry.

Examples

For an example, see Enumerating Registry Subkeys.

Requirements

Minimum supported client

Windows 2000 Professional

Minimum supported server

Windows 2000 Server

Header

Winreg.h (include Windows.h)

Library

Advapi32.lib

DLL

Advapi32.dll

Unicode and ANSI names

RegEnumKeyExW (Unicode) and RegEnumKeyExA (ANSI)

See also

FILETIME
RegCreateKeyEx
RegDeleteKey
Registry Functions
Registry Overview
RegOpenKeyEx
RegQueryInfoKey

 

 

Send comments about this topic to Microsoft

Build date: 3/6/2012

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Class name buffer length?

How do I know if the class name buffer is too small?

It' currently not documented that the function returns ERROR_MORE_DATA and the required character count in lpcClass.

Although, the "Registry Element Size Limits" documentation also does not state anything about the maximum length of a registry key class.

A key name can contain 256 characters without the terminating null character
I explained the issue in a blog post: $0$0 $0http://www.sepago.de/e/holger/2010/07/20/how-long-can-a-registry-key-name-really-be$0 $0 $0
Start at highest index if you are deleting reg keys as you go
If you are deleting child keys as you call RegEnumKeyEx, start at the highest index (discovered by RegQueryInfoKey).  If you start at zero, RegEnumKeyEx will return ERROR_NO_MORE_ITEMS before all keys are deleted.

Do not recurse into Wow6432Node in 32-bit code

In 32-bit code on a 64-bit system do not recurse into HKLM\Software\Wow6432Node. It acts like a symbolic link that loops back to HKLM\Software in the same 32-bit hive. It does not map into the 'other' hive (64-bit) as you might think. The result is infinite recursion: HKLM\Software\Wow6432Node\Wow6432Node\Wow6432Node\..., etc. (All of these are the same 32-bit key.)

You should ignore any result from RegEnumKeyEx that returns "Wow6432Node". It is a magic name that triggers special behavior by the registry API. See http://msdn2.microsoft.com/en-us/library/ms724072.aspx

Windows Server 2008: In the 32-bit hive the registry key HKLM\Software\Wow6432Node is hidden from RegEnumKeyEx. This fixes the infinite recursion bug described above. The hidden key still exists and the infinite recursion can still happen, but only if you explicitly open it.

WOW64 RegEnumKeyEx does not enumerate shared keys

Note: If RegEnumKeyEx is called on the 32-bit view of the registry it will sometimes omit registry keys that are shared between the 32-bit and 64-bit registry hives. For example, the enumeration of HKLM\Software\Microsoft\Windows\CurrentVersion will not list the child keys 'Group Policy', 'Policies', and 'Tablet PC'.

The workaround is to open the parent handle using KEY_WOW64_64KEY.