Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleUse Registry API functions to save and restore values in Visual Basic 6
DescriptionThis example shows how to use Registry API functions to save and restore values in Visual Basic 6.
KeywordsRegistry, API,RegCloseKey, RegCreateKeyEx, RegOpenKeyEx, RegQueryValueExString, RegQueryValueExLong, RegQueryValueExNULL, RegSetValueExString, RegSetValueExLong, SHDeleteKey, Visual Basic 6
CategoriesAPI, Windows
 
Thanks to WJK.

This example shows how to use API Registry functions to save and restore values.

When you click the Microsft Remove button, the following code executes. It calls subroutine DeleteKeys to remove the key HKEY_CURRENT_USER\Software\MyCompany\Registration.

 
Private Sub cmdMS_Remove_Click()
    Dim Result As Long
On Error GoTo cmdMS_Remove_Click_Error
    Call DeleteKeys(HKEY_CURRENT_USER, _
        "Software\MyCompany", "Registration", Result)
    If Result <> 0 Then
        MsgBox "Key Deletion Failed: " & CStr(Result)
    End If
    Exit Sub

cmdMS_Remove_Click_Error:
    MsgBox Err.Number & " - " & Err.Description
    Resume Next
End Sub
 
Subroutine DeleteKeys calls the RegOpenKeyEx API function to open the key and then calls SHDeleteKey to delete it.
 
'Using Recursive System Version So Deleting Parent Deletes
' Children.
'Example Call: deleteKey HKEY_LOCAL_MACHINE,
' "Software\Microsoft\Windows\CurrentVersion\Run", "test"
Public Sub DeleteKeys(hType As Long, subKey As String, Key _
    As String, lResult As Long)
    'Dim lResult As Long
    Dim hKey As Long
    If RegOpenKeyEx(hType, subKey, 0, KEY_ALL_ACCESS, hKey) _
        = ERROR_NONE Then
        lResult = SHDeleteKey(hKey, Key)
        RegCloseKey hKey
    End If
End Sub
 
When you click the Microsoft Save button, the following code executes. It creates two values in the HKEY_CURRENT_USER\Software\MyCompany\Registration key named StringValue and UseValue. It fetches the values to verify that they were written correctly (and to show how to fetch values).
 
Private Sub cmdMS_Save_Click()
    Dim KeyValue As Variant, Result As Long
On Error GoTo cmdMS_Save_Click_Error
    
    Call CreateNewKey(HKEY_CURRENT_USER, _
        "Software\MyCompany\Registration", Result)
    If Result <> 0 Then
        MsgBox "Cannot Create Requested Key " & CStr(Result)
        Exit Sub
    End If
    
    Call SetKeyValue(HKEY_CURRENT_USER, _
        "Software\MyCompany\Registration", "StringValue", _
        "Hello MyCompany World", REG_SZ, Result)
    If Result <> 0 Then
        MsgBox "Cannot Set Key Value " & CStr(Result)
        Exit Sub
    Else
        Call QueryValue(HKEY_CURRENT_USER, _
            "Software\MyCompany\Registration", _
            "StringValue", KeyValue, Result)
        If Result <> 0 Then
            MsgBox "Cannot Read Key Value " & CStr(Result)
            Exit Sub
        Else
            MsgBox "Success StringValue: " & KeyValue
        End If
    End If
    
    Call SetKeyValue(HKEY_CURRENT_USER, _
        "Software\MyCompany\Registration", "UseValue", _
        "1234567890", REG_SZ, Result)
    Call QueryValue(HKEY_CURRENT_USER, _
        "Software\MyCompany\Registration", "UseValue", _
        KeyValue, Result)
    MsgBox "Success UseValue: " & KeyValue
    
    Exit Sub

cmdMS_Save_Click_Error:
    MsgBox Err.Number & " - " & Err.Description
    Resume Next
End Sub
 
 
Copyright © 1997-2006 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated