By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,652 Members | 1,275 Online
Bytes IT Community
Submit an Article
Got Smarts?
Share your bits of IT knowledge by writing an article on Bytes.

Module to Read from the Windows Registry

NeoPa
Expert Mod 15k+
P: 31,266
This can be built on, but I've not needed to so far.
This is used in my database(s) and stored as modOS.

Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Public Const conHKCR = &H80000000
  5. Public Const conHKCU = &H80000001
  6. Public Const conHKLM = &H80000002
  7. Public Const conHKU = &H80000003
  8. Public Const conStandardRightsAll = &H1F0000
  9. Public Const conReadControl = &H20000
  10. Public Const conStandardRightsRead = (conReadControl)
  11. Public Const conRegSz = 1
  12. Public Const conOK = 0&
  13. Public Const conKeyQueryValue = &H1
  14. Public Const conKeySetValue = &H2
  15. Public Const conKeyCreateLink = &H20
  16. Public Const conKeyCreateSubKey = &H4
  17. Public Const conKeyEnumerateSubKeys = &H8
  18. Public Const conKeyNotify = &H10
  19. Public Const conSynchronise = &H100000
  20. Public Const conRegOptionNonVolatile = 0
  21. Public Const conKeyAllAccess = ((conStandardRightsAll Or _
  22.                                 conKeyQueryValue Or _
  23.                                 conKeySetValue Or _
  24.                                 conKeyCreateSubKey Or _
  25.                                 conKeyEnumerateSubKeys Or _
  26.                                 conKeyNotify Or _
  27.                                 conKeyCreateLink) And _
  28.                                (Not conSynchronise))
  29. Public Const conKeyRead = ((conReadControl Or _
  30.                             conKeyQueryValue Or _
  31.                             conKeyEnumerateSubKeys Or _
  32.                             conKeyNotify) And _
  33.                            (Not conSynchronise))
  34.  
  35. Private Declare Function RegOpenKeyEx Lib "advapi32.dll" _
  36.     Alias "RegOpenKeyExA" (ByVal hKey As Long, _
  37.                            ByVal lpSubKey As String, _
  38.                            ByVal ulOptions As Long, _
  39.                            ByVal samDesired As Long, _
  40.                            phkResult As Long) As Long
  41. Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) _
  42.                              As Long
  43. Private Declare Function RegQueryValueExStr Lib "advapi32.dll" _
  44.     Alias "RegQueryValueExA" (ByVal hKey As Long, _
  45.                               ByVal lpValueName As String, _
  46.                               ByVal lpReserved As Long, _
  47.                               lpType As Long, _
  48.                               ByVal lpData As String, _
  49.                               lpcbData As Long) As Long
  50.  
  51. Public Function RegRead(ByVal lngHive As Long, _
  52.                         ByVal strKey As String, _
  53.                         ByVal strValue As String) As Variant
  54.     Dim intIdx As Integer, intHK As Integer
  55.     Dim strWork As String
  56.     Dim lngRet As Long, lngLen As Long, lngHKey As Long, lngType As Long
  57.  
  58.     RegRead = Null
  59.     strKey = strKey & Chr(0)
  60.     lngRet = RegOpenKeyEx(lngHive, strKey, 0, conKeyRead, lngHKey)
  61.     If lngRet = conOK Then
  62.         'Create buffer to store value
  63.         strWork = Space(255)
  64.         lngLen = 255
  65.         lngRet = RegQueryValueExStr(lngHKey, _
  66.                                     strValue, _
  67.                                     0&, _
  68.                                     lngType, _
  69.                                     strWork, _
  70.                                     lngLen)
  71.         RegRead = Left(strWork, lngLen - 1)
  72.         If Len(RegRead) = 254 Then RegRead = Null
  73.         'Close key
  74.         Call RegCloseKey(lngHKey)
  75.     End If
  76. End Function
Mar 12 '07 #1
Share this Article
Share on Google+
1 Comment


NeoPa
Expert Mod 15k+
P: 31,266
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_Root - A list of the PC's file associations.
  2. HKey_Current_User - The User Profile of the currently logged in user. This can also be found within HKey_Users under the relevant profile.
  3. HKey_Local_Machine - 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_Root - conHKCR (= &H80000000)
  2. HKey_Current_User - conHKCU (= &H80000001)
  3. HKey_Local_Machine - 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_User\Control Panel\Desktop\WindowMetrics\. This Key is a child node itself of HKey_Current_User\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 :
Expand|Select|Wrap|Line Numbers
  1. Dim strBorderWidth As String
  2.  
  3. strBorderWidth = RegRead(conHKCU, _
  4.                          "Control Panel\Desktop\WindowMetrics", _
  5.                          "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!
Oct 29 '10 #2