Module: Registry32 in Category Windows : Registry from Total Visual SourceBook

Routines for manipulating the registry (add, edit, delete, enumerate, and find registry keys and values) for VB6 and VBA using 32-bit Windows API calls.

Procedure Name Type Description
(Declarations) Declarations Declarations and private variables for the modRegistry32 module.
RegistryCreateNewKey32 Procedure Create a new key in the system registry using RegCreateKeyEx.
RegistryDeleteKey32 Procedure Delete a key from the system registry using RegDeleteKey. Note that any sub-keys must be deleted first. The API documentation for this function is incorrect. The Microsoft Knowledge Base states:
"The documentation for RegDeleteKey() points out that the specified key to be deleted must not have subkeys. If the key to be deleted does have subkeys, RegDeleteKey() will fail with access denied. This happens despite the fact that the machine account has delete privileges and the registry handle passed to RegDeleteKey() was opened with delete access. The additional requirement is that the key must have no subkeys."
RegistryDeleteValue32 Procedure Delete a value from the system registry using RegOpenKeyEx and RegDeleteValue.
RegistryEnumerateSubKeys32 Procedure Enumerate the subkeys of the specified key using RegQueryInfoKey and RegEnumKey.
RegistryEnumerateNames32 Procedure Enumerate the names of the values in a specified key using RegQueryInfoKey and RegEnumValu.
RegistryEnumerateValues32 Procedure Get the names and values of a specified key using RegQueryInfoKey and RegEnumValue.
RegistryGetKeyValue32 Procedure Get a value from the system registry using commands like RegQueryValueExNULL, RegQueryValueExString, and RegQueryValueExLong.
RegistryGetKeyValueEitherRoot32 Procedure Get a value from the system registry by looking in one root (Machine or CurrentUser) and if not found, choosing the other one.
RegistrySetKeyValue32 Procedure Set a registry key value and creates it if it doesn't already exist using RegCreateKeyEx API call.
' Example of modRegistry32
'
' To use this example, create a new module and paste this code into it.
' Then run the procedures by putting the cursor in the procedure and pressing:
'    F5 to run it, or
'    F8 to step through it line-by-line (see the Debug menu for more options)

Private Sub Example_modRegistry32_Read()
  ' Comments: Example of using the modRegistry32 module to read values from the Windows Registry with support for VB6 and 32 API calls for VBA
  '           See registry values in the Immediate Window

  Const cstrCurrentVersion As String = "SOFTWARE\Microsoft\Windows\CurrentVersion"
  Dim strValue As String
  Dim astrKeys() As String
  Dim astrNames() As String
  Dim avarValues() As Variant
  Dim lngCount As Long
  Dim lngCounter As Long
  Dim eRootKeyFound As EnumRegistryRootKeys
  Dim strHive As String

  ' Get the location of the Windows Program Files
  strValue = RegistryGetKeyValue32(HKEY_LOCAL_MACHINE, cstrCurrentVersion, "ProgramFilesDir")
  MsgBox "Location of Windows Program Files: " & strValue

  ' Search from either root (starting with User) to see if the value exists
  ' This example shows that after not finding it in the USER root, the value is found in the MACHINE root
  strValue = RegistryGetKeyValueEitherRoot32(False, cstrCurrentVersion, "ProgramFilesDir", eRootKeyFound)
  If Len(strValue) > 0 Then
    If eRootKeyFound = HKEY_LOCAL_MACHINE Then
      strHive = "HKEY_LOCAL_MACHINE"
    ElseIf eRootKeyFound = HKEY_CURRENT_USER Then
      strHive = "HKEY_CURRENT_USER"
    End If
    MsgBox "Location of Windows Program Files: " & strValue & vbCrLf & _
           "Found in registry hive: " & strHive
  End If

  ' This example shows that a name that doesn't exist returns a blank value
  strValue = RegistryGetKeyValue32(HKEY_LOCAL_MACHINE, cstrCurrentVersion, "NonExistent")
  If Len(strValue) = 0 Then
    MsgBox "Test value could not be found"
  End If

  ' Enumerate the subkeys by placing their values into an array, then print the values to the Immediate Window
  Call RegistryEnumerateSubKeys32(HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Office", astrKeys(), lngCount)
  If lngCount > 0 Then
    Debug.Print "Microsoft Office SubKeys:"
    For lngCounter = 0 To lngCount - 1
      Debug.Print astrKeys(lngCounter)
    Next lngCounter
    Debug.Print
  End If

  ' Get the names in a registry key, then print them to the Immediate Window
  Call RegistryEnumerateNames32(HKEY_LOCAL_MACHINE, cstrCurrentVersion, astrNames(), lngCount)
  If lngCount > 0 Then
    Debug.Print "Microsoft Windows CurrentVersion Registry Names:"
    For lngCounter = 0 To lngCount - 1
      Debug.Print astrNames(lngCounter)
    Next lngCounter
  End If
  Debug.Print

  ' Get the names and values of a registry subkey, then print them to the Immediate Window
  Call RegistryEnumerateValues32(HKEY_LOCAL_MACHINE, cstrCurrentVersion, astrNames(), avarValues(), lngCount)
  If lngCount > 0 Then
    Debug.Print "Microsoft Windows CurrentVersion Registry Names and Values:"
    For lngCounter = 0 To lngCount - 1
      Debug.Print astrNames(lngCounter), avarValues(lngCounter)
    Next lngCounter
  End If

  MsgBox "View the Immediate Window for enumerated registry values"

End Sub

Private Sub Example_modRegistry32_Write()
  ' Comments: Example of using the modRegistry32 module to create, get and delete values from the Windows Registry with support for VB6 and 32 API calls for VBA
  '           See values in the Immediate Window

  Const cstrRegKey As String = "Software\FMS\TotalVisualSourceBook\Test"
  Const cstrRegValue As String = "TestValue"

  Dim strResult As String

  ' Creates a new key in the user's registry
  If RegistryCreateNewKey32(HKEY_CURRENT_USER, cstrRegKey) Then
    MsgBox cstrRegKey & " successfully created", vbInformation

    ' Modify an existing key by assigning it the value of "SampleData"
    If RegistrySetKeyValue32(HKEY_CURRENT_USER, cstrRegKey, cstrRegValue, "SampleData", REG_SZ) Then
      MsgBox cstrRegKey & " successfully set", vbInformation

      ' Get the value we just created
      strResult = RegistryGetKeyValue32(HKEY_CURRENT_USER, cstrRegKey, cstrRegValue)
      Debug.Print "Key value: " & strResult & vbCrLf & "Retrieved from registry key: " & cstrRegKey

      ' Delete this value back out
      If RegistryDeleteValue32(HKEY_CURRENT_USER, cstrRegKey, cstrRegValue) Then
        ' Now remove the key
        If RegistryDeleteKey32(HKEY_CURRENT_USER, cstrRegKey) Then
          MsgBox cstrRegKey & " successfully deleted", vbInformation
        Else
          MsgBox cstrRegKey & " could not be deleted", vbCritical
        End If
      End If
    Else
      MsgBox cstrRegKey & " could not be set", vbCritical
    End If
  Else
    MsgBox cstrRegKey & " could not be created", vbCritical
  End If

End Sub

Total Visual SourceBook The source code in Total Visual Sourcebook includes modules and classes for Microsoft Access, Visual Basic 6 (VB6), and Visual Basic for Applications (VBA) developers. Easily add this professionally written, tested, and documented royalty-free code into your applications to simplify your application development efforts.

Total Visual SourceBook is written for the needs of a developer using a source code library covering the many challenges you face. Countless developers over the years have told us they learned some or much of their development skills and tricks from our code. You can too!

Additional Resources

Total Visual SourceBook CD and Printed Manual

Microsoft Access/ Office 2016, 2013, 2010, and 2007 Version
is Shipping!

New features in Total Visual SourceBook for Access, Office and VB6

Supports Access/Office 2016, 2013, 2010 and 2007, and Visual Basic 6.0!


View all FMS products for Microsoft Access All Our Microsoft Access Products

Reviews

Reader Choice Award for MS Access Source Code Library
Reader Choice

"The code is exactly how I would like to write code and the algorithms used are very efficient and well-documented."

Van T. Dinh, Microsoft MVP

SourceBook Info

Additional Info

Question

 

 

Free Product Catalog from FMS