6 out of 7 rated this helpful - Rate this topic

RegOpenCurrentUser function

Applies to: desktop apps only

Retrieves a handle to the HKEY_CURRENT_USER key for the user the current thread is impersonating.

Syntax

LONG WINAPI RegOpenCurrentUser(
  __in   REGSAM samDesired,
  __out  PHKEY phkResult
);

Parameters

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. When you no longer need the returned handle, call the RegCloseKey function to close it.

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

The HKEY_CURRENT_USER key maps to the root of the current user's branch in the HKEY_USERS key. It is cached for all threads in a process. Therefore, this value does not change when another user's profile is loaded. RegOpenCurrentUser uses the thread's token to access the appropriate key, or the default if the profile is not loaded.

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

See also

RegCloseKey
Registry Functions
Registry Overview

 

 

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
If there is no thread token.

If there is no thread token, this function does fall back to the process token.

Just in case anyone wonders why you may need this, since why not just use HKCU, well, I had a case where I needed to redirect registry entries being written to HKLM to HKCU. RegOverridePredefKey doesn't allow you to override one predefined key with another, so instead I used this instead. It is either that or get the user's SID from the process token and open it up that way.

Consider using RegDisablePredefinedCache instead

Although there may be some cases where this is the ideal approach, it seems quite likely that in many cases what you'd rather use instead of RegOpenCurrentUser everywhere is a single call in your service to RegDisablePredefinedCache or RegDisablePredefinedCacheEx.

Disabling the caching overall is much safer than relying on your ability to remember to never access HKCU through the RegOpenKey and RegCreateKey APIs directly.