Here's a much easier way:Usage: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 SubSHDeleteKey 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.vb Code:
DeleteKey "Software\MyCompany\MyApp" DeleteKey "Software\MyCompany\MyApp", rrLocalMachine
My signature has a link to a whole slew of registry functions wrapped up into a class. This code is from that class.




Reply With Quote