Declare Function RegEnumValue Lib "advapi32.dll" Alias "RegEnumValueA" (ByVal hkey As Long, ByVal dwIndex As Long, ByVal lpValueName As String, lpcbValueName As Long, lpReserved As Long, lpType As Long, lpData As Byte, lpcbData As Long) As Long
Platforms: Win 32s, Win 95/98, Win NT
RegEnumValues enumerates all of the values under a given registry key. The function must be used in a loop because each call only retrieves information about the value identified by the given index. Besides merely listing the names of each value, the function can also retrieve the data associated with each value. This data, which could be in any vaid registry data format, is placed in an array of bytes passed as lpData. It is the program's responsibility to correctly interpret the information. The key whose values are enumerated must have been opened with query-level access. The values which are enumerated are not retrived in any obvious order. The function returns 0 if successful, or a non-zero error code if an error occured.
Example:
' List the values under the following registry key:
' HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion
' Display the names of all values under the key. Also display the values' data if they are
' either null-terminated strings or binary data. (This example could be extended to include
' the rest of the registry data types, but it doesn't to save space). Note how the
' byte array is used to buffer the data before it might be copied to the string.
Dim valuename As String ' name of the value being retrieved
Dim valuelen As Long ' length of valuename
Dim datatype As Long ' receives data type of value
Dim data(0 To 254) As Byte ' 255-byte data buffer for read information
Dim datalen As Long ' size of data buffer information
Dim datastring As String ' will receive data converted to a string, if necessary
Dim hkey As Long ' handle to the registry key to enumerate the values of
Dim index As Long ' counter for the index of the value to enumerate
Dim c As Long ' counter variable
Dim retval As Long ' functions' return value
' Open the registry key to enumerate the values of.
retval = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows\CurrentVersion", 0, KEY_QUERY_VALUE, hkey)
' Check to see if an error occured.
If retval <> 0 Then
Debug.Print "Registry key could not be opened -- aborting."
End ' abort the program
End If
' Begin enumerating the values. Get each one, displaying its name. If it's a null-
' terminated string or binary data, display it. If not, say so.
index = 0 ' initialize the counter
While retval = 0 ' loop while successful -- retval is already 0 to begin with
' Initialize the value name buffer.
valuename = Space(255) ' 255-space buffer
valuelen = 255 ' length of the string
datalen = 255 ' size of data buffer
' Get the next value to be enumerated
retval = RegEnumValue(hkey, index, valuename, valuelen, ByVal 0, datatype, data(0), datalen)
If retval = 0 Then ' if successful, display information
' Extract the useful information from the value name buffer and display it.
valuename = Left(valuename, valuelen)
Debug.Print "Value Name: "; valuename
' Determine the data type of the value and display it.
Select Case datatype
Case REG_SZ ' null-terminated string
' Copy the information from the byte array into the string via the memory copying
' function. We subtract one because we don't want the trailing null. (Note the necessary
' usage of ByVal when passing a string to the following function.)
datastring = Space(datalen - 1) ' make just enough room in the string
CopyMemory ByVal datastring, data(0), datalen - 1 ' copy useful data
Debug.Print " Data (string): "; datastring
Case REG_BINARY ' binary data
' Display the hexadecimal values of each byte of data, separated by spaces.
' Use the datastring buffer to allow us to assure each byte is represented
' by a two-character string.
Debug.Print " Data (binary):"; ' get ready to output
For c = 0 To datalen - 1 ' loop through returned information
datastring = Hex(data(c)) ' convert value into hex
' If needed, add leading zero(s) to make to a two-character string.
If Len(datastring) < 2 Then datastring = String(2 - Len(datastring), "0") & datastring
Debug.Print " "; datastring;
Next c
Debug.Print ' end the line
Case Else ' a data type this example doesn't handle
Debug.Print "This example doesn't know how to read that kind of data."
End Select
End If
index = index + 1 ' increment the index counter
Wend ' end the loop
' Close the registry key.
retval = RegCloseKey
Related Call: RegEnumKeyEx
Category: Registry
Back to the index.