RegOpenKeyEx Function

Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) As Long

Platforms: Win 32s, Win 95/98, Win NT

RegOpenKeyEx opens a key in the Windows registry. The handle it gives must be used when you read to or write from any values under that key. Unlike RegCreateKeyEx, this function will not create the key if it does not exist. The function puts a handle to the opened key into the variable passed as phkResult. The function returns 0 if successful, or a non-zero value error code if an error occured.

hKey
Either the handle to an open registry key or exactly one of the following flags that the desired key is under:
HKEY_CURRENT_USER = &H80000001
The HKEY_CURRENT_USER base key, which stores program information for the current user.
HKEY_LOCAL_MACHINE = &H80000002
The HKEY_LOCAL_MACHINE base key, which stores program information for all users.
HKEY_USERS = &H80000003
The HKEY_USERS base key, which has all the information for any user (not just the one provided by HKEY_CURRENT_USER).
HKEY_CURRENT_CONFIG = &H80000005
The HKEY_CURRENT_CONFIG base key, which stores computer configuration information.
HKEY_DYN_DATA = &H80000006
The HKEY_DYN_DATA base key, which stores dynamic data.
lpSubKey
The name of the key to open.
ulOptions
Reserved. Set to 0.
samDesired
One or more of the following flags specifying the desired read/write access:
KEY_ALL_ACCESS = &HF003F
Permission for all types of access.
KEY_CREATE_LINK = &H20
Permission to create symbolic links.
KEY_CREATE_SUB_KEY = &H4
Permission to create subkeys.
KEY_ENUMERATE_SUB_KEYS = &H8
Permission to enumerate subkeys.
KEY_EXECUTE = &H20019
Same as KEY_READ.
KEY_NOTIFY = &H10
Permission to give change notification.
KEY_QUERY_VALUE = &H1
Permission to query subkey data.
KEY_READ = &H20019
Permission for general read access.
KEY_SET_VALUE = &H2
Permission to set subkey data.
KEY_WRITE = &H20006
Permission for general write access.
phkResult
Receives the handle to the registry key.

Example:

' Open a key called HKEY_CURRENT_USER\Software\MyCorp\MyProgram\Config.
' Then create a "username" value under that key and set its value to "Rimmer".
Dim hregkey As Long  ' receives handle to the opened registry key
Dim subkey As String  ' name of the subkey to create
Dim retval As Long  ' return value

' Set the name of the new key and the default security settings
subkey = "Software\MyCorp\MyProgram\Config"

' Open the registry key
retval = RegOpenKeyEx(HKEY_CURRENT_USER, subkey, 0, KEY_WRITE, hregkey)
If retval <> 0 Then  ' error during open
  Debug.Print "Error opening registry key -- aborting."
  End  ' terminate the program
End If

' Insert rest of code here.....

' Close the registry key
retval = RegCloseKey(hregkey)

See Also: RegCloseKey, RegCreateKeyEx
Category: Registry

Go back to the alphabetical Function listing.
Go back to the Reference section index.


This page is copyright © 2000 Paul Kuliniewicz. Copyright Information.
Go back to the Windows API Guide home page.
E-mail: vbapi@vbapi.com Send Encrypted E-Mail
This page is at http://www.vbapi.com/ref/r/regopenkeyex.html