# 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`

RegOpenKeyEx opens a key in the Windows registry. The handle it returns must be used when you read to or write from any values under that key. 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 one of the registry base key flags that the desired key is under.
lpSubKey
  The name of the key to open.
ulOptions
  Reserved. Set to 0.
samDesired
  One or more of the registry access flags specifying the read/write access desired.
phkResult
  Receives the handle to the registry key.

Example:

    ' Read the Data value under the key:
    ' HKEY_CURRENT_USER\Software\Dummy\DummyApp\1.0\
    subkey = "Software\Dummy\DummyApp\1.0"  ' key name
    x = RegOpenKeyEx(HKEY_CURRENT_USER, subkey, 0, KEY_READ, hregkey)
    If x <> 0 Then  ' error opening key -- abort!
      Debug.Print "Could not open registry key."
      Exit Sub
    End If
    '(put rest of code here)
    x = RegCloseKey(hregkey)

Related Calls: RegCloseKey, RegCreateKeyEx
Category: Registry
Back to the index.