Visual Basic - Registry Programming

6 minute read

One of the most required tasks these days for software is to remember the configuration and the settings as changed by the user and function accordingly. This can be done either by keeping a separate database for such parameters or by using Windows Registry.  In this article, we shall deal with Registry Programming

Registry Programming

What is Registry?


The Registry is a database used to store settings and options for the 32 bit versions of Microsoft Windows including Windows 95, 98, ME and NT/2000. It contains information and settings for all the hardware, software, users, and preferences of the PC.

 

Any changes in software configuration or changes done in Control Panel settings are reflected and stored in the Registry.

Structure of Registry

 

The Registry has a hierarchal structure and appears similar directory structure as seen in Windows Explorer.  We can open Registry Editor by typing regedit in Start Menuà Run

 

clip_image002

 

Figure 1: Registry Editor


Each main branch (win a folder icon) is called a Hive, and Hives contains Keys. Each key can contain other keys (as sub-keys), as well as Values. The values contain the actual information stored in the Registry. There are three types of values; String, Binary, and DWORD - the use of these depends upon the context.

 

Before starting registry programming, let’s first understand what each of the Hives mean and the values that can stored in sub-keys.

 

Table 1: Hives

 

Hive

Interpretation

Data Stored

HKEY_CLASSES_ROOT

File Association

The branch contains all of your file association mappings to support the drag-and-drop feature, OLE information and Windows shortcuts.

HKEY_CURRENT_USER

Information regarding User Logged on.

The branch links to the section of HKEY_USERS appropriate for the user currently logged onto the PC and contains information such as logon names, desktop settings, and Start menu settings.

HKEY_LOCAL_MACHINE

Hardware Specific Information

This branch contains computer specific information about the type of hardware, software, and other preferences on a given PC.

HKEY_USERS

User Information

This branch contains individual preferences for each user of the computer, each user is represented by a SID sub-key located under the main branch.

HKEY_CURRENT_CONFIG

Current Hardware Configuration

This branch links to the section of HKEY_LOCAL_MACHINE appropriate for the current hardware configuration.

HKEY_DYN_DATA

Plug-&-Play Features of Windows

This branch is for use with the Plug-&-Play features of Windows, this section is dymanic and will change as devices are added and removed from the system

 

 


Values are of four types varying on the type of data we wish to store in the registry.

 

Table 2:  Types of Values

 

Type

Interpretation

REG_BINARY

This type stores the value as raw binary data.

REG_DWORD

This type represents the data by a four byte number and is commonly used for Boolean values, such as "0" is disabled and "1" is enabled.

REG_EXPAND_SZ

This type is an expandable data string that is string containing a variable to be replaced when called by an application.

REG_MULTI_SZ

This type is a multiple string used to represent values that contain lists or multiple values, each entry is separated by a NULL character.

REG_SZ

This type is a standard string, used to represent human readable text values.

 

Saving Values in Registry

 

When we install our software, say for example, Numerology Report System, a hive is automatically created in Registry as shown in Figure 2.

 

 

clip_image004

 

Figure 2: Registry Key in Registry Editor

 

We create an object as registry by

 


Dim reg As cRegistry

 

Thereafter, we enter the values in the keys by a small snippet.

 

Set reg = New cRegistry

      'This will create a new section to store your own keys in

reg.Classkey = Hkey ‘HKEY_CURRENT_USER

reg.SectionKey = SectionKey   ‘Software\Numerology Report System

'create the above key

reg.CreateKey                 ‘My Settings

reg.Valuetype = Valuetype     ‘REG_sz

reg.Valuekey = Valuekey       ‘Date

reg.Value = Value             ‘Date$

 

Getting Values from Registry

 

Let us obtain the value of the key, which is created while installation of our software.

 

clip_image006

 

 

Figure 3: Value of the Key in Registry Editor

 

We can obtain this value using same method:

 

Set reg = New cRegistry

reg.Classkey = Hkey

reg.SectionKey = Section

reg.Valuekey = Valuekey

 


Let’s write a piece of code to write in registry and read it.

 

Dim reg As cRegistry

 

Private Function CreateKeys(HKey As String, SectionKey As String, ValueKey As String, Valuetype As String, Value As String)

      Set reg = New cRegistry

       'This will create a new section to store your own keys in

reg.Classkey = Hkey

reg.SectionKey = SectionKey

'create the above key

reg.CreateKey

'Save the Form settings to the registry

reg.Valuetype = Valuetype

reg.Valuekey = Valuekey

reg.Value = Value

End Function

 

‘HKey = HKEY_CURRENT_USER

‘SectionKey = Software\VB and VBA Program Settings\Numerology Report System\My Settings

‘ValueKey = Date

‘ValueType = Reg_sz

‘Value = 01-05-2006

 

Call CreateKeys(HKEY_CURRENT_USER, "Software\VB and VBA Program Settings\Numerology Report System\My Settings

", "Date", Reg_sz, date$)

 

‘Hkey = HKEY_CURRENT_USER

‘Section = "Software\VB and VBA Program Settings\Numerology Report    System\My Settings

‘ValueKey = Date

 

Private Function GetKeys(Hkey As String, Section As String, Valuekey As String)  'Reads the Registry

    Set reg = New cRegistry

    reg.Classkey = Hkey

    reg.SectionKey = Section

    reg.Valuekey = Valuekey

End Function

 

Call GetKeys("Software\VB and VBA Program Settings\Numerology Report System\My Settings", "Date")

 

Another way to save and get values of a key in registry

 

There’s one method to save keys and get their values through coding.  This method uses statements instead of creating objects. They are listed in Table 3.


 

Table 3: Setting and Getting Values

 

 

Setting a Value

Getting Value

Syntax:

SaveSetting(appname, section, key, setting)

GetSetting(appname, section, key)

Example:

SaveSetting App.Title, "Settings", "Left", Me.Left

Me.Left= GetSetting(App.Title, "Settings", "Left")

 

The key Settings is created under the key which is made during installation procedure and if we wish to have different path (say for example HKEY_CURRENT_USER\SOFTWARE\NRS) then we need to mention in

 

SaveSetting App.Title, "HKEY_CURRENT_USER\SOFTWARE\NRS", "Left", Me.Left

 

Let’s now delete the key we have made.

Deleting Registry Keys

 

The statement for deleting the registry settings, we use the DeleteSetting statement as shown below,

 

DeleteSetting appname, section[, key]    

 

Let’s understand the syntax of this statement.

 

Table 4: Syntax of DeleteString

 

Argument

Interpretation

appname

Application Name
(normally referred as App.Title or App.EXEName

section

Name of the Section where the key is stored/key which needs to be deleted.

key

Expression containing name of the key setting being deleted.

 

DeleteSetting App.Title, "Settings"

 

To check if the key is deleted or not,

 

If len(GetSetting(App.Title, "Settings", "Left")) = 0 
     
'then it doesn’t exists

      MsgBox “Key not found”

End If

 

 


Why use Registry

 

The question that remains unanswered is why use registry?  Registry can be used for several reasons as listed below

 

·         To remember the last used configuration of software.

·         To keep a track of when all the software was used.

·         To keep license keys.

·         To track the changes made by the software in the system.

 

With this we end this article on Registry Programming.

Note: This series was first published in DeveloperIQ and was co-authored by Puneet Ghanshani with Pranjali Bakeri in 2005-2006.

 

Updated: