January 18, 2017
Hot Topics:

The Registry - Now I've got it what can I do?

  • November 19, 2002
  • By John Percival
  • Send Email »
  • More Articles »
Public Function GetAllValues( _
  hKey As Long, _
  strPath As String) As Variant
' Returns: a 2D array.
' (x,0) is value name
' (x,1) is value type (see constants)

Dim lRegResult As Long
Dim hCurKey As Long
Dim lValueNameSize As Long
Dim strValueName As String
Dim lCounter As Long
Dim byDataBuffer(4000) As Byte
Dim lDataBufferSize As Long
Dim lValueType As Long
Dim strNames() As String
Dim lTypes() As Long
Dim intZeroPos As Integer

lRegResult = RegOpenKey(hKey, _
               strPath, hCurKey)

' Initialise bufffers
lValueNameSize = 255
strValueName = String$(lValueNameSize, " ")
lDataBufferSize = 4000

lRegResult = RegEnumValue(hCurKey, lCounter, _
strValueName, lValueNameSize, 0&, lValueType, _
byDataBuffer(0), lDataBufferSize)

If lRegResult = ERROR_SUCCESS Then

' Save the type
ReDim Preserve strNames(lCounter) As String
ReDim Preserve lTypes(lCounter) As Long
lTypes(UBound(lTypes)) = lValueType

'Tidy up string and save it
intZeroPos = InStr(strValueName, Chr$(0))
If intZeroPos > 0 Then
strNames(UBound(strNames)) = _
Left$(strValueName, intZeroPos - 1)
strNames(UBound(strNames)) = strValueName
End If

lCounter = lCounter + 1

Exit Do
End If

'Move data into array
Dim Finisheddata() As Variant
ReDim Finisheddata(UBound(strNames), 0 To 1) As Variant

For lCounter = 0 To UBound(strNames)
Finisheddata(lCounter, 0) = strNames(lCounter)
Finisheddata(lCounter, 1) = lTypes(lCounter)

GetAllValues = Finisheddata

End Function

This function is slightly more complicated because we must know what type of data the value refers to. Because of this, this function returns a two dimensional array. The index for the value is the first dimension, and the second dimension is either 0 or 1. In 0, the name of the value is stored, and in 1, the type of data is stored. I decided to return the data type rather than the data because it makes the function easier, and also means that you can retrieve only the ones that you want, rather than eating up memory by retrieving then all.

In all other respects, this function is similar to the other. It has a counter, which counts from 0, retrieving the value names and data. The buffer length is set to 4000 because this is the largest binary type value. However, if you know that your largest value will be less than this, you can reduce it to lower the memory requirements.

Once all the data has been retrieved from the registry, it is moved into the array, which is passed from the function. This could not be done as we were going along because only the last dimension of a dynamic array can be resized. Since I wanted to keep in sync with the VB function, this seems to be the best way to do it.

Use the function in a similar way to the VB function. Although the line for printing the first element of the binary key looks slightly wrong, it is correct. Since the function returns an array, I access it outside the brackets of the function's parameters.

Dim Values As Variant
Dim KeyLoop As Integer
Dim RegPath As String
Dim HKCU As Long

HKCU = HKEY_CURRENT_USER ' to save typing
RegPath = "Software\Microsoft\Windows\CurrentVersion\Explorer"

Values = GetAllValues(HKCU, RegPath)

If VarType(Values) = vbArray + vbVariant Then

For KeyLoop = 0 To UBound(Values)
Debug.Print Values(KeyLoop, 0)

Select Case Values(KeyLoop, 1)
Debug.Print GetSettingLong(HKCU, RegPath, _
CStr(Values(KeyLoop, 0)))
Debug.Print GetSettingByte(HKCU, RegPath, _
Hex$(Values(KeyLoop, 0)))(0)
Debug.Print GetSettingString(HKCU, RegPath, _
CStr(Values(KeyLoop, 0)))
End Select


End If

Page 3 of 5

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Thanks for your registration, follow us on our social networks to keep up-to-date
Rocket Fuel