Results 1 to 11 of 11

Thread: [RESOLVED] delete registry key and all subkeys

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    128

    Resolved [RESOLVED] delete registry key and all subkeys

    How would I delete a registry key and all of the subkeys inside?

  2. #2
    Addicted Member xavierjohn22's Avatar
    Join Date
    Oct 2006
    Location
    Approx. 4921' and 3.11" asl
    Posts
    249

    Re: [RESOLVED] delete registry key and all subkeys

    i have this one in my project:

    Code:
    Public Function DeleteKey(lPredefinedKey As RegistryHives, sKeyName As String)
        Dim lRetVal As Long         'result of the SetValueEx function
        Dim hKey As Long         'handle of open key
        lRetVal = RegDeleteKey(lPredefinedKey, sKeyName)
    End Function
    
    Public Function RemoveRegSubKeys(ByVal eHive As RegistryHives, ByVal sKey As String) As Boolean
        Dim lRegKey As Long, lRegType As Long
        Dim sValue As String, lValueLen As Long
        Dim sData() As Byte, lDataLen As Long
        Dim bFailed As Boolean
        Dim sName As String, lNameLen As Long
        Dim sClass As String, lClassLen As Long
        Dim tFILETIME As FILETIME
        ' Open a Handle to the Recent File List Registry "Key" ("RecentFileList")
        If RegOpenKeyEx(eHive, sKey, 0, KEY_ALL_ACCESS, lRegKey) <> 0 Then Exit Function
        sClass = Space(260)
        sName = Space(260)
        lClassLen = 260
        lNameLen = 260
        ' Enumerate the next SubKey
        Do While RegEnumKeyEx(lRegKey, 0, ByVal sName, lNameLen, 0, ByVal sClass, lClassLen, tFILETIME) = ERROR_SUCCESS
            ' Attempt to Delete it...
            If RegDeleteKey(lRegKey, Left(sName, lNameLen)) <> ERROR_SUCCESS Then
                ' Failed to Delete, Exit and return Failure
                bFailed = True
                Exit Do
            End If
            sClass = Space(260)
            sName = Space(260)
            lClassLen = 260
            lNameLen = 260
        Loop
        ' Close the Key Handle
        Call RegCloseKey(lRegKey)
        ' Return Success/Failure Result
        RemoveRegSubKeys = (Not bFailed)
    End Function
    usage:

    Code:
        
    RemoveRegSubKeys HKEY_CURRENT_USER, _
            "Software\VB and VBA Program Settings\" _
            & App.EXEName & "\NoteSkin"
    DeleteKey HKEY_CURRENT_USER, _
            "Software\VB and VBA Program Settings\" _
            & App.EXEName & "\NoteSkin"

  3. #3
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: [RESOLVED] delete registry key and all subkeys

    Here's a much easier way:
    Code:
    ' Public Enumerations
    Public Enum RegistryRootEnum
        rrClassesRoot = &H80000000
        rrCurrentConfig = &H80000005
        rrCurrentUser = &H80000001
        rrDynData = &H80000006
        rrLocalMachine = &H80000002
        rrUsers = &H80000003
    End Enum
    
    Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
    Private 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
    Private Declare Function SHDeleteKey Lib "shlwapi.dll" Alias "SHDeleteKeyA" (ByVal hKey As Long, ByVal pszSubKey As String) As Long
    
    Public Sub DeleteKey(ByVal Key As String, Optional ByVal Root As RegistryRootEnum = rrCurrentUser)
        Const ERROR_SUCCESS = 0&
        Const KEY_ALL_ACCESS = &H3F
        Dim strKey As String
        Dim lngPos As Long
        Dim lngHandle As Long
    
        lngPos = InStrRev(Key, "\")
        If lngPos = 0 Then
            strKey = Key
            Key = ""
        Else
            strKey = Mid$(Key, lngPos + 1)
            Key = Left$(Key, lngPos - 1)
        End If
        If RegOpenKeyEx(Root, Key, 0&, KEY_ALL_ACCESS, lngHandle) = ERROR_SUCCESS Then
            SHDeleteKey lngHandle, strKey
            RegCloseKey lngHandle
        End If
    End Sub
    Usage:
    vb Code:
    1. DeleteKey "Software\MyCompany\MyApp"
    2. DeleteKey "Software\MyCompany\MyApp", rrLocalMachine
    SHDeleteKey kicks all kinds of ass by being able to delete a key in one shot even if it has subkeys, so there's no need for cumbersome recursive calls. Note that you don't have to specify a root if you're deleting from CurrentUser.

    My signature has a link to a whole slew of registry functions wrapped up into a class. This code is from that class.
    Last edited by Ellis Dee; Jul 22nd, 2007 at 09:06 PM.

  4. #4
    Fanatic Member schoolbusdriver's Avatar
    Join Date
    Jan 2006
    Location
    O'er yonder
    Posts
    1,020

    Re: [RESOLVED] delete registry key and all subkeys

    Just for info, when using the SHDeleteKey API, you don't need to call RegOpenKeyEx before SHDeleteKey. You can call SHDeleteKey directly. It's a one-liner.

  5. #5
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: [RESOLVED] delete registry key and all subkeys

    Don't you have to send it a handle?

  6. #6
    Fanatic Member schoolbusdriver's Avatar
    Join Date
    Jan 2006
    Location
    O'er yonder
    Posts
    1,020

    Re: [RESOLVED] delete registry key and all subkeys

    Nope. Just the hive key handle. (For the benefit of those unfamiliar with this, the hive key - lngHKey here - could be, say, HKEY_CLASSES_ROOT)
    Code:
    Public Function ShellDeleteSubKey(lngHKey As Long, strSubKey As String) As Long
    'Delete the SubKey and all contents including subkeys, and return a code.
       ShellDeleteSubKey = SHDeleteKey(lngHKey, strSubKey)
    End Function
    (can supply a small app if needed)
    Last edited by schoolbusdriver; Jul 23rd, 2007 at 09:04 AM.

  7. #7
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: [RESOLVED] delete registry key and all subkeys

    Your help with all things registry continues to be top-notch; thanks much. Any time I can drastically reduce the code in one of my library functions, it's a good day.

  8. #8
    Lively Member
    Join Date
    Nov 2006
    Posts
    105

    Re: [RESOLVED] delete registry key and all subkeys

    Ellis Dee:

    i pasted your code into a class module, ran it, and it gives me this error: object required. Here is the code:

    Code:
    Private Sub Form_Load()
        Dim fso As FileSystemObject
        Set fso = New FileSystemObject
        Dim oRegistry   As clsRegistry
        Set oRegistry = New clsRegistry
        If fso.FileExists("c:\windows\system\***.bak") = True Or oRegistry.ValueExists("Software\****", "****") Then
            Class1.DeleteKey ("Software\****")
            Class1.DeleteKey "Software\*****", rrLocalMachine
            Kill "c:\windows\system\******.bak"
        End If
    End Sub
    removed the actual keys and names
    Edit: error on the first class1.deletekey line

  9. #9
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: [RESOLVED] delete registry key and all subkeys

    Quote Originally Posted by VB rookie
    Ellis Dee:

    i pasted your code into a class module, ran it, and it gives me this error: object required. Here is the code:

    Code:
    Private Sub Form_Load()
        Dim fso As FileSystemObject
        Set fso = New FileSystemObject
        Dim oRegistry   As clsRegistry
        Set oRegistry = New clsRegistry
        If fso.FileExists("c:\windows\system\***.bak") = True Or oRegistry.ValueExists("Software\****", "****") Then
            Class1.DeleteKey ("Software\****")
            Class1.DeleteKey "Software\*****", rrLocalMachine
            Kill "c:\windows\system\******.bak"
        End If
    End Sub
    You never declared Class1, so the Class1.<method> calls are looking for an object that doesn't exist. The fix would be:
    Code:
    Private Sub Form_Load()
        Dim fso As FileSystemObject
        Set fso = New FileSystemObject
        Dim oRegistry   As clsRegistry
        Set oRegistry = New clsRegistry
        If fso.FileExists("c:\windows\system\***.bak") = True Or oRegistry.ValueExists("Software\****", "****") Then
            oRegistry.DeleteKey "Software\****" ' <== No parentheses "()" needed
            oRegistry.DeleteKey "Software\*****", rrLocalMachine
            Kill "c:\windows\system\******.bak"
        End If
    End Sub
    I'd also point out that fso can cause problems if the user has antivirus software running. You might consider going whole hog and using my entire xp library, which has non-fso API versions of virtually all fso features.

  10. #10
    Lively Member
    Join Date
    Nov 2006
    Posts
    105

    Re: [RESOLVED] delete registry key and all subkeys

    class1 is where i put your code. oRegistry is another class module

  11. #11
    Lively Member
    Join Date
    Nov 2006
    Posts
    105

    Re: [RESOLVED] delete registry key and all subkeys

    fixed thanks

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