[RESOLVED] Problem with Deleting a registry key
For some reason the code I have I cannot seem to delete the registry key.
I'm using the basRegistry module, that I grabbed from...Kenneth Ives
I'm calling this piece of code in particular:
Code:
Public Function regDelete_Sub_Key(ByVal lngRootKey As Long, _
ByVal strRegKeyPath As String, _
ByVal strRegSubKey As String)
' --------------------------------------------------------------
' Written by Kenneth Ives [email protected]
'
' Important: If you treat all key data strings as being
' case sensitive, you should never have a problem.
' Always backup your registry files (System.dat
' and User.dat) before performing any type of
' modifications
'
' Description: Function for removing a sub key.
'
' Parameters:
' lngRootKey - HKEY_CLASSES_ROOT, HKEY_CURRENT_USER,
' HKEY_lOCAL_MACHINE, HKEY_USERS, etc
' strRegKeyPath - is name of the key path you wish to traverse.
' strRegSubKey - is the name of the key which will be removed.
'
' Syntax:
' regDelete_Sub_Key HKEY_CURRENT_USER, _
"Software\AAA-Registry Test\Products", "StringTestData"
'
' Removes the sub key "StringTestData"
' --------------------------------------------------------------
' --------------------------------------------------------------
' Define variables
' --------------------------------------------------------------
Dim lngKeyHandle As Long
' --------------------------------------------------------------
' Make sure the key exist before trying to delete it
' --------------------------------------------------------------
If regDoes_Key_Exist(lngRootKey, strRegKeyPath) Then
' Get the key handle
m_lngRetVal = RegOpenKey(lngRootKey, strRegKeyPath, lngKeyHandle)
' Delete the sub key. If it does not exist, then ignore it.
m_lngRetVal = RegDeleteValue(lngKeyHandle, strRegSubKey)
' Always close the handle in the registry. We do not want to
' corrupt the registry.
m_lngRetVal = RegCloseKey(lngKeyHandle)
End If
End Function
Here is my code calling it: (SelectedItem is equal to: HID\VID_0925&PID_8866&COL01\7&21F7DE89&0&0000 for an example)
Code:
Private Sub DelSelectedDev_Click()
Dim n As Integer
Dim pos1 As Integer
Dim pos2 As Integer
Dim lngRootKey As Long
Dim tempKeyPath1 As String
Dim tempKeyPath2 As String
Dim tempSubKeyName1 As String
Dim tempSubKeyName2 As String
pos1 = InStr(selectedItem, "\")
pos2 = InStr(pos1 + 1, selectedItem, "\")
lngRootKey = HKEY_LOCAL_MACHINE
MsgBox pos2
On Error GoTo errorhandler
If pos2 = 0 Then
MsgBox "Invalid Key Name, unable to delete!"
End
Else
tempKeyPath1 = "SYSTEM\CurrentControlSet\Enum\" & selectedItem
tempKeyPath2 = Left(selectedItem, pos2)
tempSubKeyName1 = "Device Parameters"
tempSubKeyName2 = "Log Conf"
tempSubKeyName3 = Right(tempKeyPath1, (Len(selectedItem) - pos2))
MsgBox tempKeyPath1
MsgBox tempKeyPath2
MsgBox tempSubKeyName1
MsgBox tempSubKeyName2
MsgBox tempSubKeyName3
regDelete_Sub_Key lngRootKey, tempKeyPath1, tempSubKeyName1
regDelete_Sub_Key lngRootKey, tempKeyPath1, tempSubKeyName2
regDelete_Sub_Key lngRootKey, tempKeyPath2, tempSubKeyName3
End If
errorhandler:
MsgBox Err.Number & ": " & Err.Description
It doesn't delete the key and no error is returned. Any ideas?
Re: Problem with Deleting a registry key
Do you get down to the point where all of your message boxs are displayed?
Re: Problem with Deleting a registry key
Yep...I think I'm running into a permissions problem but I'm not sure. The varibles are the values i expect them to be,
Re: Problem with Deleting a registry key
Quote:
Originally Posted by phimuskapsi
Yep...I think I'm running into a permissions problem
Actually, if the answer to the first was yes, this was going to be my second question.
Are you an administrator of the machine?
It is possible the registry is "locked down" to all but admins.
Re: Problem with Deleting a registry key
In order to delete the subkeys of a key do I have to remove the values in teh keys first?
Re: Problem with Deleting a registry key
In order to delete the parent key, you would need to address the subkeys, but the subkey themselves should not present a problem.
What about my last question? Is the registry locked to editing?
Re: Problem with Deleting a registry key
Quote:
Originally Posted by phimuskapsi
It doesn't delete the key and no error is returned. Any ideas?
It doesn't return an error because m_lngRetVal is never evaluated.
As Hack's pointed out, if you aren't an admin, you can't access HKLM. So, again, are you an admin?.
I'm also a bit puzzled here. Are you trying to delete a Subkey or a value ? It may just be the terminology in the comments, but the API to delete a Subkey is RegDeleteKey, not RegDeleteValue as used in the above code. (RegDeleteKey will not delete a subkey if it contains a subkey, but it will delete a subkey that contains only values)
If you want to delete a subkey that contains other subkeys, It's easier to call the SHDeleteKey API. (It's a 1 liner).
Re: Problem with Deleting a registry key
I don't think so I was testing it on my machine at home so I have admin rights. I just got the ActiveX RegObj.Dll and I'll check and see if it's any better.
Re: Problem with Deleting a registry key
Lol I was just looking at the bas code and you are right the "sub key" being referred to is the sub keys values.
I want a way to remove the Key and any sub keys.
Re: Problem with Deleting a registry key
Welp. The RegObj.Dll recursively removes subkeys however. I cannot write to the HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB Key at all even in regedit...any idea on how to do it?
Re: Problem with Deleting a registry key
I had to get old school on it and crack open the resource kit to get regini.exe to "unlock" the specific keys I needed to delete, here's the modified code for anyone that is curious (I included these files in the zip: regini.exe, devcon.exe, regobj.dll):
vb Code:
[CODE]
'selectedItem = "USB\VID_046D&PID_C00C\6&21E5B2C2&0&3"
Private Sub DelSelectedDev_Click()
Dim n As Integer
Dim x As Integer
Dim pos1 As Integer
Dim pos2 As Integer
Dim ret3 As Integer
Dim lngRootKey As Long
Dim BatText As String
Dim regScript1 As String
Dim regScript2 As String
Dim regScript3 As String
Dim tempKeyPath1 As String
Dim tempKeyPath2 As String
Dim tempKeyPath3 As String
Dim objSubKey As RegKey
Dim objRegRootKey As RegKey
Dim objRegKey As RegKey
x = Len(selectedItem)
pos1 = InStr(selectedItem, "\")
pos2 = InStr(pos1 + 1, selectedItem, "\")
BatText = "C:\RID\bin\regini.exe -m \\" & MachineBox.Text & " C:\RID\bin\RegIniScript.txt"
If pos2 = 0 Then
MsgBox "Invalid Key Name, unable to delete!"
End
Else
tempKeyPath1 = "\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\"
tempKeyPath2 = tempKeyPath1 & Left(selectedItem, pos2)
tempKeyPath3 = tempKeyPath2 & Trim(Right(selectedItem, (x - pos2)))
regScript1 = "\Registry\Machine" & Right(tempKeyPath1, 31) & Left(selectedItem, pos2) & Trim(Right(selectedItem, (x - pos2))) & "\Device Parameters [1 5 7 11 14 17]"
regScript2 = "\Registry\Machine" & Right(tempKeyPath1, 31) & Left(selectedItem, pos2) & Trim(Right(selectedItem, (x - pos2))) & "\LogConf [1 5 7 11 14 17]"
regScript3 = "\Registry\Machine" & Right(tempKeyPath1, 31) & Left(selectedItem, pos2) & Trim(Right(selectedItem, (x - pos2))) & " [1 5 7 11 14 17]"
Open "C:\RID\bin\reginiscript.txt" For Output As #1
Print #1, regScript1
Print #1, regScript2
Print #1, regScript3
Close #1
Open "C:\RID\bin\Regini.bat" For Output As #2
Print #2, BatText
Close #2
ret3 = Shell("C:\RID\bin\Regini.bat", vbHide)
'Get the last key from the path'
Set objRegKey = RegKeyFromString(tempKeyPath3)
'Find the parent key of the selected key
Set objRegRootKey = objRegKey.Parent
'Remove the selected key from the Root Key subkeys
objRegRootKey.SubKeys.Remove (objRegKey)
'For Each objSubKey In objRegRootKey.SubKeys
'MsgBox objSubKey.Name
'Next objSubKey
End If
End Sub
[/CODE]
Works great!
Re: Problem with Deleting a registry key
Unfortunately, you might find the moderators will remove your zip. Compiled binary files are not generally allowed since they can be other than what they appear ;). And if they are legit, there may be redistribution rules that are being violated (not saying this is the case).
Re: Problem with Deleting a registry key
Removed it but I have it by request. Also I recommend using shellandwait for the shell command now, shell sometimes does not finish fast enough. Go here for the one I made.
http://vbforums.com/showthread.php?t...t=shellandwait
Re: Problem with Deleting a registry key
Strange that you had to resort to Regini to change the permissions, but it's not unknown for even commercial software to misbehave. If you ever have to do this manually with regedit, you can start it with "system" priveleges via Task Scheduler.
Re: Problem with Deleting a registry key
If you know a way to do it with the API I'd be interested ;-) I hate resorting to running some sort of batch file, but if it gets the job done...then well I won't argue.
Re: Problem with Deleting a registry key
Quote:
Originally Posted by LaVolpe
Unfortunately, you might find the moderators will remove your zip.
Actually, we wouldn't remove the attachment unless the only thing in it was an executable.
More often than not, member's post both the source and the executable in an attached zip file. I/we simply edit the attachment and remove the executable, leaving the source.
Re: Problem with Deleting a registry key
Quote:
Originally Posted by phimuskapsi
If you know a way to do it with the API I'd be interested ;-) I hate resorting to running some sort of batch file, but if it gets the job done...then well I won't argue.
I'm not sure about the permissions issue, because if you're logged in as admin you should be able to use the APIs to delete almost anything (apart from those that MS has deemed a no-no, and it's wise to leave well alone), unless some application changed the permissions. Starting a program as "system" should give you absolute control. Search for "Sneaking a Peek with RegEdt32" in the link.
As I mentioned earlier, I normally use the SHDeleteKey API. You'd have to test it to see if it works under the circumstances you mentioned, even running it as "system". There's only one way to find out :)
vb Code:
'Form level code.
Private Sub Form_Click()
MsgBox ShellDeleteSubKey(HKEY_LOCAL_MACHINE, "Software\SomeSubkey\AndAnotherOne")
End Sub
vb Code:
'Module level code.
Public Declare Function SHDeleteKey Lib "shlwapi.dll" Alias "SHDeleteKeyA" _
(ByVal Hkey As Long, ByVal pszSubKey As String) As Long
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
EDIT: Rather than create a batch file, you could use the ShellExecute API, which accepts command line parameters.
Re: [RESOLVED] Problem with Deleting a registry key
Oh yeah I forgot about Shell Execute.
The reason I created this program to begin with is that we have a LOT of non-present devices in the device manager on our Ghost Images for work. So we needed to go in and remove them.
Unfortunately anything under HKLM\System\CurrentControlSet\Enum\USB is "locked" (probably with Null values) so you have to unlock them first in order to delete them (even if you are logged in as admin). But I'll integrate ShellExecute and save myself some code and hassle.
Re: [RESOLVED] Problem with Deleting a registry key
Can't get ShellExecute working with a shell and wait function that I made. Any ideas?
Re: [RESOLVED] Problem with Deleting a registry key
Quote:
Originally Posted by phimuskapsi
Can't get ShellExecute working with a shell and wait function that I made. Any ideas?
You might want to post your shellandwait commands along with the declaration you are using.
Re: [RESOLVED] Problem with Deleting a registry key