RegDeleteKey error

P

Philip Stapleton

I'm trying to delete a key from the VB settings area with

Sub main()
Dim key As Long
If RegOpenKeyEx(HKEY_CURRENT_USER, "Software\VB and VBA Program
Settings\", 0, KEY_ALL_ACCESS, key) = 0 Then
MsgBox RegDeleteKey(key, "RadarTutor")
End If
End Sub

The Open works OK but the Delete returns an error code of 5 - however,
Regedit shows a Key called HKEY_CURRENT_USER\Software\VB and VBA Program
Settings\RadarTutor

What am I doing wrong?
 
T

Torgeir Bakken \(MVP\)

Philip said:
I'm trying to delete a key from the VB settings area with

Sub main()
Dim key As Long
If RegOpenKeyEx(HKEY_CURRENT_USER, "Software\VB and VBA Program
Settings\", 0, KEY_ALL_ACCESS, key) = 0 Then
MsgBox RegDeleteKey(key, "RadarTutor")
End If
End Sub

The Open works OK but the Delete returns an error code of 5 - however,
Regedit shows a Key called HKEY_CURRENT_USER\Software\VB and VBA Program
Settings\RadarTutor

What am I doing wrong?
Hi

I don't know about VB, but for VBScript/WMI you can't delete a key
that has subkeys, you need to create a recursive sub that starts the
deleting at the end of the registry branch, like this:


strComputer = "." ' use "." for local computer

Const HKLM = &H80000002 'HKEY_LOCAL_MACHINE

Set oReg = GetObject _
("winmgmts:{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\default:StdRegProv")

DeleteRegistryKey HKLM, "SOFTWARE\SomeKey"


Sub DeleteRegistryKey(ByVal sHive, ByVal sKey)
Dim aSubKeys, sSubKey, iRC
On Error Resume Next
iRC = oReg.EnumKey(sHive, sKey, aSubKeys)
If iRC = 0 And IsArray(aSubKeys) Then
For Each sSubKey In aSubKeys
If Err.Number <> 0 Then
Err.Clear
Exit Sub
End If
DeleteRegistryKey sHive, sKey & "\" & sSubKey
Next
End If
oReg.DeleteKey sHive, sKey
End Sub
 
T

Torgeir Bakken \(MVP\)

Philip said:
I'm trying to delete a key from the VB settings area with

Sub main()
Dim key As Long
If RegOpenKeyEx(HKEY_CURRENT_USER, "Software\VB and VBA Program
Settings\", 0, KEY_ALL_ACCESS, key) = 0 Then
MsgBox RegDeleteKey(key, "RadarTutor")
End If
End Sub

The Open works OK but the Delete returns an error code of 5 - however,
Regedit shows a Key called HKEY_CURRENT_USER\Software\VB and VBA Program
Settings\RadarTutor

What am I doing wrong?
Hi

I don't know about VB, but for VBScript/WMI you can't delete a key
that has subkeys, you need to create a recursive sub that starts the
deleting at the end of the registry branch, like this:


strComputer = "." ' use "." for local computer

Const HKLM = &H80000002 'HKEY_LOCAL_MACHINE

Set oReg = GetObject _
("winmgmts:{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\default:StdRegProv")

DeleteRegistryKey HKLM, "SOFTWARE\SomeKey"


Sub DeleteRegistryKey(ByVal sHive, ByVal sKey)
Dim aSubKeys, sSubKey, iRC
On Error Resume Next
iRC = oReg.EnumKey(sHive, sKey, aSubKeys)
If iRC = 0 And IsArray(aSubKeys) Then
For Each sSubKey In aSubKeys
If Err.Number <> 0 Then
Err.Clear
Exit Sub
End If
DeleteRegistryKey sHive, sKey & "\" & sSubKey
Next
End If
oReg.DeleteKey sHive, sKey
End Sub
 
G

guard

Hi

I don't know about VB, but for VBScript/WMI you can't delete a key
that has subkeys, you need to create a recursive sub that starts the
deleting at the end of the registry branch, like this:


strComputer = "." ' use "." for local computer

Const HKLM = &H80000002 'HKEY_LOCAL_MACHINE

Set oReg = GetObject _
("winmgmts:{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\default:StdRegProv")

DeleteRegistryKey HKLM, "SOFTWARE\SomeKey"


Sub DeleteRegistryKey(ByVal sHive, ByVal sKey)
Dim aSubKeys, sSubKey, iRC
On Error Resume Next
iRC = oReg.EnumKey(sHive, sKey, aSubKeys)
If iRC = 0 And IsArray(aSubKeys) Then
For Each sSubKey In aSubKeys
If Err.Number <> 0 Then
Err.Clear
Exit Sub
End If
DeleteRegistryKey sHive, sKey & "\" & sSubKey
Next
End If
oReg.DeleteKey sHive, sKey
End Sub

The Mount/\Command ".RegDeleteKey" will completely delete a registry key
including all subkeys from the command line (USE WITH CAUTION!).

SET "DeleteKey=HKEY_CURRENT_USER\Software\VB and VBA Program
Settings\RadarTutor"
%.RegDeleteKey%

*******

..RegDeleteKey is part of the Expert NT/2K/XP/K3 Command Library and performs
CONSISTENTLY across all four platforms.
(http://NTCmdLib.com)

Related commands:

.RegRead
.RegReadKey
.RegWrite
.RegWriteKey

*******

-tsg

/-----------------+---------------+----------------------\
| COMPATIBILITY | CLARITY | SPEED |
| Write code ONCE | Make it clear | THEN...Make it fast! |
\-----------------+---------------+----------------------/
400+ command-line resources using ONLY native NT commands!
(http://TheSystemGuard.com/default.asp#MasterCommandList)
 
G

guard

Hi

I don't know about VB, but for VBScript/WMI you can't delete a key
that has subkeys, you need to create a recursive sub that starts the
deleting at the end of the registry branch, like this:


strComputer = "." ' use "." for local computer

Const HKLM = &H80000002 'HKEY_LOCAL_MACHINE

Set oReg = GetObject _
("winmgmts:{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\default:StdRegProv")

DeleteRegistryKey HKLM, "SOFTWARE\SomeKey"


Sub DeleteRegistryKey(ByVal sHive, ByVal sKey)
Dim aSubKeys, sSubKey, iRC
On Error Resume Next
iRC = oReg.EnumKey(sHive, sKey, aSubKeys)
If iRC = 0 And IsArray(aSubKeys) Then
For Each sSubKey In aSubKeys
If Err.Number <> 0 Then
Err.Clear
Exit Sub
End If
DeleteRegistryKey sHive, sKey & "\" & sSubKey
Next
End If
oReg.DeleteKey sHive, sKey
End Sub

The Mount/\Command ".RegDeleteKey" will completely delete a registry key
including all subkeys from the command line (USE WITH CAUTION!).

SET "DeleteKey=HKEY_CURRENT_USER\Software\VB and VBA Program
Settings\RadarTutor"
%.RegDeleteKey%

*******

..RegDeleteKey is part of the Expert NT/2K/XP/K3 Command Library and performs
CONSISTENTLY across all four platforms.
(http://NTCmdLib.com)

Related commands:

.RegRead
.RegReadKey
.RegWrite
.RegWriteKey

*******

-tsg

/-----------------+---------------+----------------------\
| COMPATIBILITY | CLARITY | SPEED |
| Write code ONCE | Make it clear | THEN...Make it fast! |
\-----------------+---------------+----------------------/
400+ command-line resources using ONLY native NT commands!
(http://TheSystemGuard.com/default.asp#MasterCommandList)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top