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
 
 
 
 
 
TitleGet Registry values
KeywordsRegistry, regedit
CategoriesTips and Tricks
 
!!! WARNING !!!
Modifying the Registry can seriously damage your system. If you change the wrong values, you can make your system unbootable. Don't mess with the registry unless you know what you are doing and make a backup first in any case.

Use RegOpenKeyEx to open the Registry key. Use RegQueryValueEx to get the length of the desired subkey value. Allocate a buffer big enough to hold the value and then use RegQueryValueEx again to get it. Remove the trailing Null character from the result and then close the open key using RegCloseKey.

 
' Return a registry key value.
Private Function GetRegKeyValue(ByVal root As Long, ByVal _
    key_name As String, ByVal subkey_name As String) As _
    String
Dim hKey As Long
Dim value As String
Dim length As Long
Dim value_type As Long

    ' Open the key.
    If RegOpenKeyEx(root, key_name, _
        0&, KEY_QUERY_VALUE, hKey) <> ERROR_SUCCESS _
    Then
        MsgBox "Error opening key."
        Exit Function
    End If

    ' Get the subkey's size.
    If RegQueryValueEx(hKey, subkey_name, _
        0&, value_type, ByVal 0&, length) _
            <> ERROR_SUCCESS _
    Then
        MsgBox "Error getting subkey length."
    End If

    ' Get the subkey's value.
    value = Space$(length)
    If RegQueryValueEx(hKey, subkey_name, _
        0&, value_type, ByVal value, length) _
            <> ERROR_SUCCESS _
    Then
        MsgBox "Error getting subkey value."
    Else
        ' Remove the trailing null character.
        GetRegKeyValue = Left$(value, length - 1)
    End If

    ' Close the key.
    If RegCloseKey(hKey) <> ERROR_SUCCESS Then
        MsgBox "Error closing key."
    End If
End Function
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated