|
-
Jul 22nd, 2007, 08:00 PM
#1
Thread Starter
Addicted Member
[RESOLVED] delete registry key and all subkeys
How would I delete a registry key and all of the subkeys inside?
-
Jul 22nd, 2007, 08:10 PM
#2
Addicted Member
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"
-
Jul 22nd, 2007, 09:01 PM
#3
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:
DeleteKey "Software\MyCompany\MyApp"
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.
-
Jul 23rd, 2007, 03:31 AM
#4
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.
-
Jul 23rd, 2007, 04:02 AM
#5
Re: [RESOLVED] delete registry key and all subkeys
Don't you have to send it a handle?
-
Jul 23rd, 2007, 04:08 AM
#6
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.
-
Jul 23rd, 2007, 06:04 PM
#7
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.
-
Jul 24th, 2007, 10:30 AM
#8
Lively Member
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
-
Jul 24th, 2007, 10:42 AM
#9
Re: [RESOLVED] delete registry key and all subkeys
 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.
-
Jul 24th, 2007, 10:51 AM
#10
Lively Member
Re: [RESOLVED] delete registry key and all subkeys
class1 is where i put your code. oRegistry is another class module
-
Jul 24th, 2007, 10:59 AM
#11
Lively Member
Re: [RESOLVED] delete registry key and all subkeys
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|