Home arrow Visual Basic arrow Page 4 - 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 - Using the Windows API To Access the Registry
(Page 4 of 5 )

  I'll assume that you are aware of what the Windows API is and how we use it in Visual Basic.

Whenever we are going to use any API function, we need to declare it in our code. In our case, we would have to declare the following functions:

Public Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long

Public Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long

Public Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal hKey As Long, ByVal lpSubKey As String) As Long

Public Declare Function RegCloseKey Lib "advapi32.dll" Alias "RegCloseKey" (ByVal hKey As Long) As Long


Creating a Key
Lets create a function that uses the RegCreateKey API function internally to create a key anywhere in the registry:

Private Sub CreateNewKey()
Dim hNewKey As Long
Dim lRetVal As Long

lRetVal = RegCreateKey(HKEY_CURRENT_USER,"TestKey", hNewKey)
RegCloseKey (hNewKey)
End Sub


Let's look at what this code does.

First up, we call the RegCreateKey function and pass 3 parameters to it. Firstly, the hive in which we want to create the key. Secondly, the name of the key that we want to create under this hive, and finally, the key handle, which can be an empty long variable.

After this line is executed, the hNewKey long variable would contain a reference to the newly created key. This line would create a key right under the HKEY_CURRENT_USER hive. The successful execution of this line would return a long value, which we trap in the lRetVal variable.

Next up, we call the RegCloseKey function to close the reference that we have just created. This is done in order to free CPU resources. It's not a mandatory step, but should be done. It takes just one parameter, which is the key handle (or object) to be closed.

Deleting a Key
Let's code a function that uses the RegCreateKey API function internally to delete a key anywhere in the registry:

Private Sub Form_Load()
Dim lRetVal As Long
Dim hNewKey As Long

lRetVal = RegOpenKey(HKEY_CURRENT_USER, "", hNewKey)
lRetVal = RegDeleteKey(hNewKey, "Test")

RegCloseKey (hNewKey)
End Sub


Let's look at what this code does.

Firstly, we call the RegOpenKey function and pass 3 parameters to it: the hive in which we want to delete the key, the key path to delete, and the key handle.

After this line is executed, the hNewKey long variable would contain a reference to the key path specified in the RegOpenKey function. This line would create a reference to the HKEY_CURRENT_USER hive. The successful execution of this line would return a long value, which we trap in the lRetVal variable.

Secondly, we call the RegDeleteKey function and pass 2 parameters to it. Firstly, the reference to the hive we need to delete the key from. Secondly, the key path to delete. This line would delete the "Test" key under the HKEY_CURRENT_USER hive. It would also end up deleting all of its sub keys and values too. The successful execution of this line would return a long value, which we trap in the lRetVal variable.

Finally, we call the RegCloseKey function to close the reference we have just created. This is done in order to free CPU resources. This is not a mandatory step, but it should be done. It takes one parameter, which is the key handle to be closed.
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