Results 1 to 3 of 3

Thread: Deleting values from the Registry using VB

  1. #1

    Thread Starter
    Frenzied Member mxnmx's Avatar
    Join Date
    Dec 2001
    Location
    I'm back...now!!!
    Posts
    1,396

    Deleting values from the Registry using VB

    I'm saving Keys in the registry, and I have a slight issue here. What if I wanna delete some values using VB?

  2. #2
    Bouncy Member darre1's Avatar
    Join Date
    May 2001
    Location
    Peterborough, UK
    Posts
    3,828
    there are two api functions:

    RegDeleteKey and RegDeleteValue

    you'll need em.
    Confucious say, "Man standing naked in biscuit barrel not necessarily ****ing crackers."

    Don't forget to format your code in your posts

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    VB Code:
    1. 'Windows 9x: The RegDeleteKey function deletes a key
    2. 'and all its descendents.Windows NT: The RegDeleteKey
    3. 'function deletes the specified key. This function
    4. 'cannot delete a key that has subkeys.
    5. Const HKEY_CURRENT_USER = &H80000001
    6. Const REG_OPTION_BACKUP_RESTORE = 4     ' open for backup or restore
    7. Const REG_OPTION_VOLATILE = 1           ' Key is not preserved when system is rebooted
    8. Const REG_OPTION_NON_VOLATILE = 0       ' Key is preserved when system is rebooted
    9. Const STANDARD_RIGHTS_ALL = &H1F0000
    10. Const SYNCHRONIZE = &H100000
    11. Const READ_CONTROL = &H20000
    12. Const STANDARD_RIGHTS_READ = (READ_CONTROL)
    13. Const STANDARD_RIGHTS_WRITE = (READ_CONTROL)
    14. Const KEY_CREATE_LINK = &H20
    15. Const KEY_CREATE_SUB_KEY = &H4
    16. Const KEY_ENUMERATE_SUB_KEYS = &H8
    17. Const KEY_NOTIFY = &H10
    18. Const KEY_QUERY_VALUE = &H1
    19. Const KEY_SET_VALUE = &H2
    20. Const KEY_READ = ((STANDARD_RIGHTS_READ Or KEY_QUERY_VALUE Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY) And (Not SYNCHRONIZE))
    21. Const KEY_WRITE = ((STANDARD_RIGHTS_WRITE Or KEY_SET_VALUE Or KEY_CREATE_SUB_KEY) And (Not SYNCHRONIZE))
    22. Const KEY_EXECUTE = (KEY_READ)
    23. Const KEY_ALL_ACCESS = ((STANDARD_RIGHTS_ALL Or KEY_QUERY_VALUE Or KEY_SET_VALUE Or KEY_CREATE_SUB_KEY Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY Or KEY_CREATE_LINK) And (Not SYNCHRONIZE))
    24. Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
    25. Private Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal hKey As Long, ByVal lpSubKey As String) As Long
    26. Private Declare Function RegCreateKeyEx Lib "advapi32.dll" Alias "RegCreateKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal Reserved As Long, ByVal lpClass As String, ByVal dwOptions As Long, ByVal samDesired As Long, lpSecurityAttributes As Any, phkResult As Long, lpdwDisposition As Long) As Long
    27. Private Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal Reserved As Long, ByVal samDesired As Long, phkResult As Long) As Long
    28. Private Sub Form_Load()
    29.     'KPD-Team 2000
    30.     'URL: [url]http://www.allapi.net/[/url]
    31.     'E-Mail: [email][email protected][/email]
    32.     Dim Result As Long
    33.     'Check if the specified key exists
    34.     RegOpenKeyEx HKEY_CURRENT_USER, "KPD-Team", 0, KEY_ALL_ACCESS, Result
    35.     'If the key doesn't exist, we create it
    36.     If Result = 0 Then
    37.         'Create a new key
    38.         RegCreateKeyEx HKEY_CURRENT_USER, "KPD-Team", 0, "REG_DWORD", REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, ByVal 0&, Result, Ret
    39.         If Result = 0 Then
    40.             MsgBox "Error while creating the Key!!"
    41.             Exit Sub
    42.         End If
    43.     End If
    44.     'Delete the key
    45.     RegDeleteKey Result, ""
    46.     'close the handle
    47.     RegCloseKey Result
    48. End Sub
    49. 'The RegDeleteValue function removes a named value from the 'specified registry key.
    50. Const REG_SZ = 1 ' Unicode nul terminated string
    51. Const REG_BINARY = 3 ' Free form binary
    52. Const HKEY_CURRENT_USER = &H80000001
    53. Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
    54. Private Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
    55. Private Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal lpValueName As String) As Long
    56. Private Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
    57. Private Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, lpData As Any, lpcbData As Long) As Long
    58. Private Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpData As Any, ByVal cbData As Long) As Long
    59. Function RegQueryStringValue(ByVal hKey As Long, ByVal strValueName As String) As String
    60.     Dim lResult As Long, lValueType As Long, strBuf As String, lDataBufSize As Long
    61.     'retrieve nformation about the key
    62.     lResult = RegQueryValueEx(hKey, strValueName, 0, lValueType, ByVal 0, lDataBufSize)
    63.     If lResult = 0 Then
    64.         If lValueType = REG_SZ Then
    65.             'Create a buffer
    66.             strBuf = String(lDataBufSize, Chr$(0))
    67.             'retrieve the key's content
    68.             lResult = RegQueryValueEx(hKey, strValueName, 0, 0, ByVal strBuf, lDataBufSize)
    69.             If lResult = 0 Then
    70.                 'Remove the unnecessary chr$(0)'s
    71.                 RegQueryStringValue = Left$(strBuf, InStr(1, strBuf, Chr$(0)) - 1)
    72.             End If
    73.         ElseIf lValueType = REG_BINARY Then
    74.             Dim strData As Integer
    75.             'retrieve the key's value
    76.             lResult = RegQueryValueEx(hKey, strValueName, 0, 0, strData, lDataBufSize)
    77.             If lResult = 0 Then
    78.                 RegQueryStringValue = strData
    79.             End If
    80.         End If
    81.     End If
    82. End Function
    83. Function GetString(hKey As Long, strPath As String, strValue As String)
    84.     Dim Ret
    85.     'Open the key
    86.     RegOpenKey hKey, strPath, Ret
    87.     'Get the key's content
    88.     GetString = RegQueryStringValue(Ret, strValue)
    89.     'Close the key
    90.     RegCloseKey Ret
    91. End Function
    92. Sub SaveString(hKey As Long, strPath As String, strValue As String, strData As String)
    93.     Dim Ret
    94.     'Create a new key
    95.     RegCreateKey hKey, strPath, Ret
    96.     'Save a string to the key
    97.     RegSetValueEx Ret, strValue, 0, REG_SZ, ByVal strData, Len(strData)
    98.     'close the key
    99.     RegCloseKey Ret
    100. End Sub
    101. Sub SaveStringLong(hKey As Long, strPath As String, strValue As String, strData As String)
    102.     Dim Ret
    103.     'Create a new key
    104.     RegCreateKey hKey, strPath, Ret
    105.     'Set the key's value
    106.     RegSetValueEx Ret, strValue, 0, REG_BINARY, CByte(strData), 4
    107.     'close the key
    108.     RegCloseKey Ret
    109. End Sub
    110. Sub DelSetting(hKey As Long, strPath As String, strValue As String)
    111.     Dim Ret
    112.     'Create a new key
    113.     RegCreateKey hKey, strPath, Ret
    114.     'Delete the key's value
    115.     RegDeleteValue Ret, strValue
    116.     'close the key
    117.     RegCloseKey Ret
    118. End Sub
    119. Private Sub Command1_Click()
    120.     Dim strString As String
    121.     'Ask for a value
    122.     strString = InputBox("Please enter a value between 0 and 255 to be saved as a binary value in the registry.", App.Title)
    123.     If strString = "" Or Val(strString) > 255 Or Val(strString) < 0 Then
    124.         MsgBox "Invalid value entered ...", vbExclamation + vbOKOnly, App.Title
    125.         Exit Sub
    126.     End If
    127.     'Save the value to the registry
    128.     SaveStringLong HKEY_CURRENT_USER, "KPD-Team", "BinaryValue", CByte(strString)
    129. End Sub
    130. Private Sub Command2_Click()
    131.     'Get a string from the registry
    132.     Ret = GetString(HKEY_CURRENT_USER, "KPD-Team", "BinaryValue")
    133.     If Ret = "" Then MsgBox "No value found !", vbExclamation + vbOKOnly, App.Title: Exit Sub
    134.     MsgBox "The value is " + Ret, vbOKOnly + vbInformation, App.Title
    135. End Sub
    136. Private Sub Command3_Click()
    137.     'Delete the setting from the registry
    138.     DelSetting HKEY_CURRENT_USER, "KPD-Team", "BinaryValue"
    139.     MsgBox "The value was deleted ...", vbInformation + vbOKOnly, App.Title
    140. End Sub
    141. Private Sub Form_Load()
    142.     'KPD-Team 1998
    143.     'URL: [url]http://www.allapi.net/[/url]
    144.     'E-Mail: [email][email protected][/email]
    145.     Command1.Caption = "Set Value"
    146.     Command2.Caption = "Get Value"
    147.     Command3.Caption = "Delete Value"
    148. End Sub

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