Results 1 to 11 of 11

Thread: [RESOLVED] delete registry key and all subkeys

Threaded View

  1. #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.

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