Results 1 to 20 of 20

Thread: Deleting a value from the registry? [RESOLVED]

  1. #1

    Thread Starter
    Supreme User Madboy's Avatar
    Join Date
    Oct 2003
    Location
    England
    Posts
    3,253

    Deleting a value from the registry? [RESOLVED]

    How do you delete an individual value? I tried :


    VB Code:
    1. Private Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal lpValueName As String) As Long
    2.  
    3. Private Function DeleteValue(ByVal hKey As Long, ByVal strPath As String, ByVal strValue As String)
    4.     Dim keyhand As Long
    5.     Dim r As Long
    6.     r = RegOpenKey(hKey, strPath, keyhand)
    7.     r = RegDeleteValue(keyhand, strValue)
    8.     r = RegCloseKey(keyhand)
    9. End Function
    10.  
    11. [B]DeleteValue[/B] &H80000001, "HKEY_CURRENT_USER\Software\Game Maker\Version 5\Preferences\Recent0"

    Thanks, it errors "Argument Not Optional" (Bold highlighted)
    Last edited by Madboy; Jan 25th, 2004 at 12:15 PM.

  2. #2
    Frenzied Member
    Join Date
    May 2003
    Location
    So Cal
    Posts
    1,564
    You have an opening " at HKEY but no closing " at Recent0, not sure if that matters or if you just pasted here as a typo.

  3. #3

    Thread Starter
    Supreme User Madboy's Avatar
    Join Date
    Oct 2003
    Location
    England
    Posts
    3,253
    Oops, typo

  4. #4
    Frenzied Member
    Join Date
    May 2003
    Location
    So Cal
    Posts
    1,564

  5. #5

    Thread Starter
    Supreme User Madboy's Avatar
    Join Date
    Oct 2003
    Location
    England
    Posts
    3,253
    Oops, this wasnt meant to be a double post, i just moved my new problem to a new thread, as my other problem had been resolved thus noone else was looking at it.

  6. #6
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171
    Either change the function to this (note the bold) :

    VB Code:
    1. Private Function DeleteValue(ByVal hKey As Long, ByVal strPath As String, [b]Optional[/b] ByVal strValue As String)

    or pass a value for strValue... If you don't want anything pass vbNullString...


    Has someone helped you? Then you can Rate their helpful post.

  7. #7

    Thread Starter
    Supreme User Madboy's Avatar
    Join Date
    Oct 2003
    Location
    England
    Posts
    3,253
    I take it i need to close that value now, how can i achieve this?

    Thanks

  8. #8
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171
    Originally posted by Madboy
    I take it i need to close that value now, how can i achieve this?

    Thanks
    I don't quite get it... Could you rephrase or something?


    Has someone helped you? Then you can Rate their helpful post.

  9. #9

    Thread Starter
    Supreme User Madboy's Avatar
    Join Date
    Oct 2003
    Location
    England
    Posts
    3,253
    Well, i changed the API to the one you boldened (it worked). Put when i look for it in the registry, it is still there. Do i need to close it, refresh it or something?

  10. #10
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171
    This should help :

    VB Code:
    1. Const HKEY_CURRENT_USER = &H80000001
    2. Const REG_OPTION_BACKUP_RESTORE = 4     ' open for backup or restore
    3. Const REG_OPTION_VOLATILE = 1           ' Key is not preserved when system is rebooted
    4. Const REG_OPTION_NON_VOLATILE = 0       ' Key is preserved when system is rebooted
    5. Const STANDARD_RIGHTS_ALL = &H1F0000
    6. Const SYNCHRONIZE = &H100000
    7. Const READ_CONTROL = &H20000
    8. Const STANDARD_RIGHTS_READ = (READ_CONTROL)
    9. Const STANDARD_RIGHTS_WRITE = (READ_CONTROL)
    10. Const KEY_CREATE_LINK = &H20
    11. Const KEY_CREATE_SUB_KEY = &H4
    12. Const KEY_ENUMERATE_SUB_KEYS = &H8
    13. Const KEY_NOTIFY = &H10
    14. Const KEY_QUERY_VALUE = &H1
    15. Const KEY_SET_VALUE = &H2
    16. Const KEY_READ = ((STANDARD_RIGHTS_READ Or KEY_QUERY_VALUE Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY) And (Not SYNCHRONIZE))
    17. Const KEY_WRITE = ((STANDARD_RIGHTS_WRITE Or KEY_SET_VALUE Or KEY_CREATE_SUB_KEY) And (Not SYNCHRONIZE))
    18. Const KEY_EXECUTE = (KEY_READ)
    19. 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))
    20. Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
    21. Private Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal hKey As Long, ByVal lpSubKey As String) As Long
    22. 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
    23. 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
    24. Private Sub Form_Load()
    25.     'KPD-Team 2000
    26.     'URL: [url]http://www.allapi.net/[/url]
    27.     'E-Mail: [email][email protected][/email]
    28.     Dim Result As Long
    29.     'Check if the specified key exists
    30.     RegOpenKeyEx HKEY_CURRENT_USER, "KPD-Team", 0, KEY_ALL_ACCESS, Result
    31.     'If the key doesn't exist, we create it
    32.     If Result = 0 Then
    33.         'Create a new key
    34.         RegCreateKeyEx HKEY_CURRENT_USER, "KPD-Team", 0, "REG_DWORD", REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, ByVal 0&, Result, Ret
    35.         If Result = 0 Then
    36.             MsgBox "Error while creating the Key!!"
    37.             Exit Sub
    38.         End If
    39.     End If
    40.     'Delete the key
    41.     RegDeleteKey Result, ""
    42.     'close the handle
    43.     RegCloseKey Result
    44. End Sub


    Has someone helped you? Then you can Rate their helpful post.

  11. #11

    Thread Starter
    Supreme User Madboy's Avatar
    Join Date
    Oct 2003
    Location
    England
    Posts
    3,253
    I have all them and more in VB book, it just doesnt tell me how to use it. This is what it says:

    The RegDeleteKey function deletes a key and all of its subkeys and values from the registry. The RegDeleteValue function deletes a value from the registry. Both of these functions require an open key handle. Note that the handle still needs to be closed, even after the key has been deleted.
    Unless ive accidentally deleted another key.

  12. #12
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171
    OK, hang on for a sec... I'll look through some modules I have on my PC and find one that I know works from an older project of mine...


    Has someone helped you? Then you can Rate their helpful post.

  13. #13
    The picture isn't missing BuggyProgrammer's Avatar
    Join Date
    Oct 2000
    Location
    Vancouver, Canada
    Posts
    5,217
    how about try:

    VB Code:
    1. Const HKEY_CURRENT_USER = &H80000001
    2. Const REG_OPTION_BACKUP_RESTORE = 4     ' open for backup or restore
    3. Const REG_OPTION_VOLATILE = 1           ' Key is not preserved when system is rebooted
    4. Const REG_OPTION_NON_VOLATILE = 0       ' Key is preserved when system is rebooted
    5. Const STANDARD_RIGHTS_ALL = &H1F0000
    6. Const SYNCHRONIZE = &H100000
    7. Const READ_CONTROL = &H20000
    8. Const STANDARD_RIGHTS_READ = (READ_CONTROL)
    9. Const STANDARD_RIGHTS_WRITE = (READ_CONTROL)
    10. Const KEY_CREATE_LINK = &H20
    11. Const KEY_CREATE_SUB_KEY = &H4
    12. Const KEY_ENUMERATE_SUB_KEYS = &H8
    13. Const KEY_NOTIFY = &H10
    14. Const KEY_QUERY_VALUE = &H1
    15. Const KEY_SET_VALUE = &H2
    16. Const KEY_READ = ((STANDARD_RIGHTS_READ Or KEY_QUERY_VALUE Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY) And (Not SYNCHRONIZE))
    17. Const KEY_WRITE = ((STANDARD_RIGHTS_WRITE Or KEY_SET_VALUE Or KEY_CREATE_SUB_KEY) And (Not SYNCHRONIZE))
    18. Const KEY_EXECUTE = (KEY_READ)
    19. 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))
    20. Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
    21. Private Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal hKey As Long, ByVal lpSubKey As String) As Long
    22. 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
    23. 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
    24. Private Sub Form_Load()
    25.     'KPD-Team 2000
    26.     'URL: [url]http://www.allapi.net/[/url]
    27.     'E-Mail: [email][email protected][/email]
    28.     Dim Result As Long
    29.     'Check if the specified key exists
    30.     RegOpenKeyEx HKEY_CURRENT_USER, "\Software\Game Maker\Version 5\Preferences\", 0, KEY_ALL_ACCESS, Result
    31.    
    32.     'Delete the key
    33.     RegDeleteKey Result, "Recent0"
    34.     'close the handle
    35.     RegCloseKey Result
    36. End Sub

    EDIT: after realizing Recent0 was the value, I changed the code to reflect that.
    Last edited by BuggyProgrammer; Jan 24th, 2004 at 07:26 PM.
    Remember, if someone's post was not helpful, you can always rate their post negatively .

  14. #14
    The picture isn't missing BuggyProgrammer's Avatar
    Join Date
    Oct 2000
    Location
    Vancouver, Canada
    Posts
    5,217
    maybe, in your original post:
    VB Code:
    1. Private Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal lpValueName As String) As Long
    2.  
    3. Private SUB DeleteValue(ByVal hKey As Long, ByVal strPath As String, ByVal strValue As String)
    4. 'DeleteValue should be a SUB
    5. 'hKEY is a one of the HKEY_ constants that you can from the apiviewer
    6. 'strPath is the path, minus the HKEY, minus the value name
    7. 'strValue is the value name.  
    8. 'you melded all three together into strpath
    9.     Dim keyhand As Long
    10.     Dim r As Long
    11.     r = RegOpenKey(hKey, strPath, keyhand)
    12.     r = RegDeleteValue(keyhand, strValue)
    13.     r = RegCloseKey(keyhand)
    14. End Function
    15.  
    16. DeleteValue &H80000001, "\Software\Game Maker\Version 5\Preferences", "Recent0"
    Remember, if someone's post was not helpful, you can always rate their post negatively .

  15. #15
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171
    Try this module
    Attached Files Attached Files


    Has someone helped you? Then you can Rate their helpful post.

  16. #16
    New Member
    Join Date
    Jan 2004
    Posts
    3
    Heres what worx for me not sure whats needed


    Const HKEY_CURRENT_USER = &H80000001
    Const REG_OPTION_BACKUP_RESTORE = 4
    Const REG_OPTION_VOLATILE = 1
    Const REG_OPTION_NON_VOLATILE = 0
    Const STANDARD_RIGHTS_ALL = &H1F0000
    Const SYNCHRONIZE = &H100000
    Const READ_CONTROL = &H20000
    Const STANDARD_RIGHTS_READ = (READ_CONTROL)
    Const STANDARD_RIGHTS_WRITE = (READ_CONTROL)
    Const KEY_CREATE_LINK = &H20
    Const KEY_CREATE_SUB_KEY = &H4
    Const KEY_ENUMERATE_SUB_KEYS = &H8
    Const KEY_NOTIFY = &H10
    Const KEY_QUERY_VALUE = &H1
    Const KEY_SET_VALUE = &H2
    Const KEY_READ = ((STANDARD_RIGHTS_READ Or KEY_QUERY_VALUE Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY) And (Not SYNCHRONIZE))
    Const KEY_WRITE = ((STANDARD_RIGHTS_WRITE Or KEY_SET_VALUE Or KEY_CREATE_SUB_KEY) And (Not SYNCHRONIZE))
    Const KEY_EXECUTE = (KEY_READ)
    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))
    Const KEY_ALL_ACCESS As Long = &H3F
    Const REG_OPTION_NON_VOLATILE = 0

    Public Declare Function RegCloseKey Lib "advapi32.dll" Alias "RegCloseKey" (ByVal hKey As Long) As Long
    Public Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) As Long
    Public Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal lpValueName As String) As Long


    Now call it

    RegOpenKeyEx HKEY_LOCAL_MACHINE, "The Key Path", 0, KEY_ALL_ACCESS, Result
    RegDeleteValue Result, "Your Value"
    RegCloseKey Result

  17. #17

    Thread Starter
    Supreme User Madboy's Avatar
    Join Date
    Oct 2003
    Location
    England
    Posts
    3,253
    Thanks, i must need to close the key after though, as when i look in the registry the key still exists?

  18. #18

    Thread Starter
    Supreme User Madboy's Avatar
    Join Date
    Oct 2003
    Location
    England
    Posts
    3,253
    None of these work, any chance of a working example. I always get errors like:

    "Variable not defined"
    "Argument not optional"

    VB Code:
    1. Const KEY_ALL_ACCESS = 'This errors!

    I only need to delete a single value

    Thanks

  19. #19
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141
    Try the attached module with this syntax.
    VB Code:
    1. DeleteValue HKEY_CURRENT_USER, "Software\Game Maker\Version 5\Preferences", "Recent0"
    Attached Files Attached Files

  20. #20

    Thread Starter
    Supreme User Madboy's Avatar
    Join Date
    Oct 2003
    Location
    England
    Posts
    3,253
    Originally posted by MarkT
    Try the attached module with this syntax.
    VB Code:
    1. DeleteValue HKEY_CURRENT_USER, "Software\Game Maker\Version 5\Preferences", "Recent0"
    That works a treat thanks mate

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