473,386 Members | 1,823 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,386 developers and data experts.

Module to Read from the Windows Registry

NeoPa
32,556 Expert Mod 16PB
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
1 8363
NeoPa
32,556 Expert Mod 16PB
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

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Nuno Paquete | last post by:
Hi group. How can I get PHP working under windows, with Apache web server? Where can I find the module? I suppose that I need to install a module like in Linux... Thanks in advance. Nuno...
14
by: Alex Hunsley | last post by:
Does python provide a way to dynamically use modules and/or classes? I'm thinking in the vein of Java's Class.forName. As a pseudocode example, I'm looking for the following ability: ...
0
by: The Jetman | last post by:
I'm trying to port PyWin into a limited version of Windows XP, called BartPE. Essentially, it's a matter of taking the installed elements of most WinApps (ie. registry settings and files) and...
2
by: sriram pasham | last post by:
Hi Could any one tell me how to access(read) a registry key using javascript from a webpage(ASP page). Thanks in advance. Sriram *** Sent via Developersdex http://www.developersdex.com ***...
2
by: fabrice | last post by:
hello, Is it possible to read a registry key in ASP, and use the value in an application via a session variable. I'm using II6 on windows server 2003. I'd like to create a key in the registry...
1
by: Borka | last post by:
Hi all! I have some questions. 1. I write script which controls registry settings, which must work on every computer. If I try JFSO = new ActiveXObject("Scripting.FileSystemObject"); JWS =...
3
by: 3c273 | last post by:
Does it signify something? Just curious. Louis
0
by: John Allman | last post by:
Hi all, I'm trying to create a setup which allows a program to request an object using strings and get an object of that type. It appears to be mostly working but i have difficulties if i...
11
by: vovan | last post by:
I'm using this approach to read from registry. Dim f As New RegistryPermission(RegistryPermissionAccess.AllAccess, _ "HKEY_LOCAL_MACHINE\SOFTWARE\SRS Enterprises\Coordinator\Settings") 'Vlad...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.