Results 1 to 20 of 20

Thread: Working with Windows Registry using Visual Basic 6 - A complete Tutorial

Hybrid View

  1. #1

    Thread Starter
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Writing to The Windows Registry

    To write to the registry we will use the SaveSetting command. SaveSetting Saves or creates an application entry in the application's entry in the Windows Registry. Visual Basic, by default using GetSetting and SaveSetting allows reading and writing only to the VB and VBA Programs key under HKEY_Current_User\Software.

    Syntax

    SaveSetting appname, section, key, setting

    The SaveSetting statement syntax has these named arguments:

    Part Description
    appname Required String expression containing the name of the application orproject to which the setting applies.
    section Required String expression containing the name of the section where the key setting is being saved.
    key Required String expression containing the name of the key setting being saved.
    setting Required Expression containing the value that key is being set to.

    Lets understand the syntax...
    You can compare the registry to a database which has fields and records in it...
    The first three arguments of the SaveSetting statement can be compared to fields in a record. The field names just happen to be appname, section, key and setting and we need to simply supply values to this via SaveSetting.

    The best thing about the SaveSetting statement is that you can specify any values you want for appname, section and key. However please ensure that you pick something meaningful. The last field "setting" holds the value that we are really interested in. Lets try it with an example which uses the SaveSetting statement to make entries in the Windows registry for the MyApp application.

    vb Code:
    1. '~~> Place some settings in the registry.
    2. SaveSetting appname := "MyApp", section := "Startup", key := "Top", setting := 75
    3. 'Or
    4. SaveSetting "MyApp","Startup", "Top", 75

    What this code does is that it saves the .Top value of your startup form in the Windows Registry.

    Once this line of code executes, a 'record' is written to the Windows Registry with these four 'field' values. If you want to check this out for yourself, you can start the Registry Editor again, and search for the value by selecting Edit-Find from the Registry Editor's Menu Bar.



    Last edited by Siddharth Rout; Mar 26th, 2009 at 05:11 AM.
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  2. #2

    Thread Starter
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Reading from The Windows Registry

    Now that we have the MyApp's startup form's .Top value saved in the Windows Registry, we need to access the value whenever our program starts up. We'll use the Visual Basic GetSetting function to read this value from the Windows Registry, and a good place to execute that function is in the Load Event Procedure of the form. Before I show you that code, let me show you the format for the GetSetting function.

    Syntax

    GetSetting(appname, section, key[, default])

    The GetSetting function syntax has thesenamed arguments:

    Part Description
    appname Required String expression containing the name of the application or project whose key setting is requested.
    section Required String expression containing the name of the section where the key setting is found.
    key Required String expression containing the name of the key setting to return.
    default Optional Expression containing the value to return if no value is set in the key setting. If omitted, default is assumed to be a zero-length string ("").

    As you can see, the first three arguments of the GetSetting function are identical to the SaveSetting statement. The difference lies in the fourth argument. With the SaveSetting Statement, the fourth argument was the value to be written to the Registry. For the GetSetting function, the fourth argument is an optional argument used to specify a default value to be returned in the event that the entry specified cannot be located in the Windows Registry.

    Here's the code to place in the Load Event Procedure of the Startup Form. I have inserted 2 message box in the code below so that you can understand how it works...

    vb Code:
    1. Private Sub Form_Load()
    2.     '~~> Check what is the current top value
    3.     MsgBox Form1.Top
    4.     '~~> Get value from registry
    5.     Form1.Top = GetSetting("MyApp", "Startup", "Top", 25)
    6.     '~~> Check the new top value
    7.     MsgBox Form1.Top
    8. End Sub
    Last edited by Siddharth Rout; Mar 26th, 2009 at 03:51 AM.
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  3. #3

    Thread Starter
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Deleting entries in The Windows Registry

    To delete the entry that you made in the registry, you need to use DeleteSetting statement

    Syntax

    DeleteSetting appname, section[, key]

    The DeleteSetting statement syntax has these named arguments:

    Part Description
    appname Required String expression containing the name of the application or project to which the section or key setting applies.
    section Required String expression containing the name of the section where the key setting is being deleted. If only appname and section are provided, the specified section is deleted along with all related key settings.
    key Optional String expression containing the name of the key setting being deleted.

    You can use it to delete the appname, section and key entry, similar to the way you created the entry to begin with. For example

    vb Code:
    1. DeleteSetting "MyApp", "Startup", "Top

    After deleting the key, try searching the registry again and you will see that the key has been deleted.
    Last edited by Siddharth Rout; Mar 26th, 2009 at 03:57 AM.
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  4. #4

    Thread Starter
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    GetAllSettings

    Visual Basic provides us a function called GetAllSettings which can be used to return the Registry entries for a particular Appname and Setting. Here's the format for the GetAllSettings function. This is used for example in case we loose track of the entries we have made in the windows registry.

    Syntax

    GetAllSettings(appname, section)

    The GetAllSettings function syntax has these named arguments:

    Part Description
    appname Required String expression containing the name of the application orproject whose key settings are requested.
    section Required String expression containing the name of the section whose key settings are requested. GetAllSettings returns aVariant whose contents is a two-dimensional array of strings containing all the key settings in the specified section and their corresponding values.

    Once GetAllSettings executes and returns a two dimensional array, we can use the Lbound and Ubound functions, in conjunction with a For-Next Loop to move through the elements of the Array and display them as shown in the code below

    vb Code:
    1. Private Sub Command2_Click()
    2.     Dim MyAppSettings As Variant, i As Long
    3.    
    4.     MyAppSettings = GetAllSettings("MyApp", "Startup")
    5.    
    6.     '~~> Loop through the array
    7.     For i = LBound(MyAppSettings, 1) To UBound(MyAppSettings, 1)
    8.         '~~> Display the values in the array
    9.         MsgBox MyAppSettings(i, 0), MyAppSettings(i, 1)
    10.     Next i
    11. End Sub
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  5. #5

    Thread Starter
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Checking if a key created by SaveSetting exists using vb 6.0 code

    Assuming that you haven't deleted the key that we created above, lets now try to find the key we created.

    Like I mentioned above, Visual Basic, by default using GetSetting and SaveSetting allows reading and writing only to the VB and VBA Programs key under HKEY_Current_User\Software.

    Create a form and place a Text Box and a Command Button in it. Place the code in the general declaration of the form.

    vb Code:
    1. Private Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" _
    2. (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, _
    3. ByVal samDesired As Long, phkResult As Long) As Long
    4.  
    5. Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As _
    6. Long
    7.  
    8. Const KEY_READ = &H20019
    9. Const HKEY_CURRENT_USER = &H80000001
    10.  
    11. '~~> Return True if a Registry key exists
    12. Function CheckRegistryKey(ByVal hKey As Long, ByVal KeyName As String) As Boolean
    13.     Dim handle As Long
    14.     '~~> Try to open the key
    15.     If RegOpenKeyEx(hKey, KeyName, 0, KEY_READ, handle) = 0 Then
    16.         '~~> The key exists
    17.         CheckRegistryKey = True
    18.         '~~> Close it before exiting
    19.         RegCloseKey handle
    20.     End If
    21. End Function
    22.  
    23. Private Sub Command1_Click()
    24. '~~> Visual Basic, by default using GetSetting and SaveSetting allows reading and
    25. '~~> writing only to the VB and VBA Programs key under HKEY_Current_User\Software.
    26.     Dim strSubKey As String
    27.     strSubKey = Trim(Text1.Text)
    28.     '~~> will return True if it exists
    29.     MsgBox CheckRegistryKey(HKEY_CURRENT_USER, strSubKey)
    30. End Sub

    Run the Form and type "Software\VB and VBA Program Settings\MyApp" in the Text Box. Once done, click the Command Button. you will get the message "True" if the key exists or "False" if it doesn't.
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  6. #6

    Thread Starter
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Writing to any portion of the registry for which you have permissions

    I have mentioned the constants for the hives in the Windows Registry so that you can experiment if you want to...

    vb Code:
    1. '~~> Constants for the Registry Hive in case you want to experiment
    2. Const HKEY_CLASSES_ROOT = &H80000000
    3. Const HKEY_CURRENT_USER = &H80000001
    4. Const HKEY_LOCAL_MACHINE = &H80000002
    5. Const HKEY_USERS = &H80000003
    6.  
    7. '~~> Other Constants for RegCreateKeyEx
    8. Const REG_OPTION_BACKUP_RESTORE = 4   '<~~ open for backup or restore
    9. Const REG_OPTION_VOLATILE = 1         '<~~ Key isn't preserved if system is rebooted
    10. Const REG_OPTION_NON_VOLATILE = 0     '<~~ Key is preserved if system is rebooted
    11. Const STANDARD_RIGHTS_ALL = &H1F0000
    12. Const SYNCHRONIZE = &H100000
    13. Const READ_CONTROL = &H20000
    14. Const STANDARD_RIGHTS_READ = (READ_CONTROL)
    15. Const STANDARD_RIGHTS_WRITE = (READ_CONTROL)
    16. Const KEY_CREATE_LINK = &H20
    17. Const KEY_CREATE_SUB_KEY = &H4
    18. Const KEY_ENUMERATE_SUB_KEYS = &H8
    19. Const KEY_NOTIFY = &H10
    20. Const KEY_QUERY_VALUE = &H1
    21. Const KEY_SET_VALUE = &H2
    22. Const KEY_READ = ((STANDARD_RIGHTS_READ Or KEY_QUERY_VALUE Or _
    23. KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY) And (Not SYNCHRONIZE))
    24. Const KEY_WRITE = ((STANDARD_RIGHTS_WRITE Or KEY_SET_VALUE Or _
    25. KEY_CREATE_SUB_KEY) And (Not SYNCHRONIZE))
    26. Const KEY_EXECUTE = (KEY_READ)
    27. Const KEY_ALL_ACCESS = ((STANDARD_RIGHTS_ALL Or KEY_QUERY_VALUE Or _
    28. KEY_SET_VALUE Or KEY_CREATE_SUB_KEY Or KEY_ENUMERATE_SUB_KEYS Or _
    29. KEY_NOTIFY Or KEY_CREATE_LINK) And (Not SYNCHRONIZE))
    30.  
    31. '~~> The RegCloseKey function releases the handle of the specified key.
    32. Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
    33.  
    34. '~~> The RegDeleteKey function deletes the specified key
    35. Private Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" _
    36. (ByVal hKey As Long, ByVal lpSubKey As String) As Long
    37.  
    38. '~~> The RegCreateKeyEx function creates the specified key. If the key already exists
    39. 'in the registry, the function opens it.
    40. Private Declare Function RegCreateKeyEx Lib "advapi32.dll" Alias "RegCreateKeyExA" _
    41. (ByVal hKey As Long, ByVal lpSubKey As String, ByVal Reserved As Long, ByVal _
    42. lpClass As String, ByVal dwOptions As Long, ByVal samDesired As Long, _
    43. lpSecurityAttributes As Any, phkResult As Long, lpdwDisposition As Long) As Long
    44.  
    45. '~~> The RegOpenKeyEx function opens the specified key.
    46. Private Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" _
    47. (ByVal hKey As Long, ByVal lpSubKey As String, ByVal Reserved As Long, ByVal _
    48. samDesired As Long, phkResult As Long) As Long
    49.  
    50. Private Sub Command1_Click()
    51.     '~~> Code adapted from APIGuide <~~'
    52.     Dim Result As Long
    53.    
    54.     '~~> Check if the specified key "koolsid" exists under HKEY_CLASSES_ROOT
    55.     RegOpenKeyEx HKEY_CLASSES_ROOT, "koolsid", 0, KEY_ALL_ACCESS, Result
    56.    
    57.     '~~> If the key doesn't exist, we create it
    58.     If Result = 0 Then
    59.         '~~> Create a new key under HKEY_CLASSES_ROOT
    60.         RegCreateKeyEx HKEY_CLASSES_ROOT, "koolsid", 0, "REG_DWORD", _
    61.         REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, ByVal 0&, Result, ret
    62.        
    63.         If Result = 0 Then
    64.             MsgBox "Error while creating the Key!!"
    65.             Exit Sub
    66.         End If
    67.         '~~> Inform user that the key has been created
    68.         MsgBox "Key Created..."
    69.     End If
    70.    
    71.     '~~> Inform user before deleting the key
    72.     MsgBox "Deleting the Key..."
    73.    
    74.     '~~> Delete the key
    75.     RegDeleteKey Result, ""
    76.    
    77.     '~~> Close the handle
    78.     RegCloseKey Result
    79. End Sub
    Last edited by Siddharth Rout; Mar 26th, 2009 at 06:23 AM.
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  7. #7

    Thread Starter
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Find keys in the Windows Registry

    Based on Article by Microsoft.Com....

    Using API functions to programmatically find keys in the system registry. The sample demonstrates searches of root subkeys for specified text strings. An option to perform case-sensitive text search is also available.

    The example uses the RegEnumKeyEx API function to enumerate subkeys of a specified open registry key. The function retrieves information about one subkey each time it is called.
    Attached Files Attached Files
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width