Registry Fever With Visual Basic - Why The Registry? (Page 2 of 5 )
In the olden days (and sometimes even now), most of the user related information was stored in INI files. The INI file stored data in a name/value format. Then, these values were set and retrieved from the INI files whenever required. The INI file is easy to read via a normal text editor. Here is an example of a standard INI file:
; This is a comment
[Category]
Name1=Value1
Name2= Value2
The INI file looks quite self-explanatory. The first line is a comment. Notice how a semicolon precedes it? The second line is the category name. Notice how its enclosed within "[" brackets. Finally, we have the name/value data on the third and fourth line.
A few drawbacks of storing data in "profiles" (which is another name for INI files) include:
- There wasn't any central database where data was stored as each program had its own INI file in its respective directory.
- Setting and retrieving values from these files was quite an inefficient task. They weren't organized in anyway.
These aspects lead to creation of the registry.
What Is The Registry? The registry is a hierarchical database. Its similar to the directory structure you see in Windows explorer. The registry consists of various hives and keys. You could navigate via the registry the same way you navigate through your Windows explorer. To open the windows registry on your Windows 9x pc, follow these steps:
- Click on Start -> Run
- Enter "regedit" in the text box
- Click "OK"
If all goes right then you will see the Windows registry, which looks like this:
The registry stores information in different categories, which are known as hives. The folders appearing on the left hand side of the registry are all hives. A hive may be further divided into keys. All of the information in the registry is divided into 6 major categories (or hives). Each hive may contain multiple keys and those keys may contain multiple sub-keys. Now let's look at what information these different hives (or categories) contain:
1. HKEY_CLASSES_ROOT
Stores information about the DLL's on the current computer. It also stores information about file extensions.
2. HKEY_CURRENT_USER
Stores information about the user who is currently logged on to the computer. Information stored includes environment variables, desktop settings, network configuration, etc.
3. HKEY_LOCAL_MACHINE
Stores information about the hardware and software configuration on the computer.
4. HKEY_USERS
Stores information about all users registered on the computer. Information stored includes environment variables, desktop settings, network configuration, etc.
5. HKEY_CURRENT_CONFIG
Stores information about the current hardware and software configuration on the computer.
6. HKEY_DYN_DATA
Stores information that changes frequently (dynamic information) on your computer. This includes thread/process details, plug and play, etc.
Functions To Access The Registry You can access the registry a number of ways in Visual Basic. One of the ways is to use functions for interacting with the registry.
The SaveSetting Function The SaveSetting function allows us to store any value in the registry. The functions signature looks like this:
SaveSetting <ApplicationName>, <Section>, <Key>, <Value> Application Name: The name of the application that's creating the key
Section: The section name under which this key is being created.
Key: The name of the key being created
Value: The value of the key being created
The following code snippet would add a key called TestKey in the registry to the HKEY_CURRENT_USER\Software\VB and VBA Program Settings\TestApp\TestSection path:
SaveSetting "TestApp", "TestSection", "TestKey", "TestVal" The GetSetting Function The GetSetting function allows us to retrieve any value from the registry. The usage of the GetSetting function looks like this:
GetSetting (<ApplicationName>, <Section>, <Key>[, Default]) Application Name: The application name for which we are retrieving this key
Section: The section name under which this key is being stored
Key: The name of the key that we are creating
Default: The default value would be returned if the value of the key couldn't be retrieved. This is an optional parameter.
The following code snippet would retrieve the value of the key TestKey from the registry under the following path HKEY_CURRENT_USER\Software\VB and VBA Program Settings\TestApp\TestSection path:
Dim value As String
value = GetSetting("TestApp", "TestSection", "TestKey", "Default")
MsgBox value "Default" would be displayed if no such entry was found.
Next: The DeleteSetting Function >>
More Visual Basic Articles
More By Neville Mehta