Results 1 to 21 of 21

Thread: [RESOLVED] Problem with Deleting a registry key

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2007
    Posts
    29

    Resolved [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?

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Problem with Deleting a registry key

    Do you get down to the point where all of your message boxs are displayed?

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Apr 2007
    Posts
    29

    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,

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    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.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Apr 2007
    Posts
    29

    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?

  6. #6
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    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?

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

    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).

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Apr 2007
    Posts
    29

    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.

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Apr 2007
    Posts
    29

    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.

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Apr 2007
    Posts
    29

    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?

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Apr 2007
    Posts
    29

    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:
    1. [CODE]
    2. 'selectedItem = "USB\VID_046D&PID_C00C\6&21E5B2C2&0&3"
    3.  
    4. Private Sub DelSelectedDev_Click()
    5.     Dim n As Integer
    6.     Dim x As Integer
    7.     Dim pos1 As Integer
    8.     Dim pos2 As Integer
    9.     Dim ret3 As Integer
    10.     Dim lngRootKey As Long
    11.     Dim BatText As String
    12.     Dim regScript1 As String
    13.     Dim regScript2 As String
    14.     Dim regScript3 As String
    15.     Dim tempKeyPath1 As String
    16.     Dim tempKeyPath2 As String
    17.     Dim tempKeyPath3 As String
    18.     Dim objSubKey As RegKey
    19.     Dim objRegRootKey As RegKey
    20.     Dim objRegKey As RegKey
    21.    
    22.     x = Len(selectedItem)
    23.     pos1 = InStr(selectedItem, "\")
    24.     pos2 = InStr(pos1 + 1, selectedItem, "\")
    25.    
    26.     BatText = "C:\RID\bin\regini.exe -m \\" & MachineBox.Text & " C:\RID\bin\RegIniScript.txt"
    27.    
    28.     If pos2 = 0 Then
    29.         MsgBox "Invalid Key Name, unable to delete!"
    30.         End
    31.     Else
    32.         tempKeyPath1 = "\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\"
    33.         tempKeyPath2 = tempKeyPath1 & Left(selectedItem, pos2)
    34.         tempKeyPath3 = tempKeyPath2 & Trim(Right(selectedItem, (x - pos2)))
    35.         regScript1 = "\Registry\Machine" & Right(tempKeyPath1, 31) & Left(selectedItem, pos2) & Trim(Right(selectedItem, (x - pos2))) & "\Device Parameters [1 5 7 11 14 17]"
    36.         regScript2 = "\Registry\Machine" & Right(tempKeyPath1, 31) & Left(selectedItem, pos2) & Trim(Right(selectedItem, (x - pos2))) & "\LogConf [1 5 7 11 14 17]"
    37.         regScript3 = "\Registry\Machine" & Right(tempKeyPath1, 31) & Left(selectedItem, pos2) & Trim(Right(selectedItem, (x - pos2))) & " [1 5 7 11 14 17]"
    38.         Open "C:\RID\bin\reginiscript.txt" For Output As #1
    39.             Print #1, regScript1
    40.             Print #1, regScript2
    41.             Print #1, regScript3
    42.         Close #1
    43.         Open "C:\RID\bin\Regini.bat" For Output As #2
    44.             Print #2, BatText
    45.         Close #2
    46.         ret3 = Shell("C:\RID\bin\Regini.bat", vbHide)
    47.        
    48.         'Get the last key from the path'
    49.         Set objRegKey = RegKeyFromString(tempKeyPath3)
    50.         'Find the parent key of the selected key
    51.         Set objRegRootKey = objRegKey.Parent
    52.         'Remove the selected key from the Root Key subkeys
    53.         objRegRootKey.SubKeys.Remove (objRegKey)
    54.         'For Each objSubKey In objRegRootKey.SubKeys
    55.             'MsgBox objSubKey.Name
    56.         'Next objSubKey
    57.        
    58.     End If
    59. End Sub
    60. [/CODE]

    Works great!
    Last edited by phimuskapsi; Jan 3rd, 2008 at 12:31 AM.

  12. #12
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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).

  13. #13

    Thread Starter
    Junior Member
    Join Date
    Apr 2007
    Posts
    29

    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

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

    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.
    Last edited by schoolbusdriver; Jan 3rd, 2008 at 05:03 AM. Reason: Changed Task Manager to Task Scheduler.

  15. #15

    Thread Starter
    Junior Member
    Join Date
    Apr 2007
    Posts
    29

    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.

  16. #16
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    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.

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

    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:
    1. 'Form level code.
    2. Private Sub Form_Click()
    3.    MsgBox ShellDeleteSubKey(HKEY_LOCAL_MACHINE, "Software\SomeSubkey\AndAnotherOne")
    4. End Sub

    vb Code:
    1. 'Module level code.
    2. Public Declare Function SHDeleteKey Lib "shlwapi.dll" Alias "SHDeleteKeyA" _
    3.    (ByVal Hkey As Long, ByVal pszSubKey As String) As Long
    4.  
    5. Public Function ShellDeleteSubKey(lngHKey As Long, strSubKey As String) As Long
    6. 'Delete the SubKey and all contents including subkeys, and return a code.
    7.    ShellDeleteSubKey = SHDeleteKey(lngHKey, strSubKey)
    8. End Function

    EDIT: Rather than create a batch file, you could use the ShellExecute API, which accepts command line parameters.
    Last edited by schoolbusdriver; Jan 3rd, 2008 at 12:41 PM.

  18. #18

    Thread Starter
    Junior Member
    Join Date
    Apr 2007
    Posts
    29

    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.

  19. #19

    Thread Starter
    Junior Member
    Join Date
    Apr 2007
    Posts
    29

    Re: [RESOLVED] Problem with Deleting a registry key

    Can't get ShellExecute working with a shell and wait function that I made. Any ideas?

  20. #20
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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.

  21. #21
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: [RESOLVED] Problem with Deleting a registry key

    There is a "shellexecute and wait" example here: http://www.vbforums.com/showpost.php...3&postcount=25

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