Home arrow Visual Basic arrow Page 2 - Registry Fever With Visual Basic
VISUAL BASIC

Registry Fever With Visual Basic


The Windows registry is the central database of any Windows PC. In this article Neville explains the registry, and shows us how to access it using both Visual Basic's functions and the Windows API.

Author Info:
By: Neville Mehta
Rating: 4 stars4 stars4 stars4 stars4 stars / 69
December 20, 2002
TABLE OF CONTENTS:
  1. · Registry Fever With Visual Basic
  2. · Why The Registry?
  3. · The DeleteSetting Function
  4. · Using the Windows API To Access the Registry
  5. · Conclusion

print this article
SEARCH DEVARTICLES

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.
blog comments powered by Disqus
VISUAL BASIC ARTICLES

- Developing an XML Web Service Using Visual S...
- Creating an HTML File List with VB
- Fun with Email: VB6, CDO, MAPI, and a Remote...
- Extranet/Intranet Dictionary Cracker in VB
- Finding Default App Icons With Visual Basic
- Registry Fever With Visual Basic
- Implementing An ADO Data Control With VB6
- Printing With Visual Basic
- MSMQ Part 1/2: Architecture and Simple Imple...
- Magnifying The Desktop With Visual Basic
- Sending Email With MAPI Components in Visual...
- Two Person Chat With The Winsock Control And...
- A Real-Time ActiveX News Control
- Accessing the Windows API in Visual Basic

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials