Complete Registry control
Download the cRegistry Class (5kb) Download the RegEdit Demo Application (66kb)
  |
Updated! 21 March 2000
|
  |
  |
|
Modified the CreateExeAssociation and CreateAdditionalExeAssociations
methods to update the HKEY_LOCAL_MACHINE\SOFTWARE\Classes sections as well as the default HKEY_CLASSES_ROOT.
|
  |
  |
Updated! 2 January 1999
|
  |
  |
|
Fixed a bug with the CreateExeAssociation method. If you did not
provide the optional document default icon parameter the association was not set up correctly.
The CreateExeAssociation method now allows Print,Install and New associations to be created and
also allows the menu text to be customised.
A further method, CreateAdditionalExeAssociations allows you to create customised items.
See the Passing Command Line Parameters article for
more details on this.
|
  |
Introduction
The cRegistry class is an easy, self-contained way to get complete access to the Windows registry. Simple
methods allow you to create, enumerate and delete keys and values in the registry, without restriction. You
can even read/write binary data to the registry.
To see how powerful this library is, download the demonstration Registry Editor, written entirely in VB.
It runs at about the same speed as RegEdit shipped with Windows (although in my version not all the features are finished!)
The demo also demonstrates my simple splitter class.
Usage
Here is a brief summary of typical uses of the class:
To get a String Value from the Registry
    Dim c As New cRegistry
    With c
        .ClassKey = HKEY_LOCAL_MACHINE
        .SectionKey = "Software\MyApp\Tips"
        .ValueKey = "Tip1"
        .ValueType = REG_SZ
        sTip = .Value
    End With
To get a Numeric Value from the Registry
    Dim c As New cRegistry
    With c
        .ClassKey = HKEY_LOCAL_MACHINE
        .SectionKey = "Software\MyApp\Tips"
        .ValueKey = "TipCount"
        .ValueType = REG_DWORD
        lTipCount = .Value
    End With
To Save a Form's position to the Registry
    Dim c As New cRegistry
    With c
        .ClassKey = HKEY_CURRENT_USER
        ' You don't need to check if this key already exists
        ' - the class will create it for you
        .SectionKey = "Software\" & App.ExeName & "\" & frmThis.Name
        .ValueKey = "Maximized"
        .ValueType = REG_DWORD
        .Value = (frmThis.WindowState = vbMaximized)
        If (frmThis.WindowState vbMaximized)
            .ValueKey = "Left"
            .Value = frmThis.Left
            .ValueKey = "Top"
            .Value = frmThis.Top
            .ValueKey = "Width"
            .Value = frmThis.Width
            .ValueKey = "Height"
            .Value = frmThis.Height
        End If
    End With
To Get All The SubKeys of a Key
Getting all the values with a key is achieved in a similar way, except you use EnumerateValues instead
of EnumerateSections
    Dim c As New cRegistry
    Dim sKeys() As String, iKeyCount As Long
    With c
        .ClassKey = HKEY_LOCAL_MACHINE
        .SectionKey = "Software"
        .EnumerateSections(sKeys(), iKeyCount)
        For iKey = 1 To iKeyCount
            Debug.Print sKeys(iKey)
        Next iKey
    End With
To Delete a Key
    Dim c As New cRegistry
    With c
        .ClassKey = HKEY_LOCAL_MACHINE
        .SectionKey = "Software\MyApp\Tips"
        .DeleteKey
    End With
To Associate a File of type .CCD with your executable
    Dim c As New cRegistry
    With c
        .CreateEXEAssociation _
            App.Path & "\" & App.ExeName, _
            "CCarDesign.Project", _
            "Custom Car Designer Project", _
            "CCD"
    End With
To Delete a Value
    Dim c As New cRegistry
    With c
        .ClassKey = HKEY_LOCAL_MACHINE
        .SectionKey = "Software\MyApp\Tips"
        .SectionKey = "Tip1"
        .DeleteValue
    End With
To Read BINARY values from the registry
Binary values are returned as a variant of type byte array. This code demonstrates how
to format the returned value into a string of hexadecimal values, similar to the display
provided in RegEdit:
    Dim cR As New cRegistry
    Dim iByte As Long
    Dim vR as Variant
    With cR
        .ClassKey = HKEY_CURRENT_USER
        .SectionKey = "Control Panel\Appearance"
        .ValueKey = "CustomColors"
        vR = .Value
        If .ValueType = REG_BINARY Then
        ' Read through the byte array and output it as a series of hex values:
        For iByte = LBound(vR) To UBound(vR)
            sOut = sOut & "&H"
            If (iByte
                sOut = sOut & "0"
            End If
            sOut = sOut & Hex$(vR(iByte)) & " "
            Next iByte
        Else
            sOut = vR
        End If
        Debug.Print sOut
    End With
To Set BINARY values from the registry
Similarly, to store binary values in the registry, cRegistry.cls expects a byte array
of the binary values you wish to store. This example (rather uselessly!) stores all the
Red, Green, Blue values of each of VB's QBColors into a binary array:
    Dim cR As New cRegistry
    Dim i As Long
    Dim lC As Long
    Dim bR As Byte
    Dim bG As Byte
    Dim bB As Byte
    Dim bOut() As Byte
    ' Create a binary array containing all the Red,Green,Blue values of the QBColors:
    ReDim bOut(0 To 15 * 3 - 1) As Byte
    For i = 1 To 15
        ' Get the Red, Green, Blue for the QBColor at index i:
        lC = QBColor(i)
        bR = (lC And &HFF&)
        bG = ((lC And &HFF00&) \ &H100&)
        bB = ((lC And &HFF0000) \ &H10000)
   
        ' Add Red, Green, Blue to the byte array to store:
        bOut((i - 1) * 3) = bR
        bOut((i - 1) * 3 + 1) = bG
        bOut((i - 1) * 3 + 2) = bB
    Next i
    ' Store it:
    With cR
        .ClassKey = HKEY_CURRENT_USER
        .SectionKey = "software\vbaccelerator\cRegistry\Binary Test"
        .ValueKey = "QBColors"
        .ValueType = REG_BINARY
        .Value = bOut()
    End With
Back to top
Back to Source Code
|