RegOpenKeyEx Function
RegOpenKeyEx Function

Opens the specified registry key. Note that key names are not case sensitive.

To perform transacted registry operations on a key, call the RegOpenKeyTransacted function.

Syntax

C++
LONG WINAPI RegOpenKeyEx(
  __in        HKEY hKey,
  __in_opt    LPCTSTR lpSubKey,
  __reserved  DWORD ulOptions,
  __in        REGSAM samDesired,
  __out       PHKEY phkResult
);

Parameters

hKey [in]

A handle to an open registry key. This handle is returned by the RegCreateKeyEx or RegOpenKeyEx function, or it can be one of the following predefined keys:

HKEY_CLASSES_ROOT
HKEY_CURRENT_USER
HKEY_LOCAL_MACHINE
HKEY_USERS
lpSubKey [in, optional]

The name of the registry subkey to be opened.

Key names are not case sensitive.

If this parameter is NULL or a pointer to an empty string, the function will open a new handle to the key identified by the hKey parameter.

For more information, see Registry Element Size Limits.

ulOptions

This parameter is reserved and must be zero.

samDesired [in]

A mask that specifies the desired access rights to the key. The function fails if the security descriptor of the key does not permit the requested access for the calling process. For more information, see Registry Key Security and Access Rights.

phkResult [out]

A pointer to a variable that receives a handle to the opened key. If the key is not one of the predefined registry keys, call the RegCloseKey function after you have finished using the handle.

Return Value

If the function succeeds, the return value is ERROR_SUCCESS.

If the function fails, the return value is a nonzero error code defined in Winerror.h. You can use the FormatMessage function with the FORMAT_MESSAGE_FROM_SYSTEM flag to get a generic description of the error.

Remarks

Unlike the RegCreateKeyEx function, the RegOpenKeyEx function does not create the specified key if the key does not exist in the registry.

If your service or application impersonates different users, do not use this function with HKEY_CURRENT_USER. Instead, call the RegOpenCurrentUser function.

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 Deleting a Key with Subkeys.

Requirements

Minimum supported clientWindows 2000 Professional
Minimum supported serverWindows 2000 Server
HeaderWinreg.h (include Windows.h)
LibraryAdvapi32.lib
DLLAdvapi32.dll
Unicode and ANSI namesRegOpenKeyExW (Unicode) and RegOpenKeyExA (ANSI)

See Also

RegCloseKey
RegCreateKeyEx
RegDeleteKey
Registry Functions
Registry Overview
RegOpenKeyTransacted

Send comments about this topic to Microsoft

Build date: 11/19/2009

Community Content   What is Community Content?
Add new content RSS  Annotations
Keys are created.      rhowen   |   Edit   |   Show History

RegOpenKeyEx does, in fact, create a non-existing key and returns 0 (ERROR_SUCCESS?).

LONG lRegResult = RegOpenKeyEx(HKEY_CLASSES_ROOT, m_RegSubKey, 0, KEY_QUERY_VALUE + KEY_WRITE, &hkRegistry);

Successfully opens or creates m_RegSubKey under HKEY_CLASSES_ROOT.

Tags What's this?: Add a tag
Flag as ContentBug
RegCreateKeyEx creates intermediate keys      Gideon7   |   Edit   |   Show History
Note: RegCreateKeyEx("woz\foo\baz") will create the ancestor keys "woz" and "foo" if they do not exist. This means that if (for example) an .MSI installer file contains a typo for an ancestor key it will inadvertently create a whole new branch.
Tags What's this?: Add a tag
Flag as ContentBug
samDesired is not enforced on the key, just checked on open      DaveMethvin   |   Edit   |   Show History
If you open a key with samDesired=KEY_READ, it does not prevent the HKEY returned in phkResult being used for operations that modify the registry. For example, a KEY_READ key returned by RegOpenKeyEx can be passed as the hKey argument to RegCreateKeyEx and it WILL create a key. In contrast, RegSetValueEx specifically requires a key opened with KEY_SET_VALUE rights.



Tags What's this?: Add a tag
Flag as ContentBug
If combined with KEY_WOW64_32KEY this function will fail under Windows 2000      luxspes   |   Edit   |   Show History
This call works perfectly in Windows 2000 and XP:

lResult = RegOpenKeyEx (hKeyRoot, lpSubKey, 0, KEY_READ, &hKey);

But this one fails in Windows 2000

lResult = RegOpenKeyEx (hKeyRoot, lpSubKey, 0, KEY_READ|KEY_WOW64_32KEY, &hKey);

The difference? The KEY_WOW64_32KEY:

http://msdn.microsoft.com/en-us/library/ms724878%28VS.85%29.aspx
Processing
Page view tracker