Visual Basic
  Home arrow Visual Basic arrow Page 4 - Registry Fever With Visual Basic
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
VISUAL BASIC

Registry Fever With Visual Basic
By: Neville Mehta
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 51
    2002-12-20

    Table of Contents:
  • Registry Fever With Visual Basic
  • Why The Registry?
  • The DeleteSetting Function
  • Using the Windows API To Access the Registry
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

    More Visual Basic Articles
    More By Neville Mehta


       · We need a specific registry project like disabling Task manager or folder option.......
     

    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







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    Stay green...Green IT