Home arrow Visual Basic arrow Page 3 - Registry Fever With 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
  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

Registry Fever With Visual Basic - The DeleteSetting Function
(Page 3 of 5 )

The DeleteSetting function allows us to delete any value/values from the registry. The signature of the DeleteSetting function looks like this:

DeleteSetting <ApplicationName> [,<Section> [,<Key>]]

Application Name: The application name from which we are deleting keys
Section: Contains the keys you need to delete. This is an optional parameter
Key: The name of the key that you want to delete. This is an optional parameter

Private Sub DeleteRegistry()
DeleteSetting "TestApp"
DeleteSetting "TestApp2", "TestSection2"
DeleteSetting "TestApp3", "TestSection3", "TestKey3"
End Sub

Let's see exactly what the code above does:

Firstly, it would delete the entire TestApp key (including all sub-keys and values) under it from the HKEY_CURRENT_USER\Software\VB and VBA Program Settings\TestApp path.

Next, it would delete the entire TestSection2 key (which is a part of the TestApp2 key) and all sub-keys and values under it from the HKEY_CURRENT_USER\Software\VB and VBA Program Settings\TestApp2\TestSection2 path. However, the TestApp2 key and its contents wouldn’t be deleted.

Finally, it would delete the TestKey3 string value (which is a part of the TestApp3\TestSection3 keys). However the TestApp3 key and TestSection3 key (and their contents) wouldn't be deleted.

The GetAllSettings Function
The GetAllSettings function allows us to retrieve an entire collection of string values from the registry. Using this function, we can retrieve an entire set of string values from a particular section. The signature for the GetAllSettings function looks like this:

GetAllSettings (<ApplicationName>, <Section>)

Application Name: The application key under which you need to retrieve the collection of string values
Section: The key to where the collections of values are present

Private Sub GetAllRegistry()
Dim valuesandkeys As Variant
Dim count As Integer

SaveSetting "Tester", "TestSec", "TestName1", "TestVal1"
SaveSetting "Tester", "TestSec", "TestName2", "TestVal2"

valuesandkeys = GetAllSettings("Tester", "TestSec")

For count = LBound(valuesandkeys) To UBound(valuesandkeys)
Key = valuesandkeys(count, 0)
value = valuesandkeys(count, 1)
MsgBox Key
MsgBox value
Next count
End Sub

Let's see exactly what the code above does:

Firstly, we add two string values into the registry under the HKEY_CURRENT_USER\Software\VB and VBA Program Settings\Tester\TestSec\ path.

Next, we call the GetAllSettings function which returns a two dimensional array of all keys and values in the HKEY_CURRENT_USER\Software\VB and VBA Program Settings\Tester\TestSec\ hive.

Finally, we use a simple for loop to display all of the values in the array. The first index in the array holds the name/key serial count. We would pass 0 as the next index if we wanted to display the string name and 1 if we wanted to trap the key string value. The structure of the two dimensional array looks like this:

     0     1
0     TestName1     TestValue1
1     TestName2     TestValue2

Thus, if we pass the index 1,1 to the array, then it would return "TestValue2". If we pass the index 0,0 to the array then it would return "TestName1".

There are some points to remember when using Visual Basic functions to access the registry:
  1. This code wouldn't work if you didn’t have appropriate permissions to access the registry. Remember, whenever you run software on a computer that interacts with the registry, you have to make sure the logged in user has privileges to access the system registry or else your program will not function.
  2. The Visual Basic functions help us interact with the registry only under the following path "HKEY_CURRENT_USER\Software\VB and VBA Program Settings"..
  3. The Visual Basic functions are capable of retrieving and setting String keys only.
As you can see, there are some disadvantages to using Visual Basic's registry functions. For example, what if you needed to set/retrieve values from other parts of the registry? What if you need to set/retrieve binary or dword values from the registry? Well, in such cases you may have to use the Windows API to access the registry, which is what we will look at next.
blog comments powered by Disqus

- 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 

Developer Shed Affiliates


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