The new vbAccelerator Site - more VB and .NET Code and Controls
Source Code
3 Code Libraries &nbsp

NOTE: this code has been superceded by the version at the new site.


Complete Registry control

[VB Registry Editor Demo]

Download the cRegistry Class (5kb)
Download the RegEdit Demo Application (66kb)

&nbsp UpdatedUpdated! 21 March 2000 &nbsp
&nbsp Modified the CreateExeAssociation and CreateAdditionalExeAssociations methods to update the HKEY_LOCAL_MACHINE\SOFTWARE\Classes sections as well as the default HKEY_CLASSES_ROOT. &nbsp
&nbsp UpdatedUpdated! 2 January 1999 &nbsp
&nbsp 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.

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.

Here is a brief summary of typical uses of the class:

To get a String Value from the Registry

&nbsp &nbsp Dim c As New cRegistry
&nbsp &nbsp With c
&nbsp &nbsp &nbsp &nbsp .ClassKey = HKEY_LOCAL_MACHINE
&nbsp &nbsp &nbsp &nbsp .SectionKey = "Software\MyApp\Tips"
&nbsp &nbsp &nbsp &nbsp .ValueKey = "Tip1"
&nbsp &nbsp &nbsp &nbsp .ValueType = REG_SZ
&nbsp &nbsp &nbsp &nbsp sTip = .Value
&nbsp &nbsp End With

To get a Numeric Value from the Registry

&nbsp &nbsp Dim c As New cRegistry
&nbsp &nbsp With c
&nbsp &nbsp &nbsp &nbsp .ClassKey = HKEY_LOCAL_MACHINE
&nbsp &nbsp &nbsp &nbsp .SectionKey = "Software\MyApp\Tips"
&nbsp &nbsp &nbsp &nbsp .ValueKey = "TipCount"
&nbsp &nbsp &nbsp &nbsp .ValueType = REG_DWORD
&nbsp &nbsp &nbsp &nbsp lTipCount = .Value
&nbsp &nbsp End With

To Save a Form's position to the Registry

&nbsp &nbsp Dim c As New cRegistry
&nbsp &nbsp With c
&nbsp &nbsp &nbsp &nbsp .ClassKey = HKEY_CURRENT_USER
&nbsp &nbsp &nbsp &nbsp ' You don't need to check if this key already exists
&nbsp &nbsp &nbsp &nbsp ' - the class will create it for you
&nbsp &nbsp &nbsp &nbsp .SectionKey = "Software\" & App.ExeName & "\" & frmThis.Name
&nbsp &nbsp &nbsp &nbsp .ValueKey = "Maximized"
&nbsp &nbsp &nbsp &nbsp .ValueType = REG_DWORD
&nbsp &nbsp &nbsp &nbsp .Value = (frmThis.WindowState = vbMaximized)
&nbsp &nbsp &nbsp &nbsp If (frmThis.WindowState vbMaximized)
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp .ValueKey = "Left"
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp .Value = frmThis.Left
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp .ValueKey = "Top"
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp .Value = frmThis.Top
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp .ValueKey = "Width"
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp .Value = frmThis.Width
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp .ValueKey = "Height"
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp .Value = frmThis.Height
&nbsp &nbsp &nbsp &nbsp End If
&nbsp &nbsp 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

&nbsp &nbsp Dim c As New cRegistry
&nbsp &nbsp Dim sKeys() As String, iKeyCount As Long

&nbsp &nbsp With c
&nbsp &nbsp &nbsp &nbsp .ClassKey = HKEY_LOCAL_MACHINE
&nbsp &nbsp &nbsp &nbsp .SectionKey = "Software"
&nbsp &nbsp &nbsp &nbsp .EnumerateSections(sKeys(), iKeyCount)
&nbsp &nbsp &nbsp &nbsp For iKey = 1 To iKeyCount
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp Debug.Print sKeys(iKey)
&nbsp &nbsp &nbsp &nbsp Next iKey
&nbsp &nbsp End With

To Delete a Key

&nbsp &nbsp Dim c As New cRegistry
&nbsp &nbsp With c
&nbsp &nbsp &nbsp &nbsp .ClassKey = HKEY_LOCAL_MACHINE
&nbsp &nbsp &nbsp &nbsp .SectionKey = "Software\MyApp\Tips"
&nbsp &nbsp &nbsp &nbsp .DeleteKey
&nbsp &nbsp End With

To Associate a File of type .CCD with your executable

&nbsp &nbsp Dim c As New cRegistry
&nbsp &nbsp With c
&nbsp &nbsp &nbsp &nbsp .CreateEXEAssociation _
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp App.Path & "\" & App.ExeName, _
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp "CCarDesign.Project", _
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp "Custom Car Designer Project", _
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp "CCD"
&nbsp &nbsp End With

To Delete a Value

&nbsp &nbsp Dim c As New cRegistry
&nbsp &nbsp With c
&nbsp &nbsp &nbsp &nbsp .ClassKey = HKEY_LOCAL_MACHINE
&nbsp &nbsp &nbsp &nbsp .SectionKey = "Software\MyApp\Tips"
&nbsp &nbsp &nbsp &nbsp .SectionKey = "Tip1"
&nbsp &nbsp &nbsp &nbsp .DeleteValue
&nbsp &nbsp 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:

&nbsp &nbsp Dim cR As New cRegistry
&nbsp &nbsp Dim iByte As Long
&nbsp &nbsp Dim vR as Variant

&nbsp &nbsp With cR
&nbsp &nbsp &nbsp &nbsp .ClassKey = HKEY_CURRENT_USER
&nbsp &nbsp &nbsp &nbsp .SectionKey = "Control Panel\Appearance"
&nbsp &nbsp &nbsp &nbsp .ValueKey = "CustomColors"
&nbsp &nbsp &nbsp &nbsp vR = .Value

&nbsp &nbsp &nbsp &nbsp If .ValueType = REG_BINARY Then
&nbsp &nbsp &nbsp &nbsp ' Read through the byte array and output it as a series of hex values:
&nbsp &nbsp &nbsp &nbsp For iByte = LBound(vR) To UBound(vR)
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp sOut = sOut & "&H"
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp If (iByte &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp sOut = sOut & "0"
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp End If
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp sOut = sOut & Hex$(vR(iByte)) & " "
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp Next iByte
&nbsp &nbsp &nbsp &nbsp Else
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp sOut = vR
&nbsp &nbsp &nbsp &nbsp End If

&nbsp &nbsp &nbsp &nbsp Debug.Print sOut
&nbsp &nbsp 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:

&nbsp &nbsp Dim cR As New cRegistry
&nbsp &nbsp Dim i As Long
&nbsp &nbsp Dim lC As Long
&nbsp &nbsp Dim bR As Byte
&nbsp &nbsp Dim bG As Byte
&nbsp &nbsp Dim bB As Byte
&nbsp &nbsp Dim bOut() As Byte

&nbsp &nbsp ' Create a binary array containing all the Red,Green,Blue values of the QBColors:
&nbsp &nbsp ReDim bOut(0 To 15 * 3 - 1) As Byte
&nbsp &nbsp For i = 1 To 15
&nbsp &nbsp &nbsp &nbsp ' Get the Red, Green, Blue for the QBColor at index i:
&nbsp &nbsp &nbsp &nbsp lC = QBColor(i)
&nbsp &nbsp &nbsp &nbsp bR = (lC And &HFF&)
&nbsp &nbsp &nbsp &nbsp bG = ((lC And &HFF00&) \ &H100&)
&nbsp &nbsp &nbsp &nbsp bB = ((lC And &HFF0000) \ &H10000)
&nbsp &nbsp
&nbsp &nbsp &nbsp &nbsp ' Add Red, Green, Blue to the byte array to store:
&nbsp &nbsp &nbsp &nbsp bOut((i - 1) * 3) = bR
&nbsp &nbsp &nbsp &nbsp bOut((i - 1) * 3 + 1) = bG
&nbsp &nbsp &nbsp &nbsp bOut((i - 1) * 3 + 2) = bB
&nbsp &nbsp Next i

&nbsp &nbsp ' Store it:
&nbsp &nbsp With cR
&nbsp &nbsp &nbsp &nbsp .ClassKey = HKEY_CURRENT_USER
&nbsp &nbsp &nbsp &nbsp .SectionKey = "software\vbaccelerator\cRegistry\Binary Test"
&nbsp &nbsp &nbsp &nbsp .ValueKey = "QBColors"
&nbsp &nbsp &nbsp &nbsp .ValueType = REG_BINARY
&nbsp &nbsp &nbsp &nbsp .Value = bOut()
&nbsp &nbsp End With

TopBack to top

Source Code - What We're About!Back to Source Code


AboutContributeSend FeedbackPrivacy

Copyright 1998-2000, Steve McMahon ( All Rights Reserved.
Last updated: 21 March 2000