Module to Read from the Windows Registry

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • NeoPa
    Recognized Expert Moderator MVP
    • Oct 2006
    • 32668

    Module to Read from the Windows Registry

    This can be built on, but I've not needed to so far.
    This is used in my database(s) and stored as modOS.

    [CODE=vb]
    Option Compare Database
    Option Explicit

    Public Const conHKCR = &H80000000
    Public Const conHKCU = &H80000001
    Public Const conHKLM = &H80000002
    Public Const conHKU = &H80000003
    Public Const conStandardRigh tsAll = &H1F0000
    Public Const conReadControl = &H20000
    Public Const conStandardRigh tsRead = (conReadControl )
    Public Const conRegSz = 1
    Public Const conOK = 0&
    Public Const conKeyQueryValu e = &H1
    Public Const conKeySetValue = &H2
    Public Const conKeyCreateLin k = &H20
    Public Const conKeyCreateSub Key = &H4
    Public Const conKeyEnumerate SubKeys = &H8
    Public Const conKeyNotify = &H10
    Public Const conSynchronise = &H100000
    Public Const conRegOptionNon Volatile = 0
    Public Const conKeyAllAccess = ((conStandardRi ghtsAll Or _
    conKeyQueryValu e Or _
    conKeySetValue Or _
    conKeyCreateSub Key Or _
    conKeyEnumerate SubKeys Or _
    conKeyNotify Or _
    conKeyCreateLin k) And _
    (Not conSynchronise) )
    Public Const conKeyRead = ((conReadContro l Or _
    conKeyQueryValu e Or _
    conKeyEnumerate SubKeys Or _
    conKeyNotify) And _
    (Not conSynchronise) )

    Private Declare Function RegOpenKeyEx Lib "advapi32.d ll" _
    Alias "RegOpenKey ExA" (ByVal hKey As Long, _
    ByVal lpSubKey As String, _
    ByVal ulOptions As Long, _
    ByVal samDesired As Long, _
    phkResult As Long) As Long
    Private Declare Function RegCloseKey Lib "advapi32.d ll" (ByVal hKey As Long) _
    As Long
    Private Declare Function RegQueryValueEx Str Lib "advapi32.d ll" _
    Alias "RegQueryValueE xA" (ByVal hKey As Long, _
    ByVal lpValueName As String, _
    ByVal lpReserved As Long, _
    lpType As Long, _
    ByVal lpData As String, _
    lpcbData As Long) As Long

    Public Function RegRead(ByVal lngHive As Long, _
    ByVal strKey As String, _
    ByVal strValue As String) As Variant
    Dim intIdx As Integer, intHK As Integer
    Dim strWork As String
    Dim lngRet As Long, lngLen As Long, lngHKey As Long, lngType As Long

    RegRead = Null
    strKey = strKey & Chr(0)
    lngRet = RegOpenKeyEx(ln gHive, strKey, 0, conKeyRead, lngHKey)
    If lngRet = conOK Then
    'Create buffer to store value
    strWork = Space(255)
    lngLen = 255
    lngRet = RegQueryValueEx Str(lngHKey, _
    strValue, _
    0&, _
    lngType, _
    strWork, _
    lngLen)
    RegRead = Left(strWork, lngLen - 1)
    If Len(RegRead) = 254 Then RegRead = Null
    'Close key
    Call RegCloseKey(lng HKey)
    End If
    End Function[/CODE]
  • NeoPa
    Recognized Expert Moderator MVP
    • Oct 2006
    • 32668

    #2
    It's just been brought to my attention that a few basics about the Windows Registry might be helpful here, for a newcomer to appreciate what is required even to call the function. Let me see what I can make clearer.

    The Registry on a particular PC at any one time consists of four main hives. These are the base-points of separate structures and all data is found starting from the Hive. The four Hives are :
    1. HKey_Classes_Ro ot - A list of the PC's file associations.
    2. HKey_Current_Us er - The User Profile of the currently logged in user. This can also be found within HKey_Users under the relevant profile.
    3. HKey_Local_Mach ine - The Machine Profile for the PC.
    4. HKey_Users - The User Profiles of all users who've used the PC.


    In the code, the values that represent these hives are declared as public (so can be used in your own code as long as this module exists in your project). They are set as :
    1. HKey_Classes_Ro ot - conHKCR (= &H80000000)
    2. HKey_Current_Us er - conHKCU (= &H80000001)
    3. HKey_Local_Mach ine - conHKLM (= &H80000002)
    4. HKey_Users - conHKU (= &H80000003)


    Within each Hive there are Keys which are the branches of the tree and Values which are the nodes (where the actual data is stored). Keys, like folders in a file structure, are recursive and can have multiple parent keys. EG. To hold information about Window Metrics there are Values reflecting these, and they are found in the Key HKey_Current_Us er\Control Panel\Desktop\W indowMetrics\. This Key is a child node itself of HKey_Current_Us er\Control Panel\Desktop\. So, Keys can hold both Keys and Values, but Values hold nothing but the values contained within them.

    So, to call the ReadRegistry() function for one of the WindowMetrics Values, say BorderWidth, we would use something like :
    Code:
    Dim strBorderWidth As String
    
    strBorderWidth = RegRead(conHKCU, _
                             "Control Panel\Desktop\WindowMetrics", _
                             "BorderWidth")
    Hopefully, this makes it easier for those less experienced with using the Registry, to take their first steps.

    NB. This function enables you to read the values in the registry. I believe this is quite safe. Any sort of updating of the Windows Registry though, comes with a strong warning. It is a very powerful tool, and like most such in life, can easily cause a great deal of damage. The stories of those who've had to rebuild their PCs, after making changes without a backup, are legion. Don't let it happen to you!
    Last edited by NeoPa; Oct 29 '10, 01:49 PM.

    Comment

    Working...