• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1212
  • Last Modified:

Delete only some value from "HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\TypedURLs"

We all know that web addresses are stored in registry, key: HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\TypedURLs. It's simply to have IE delete all of them or delete them manually by using registry.

The problem is: How to use VB6 to delete only some value from "HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\TypedURLs", for example: there are a lot of web addressses typed urls in registry, and I want to get them all listed in a VB combobox (or VB listbox) in order to see and delete them selectively as I expected.

I haved tried the code at: http://www.arcatapet.net/vbregget.cfm and http://www.arcatapet.net/vbregset.cfm but they are only useful if I know clearly the address and only get one address at a time, the problem still: I want to get them all listed in a VB combobox (or VB listbox) in order to see all and delete them selectively as I expected (VB6).

NOTE: from Microsoft: Values in this registry key are listed as Url1, Url2, Url3, and so on. If you delete only some values and the remaining values are not in consecutive numerical order, only some of the remaining entries are listed in the Address box. To prevent this behavior from occurring, rename the remaining values so that they are in consecutive numerical order.
0
taycuong76
Asked:
taycuong76
  • 2
  • 2
1 Solution
 
nffvrxqgrcfqvvcCommented:
If you are going to do this the basic breakdown you will need to both read and delete the values using API are below:
You should add them to an array so you can easily reference which values your deleting.

1. To open the handle to the key you could shoot for one of the API's below:

RegOpenCurrentUser, RegOpenKey

2. To enumerate the value name in the TypedUrls key you should use:
RegEnumValue

3. To Query the actual value data you should use
RegQueryValue,RegQueryValueEx

4. To delete the value use:
RegDeleteValue
0
 
ArkCommented:
Add listbox (style=checkbox) and command button on form

Option Explicit

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 RegEnumKey Lib "advapi32.dll" Alias "RegEnumKeyA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpName As String, ByVal cbName As Long) As Long
Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Private Declare Function RegEnumValue Lib "advapi32.dll" Alias "RegEnumValueA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpValueName As String, lpcbValueName As Long, ByVal lpReserved As Long, lpType As Long, lpData As Byte, lpcbData As Long) As Long
Private Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal lpValueName As String) As Long

Private Const ERROR_SUCCESS = 0&
Private Const HKEY_CURRENT_USER = &H80000001
Private Const KEY_ALL_ACCESS = &HF003F
Private Const REG_SZ = 1

Private Function EnumTypedURLs() As Variant
   Dim sURLs() As String
   Dim sName As String, sValue As String
   Dim abData() As Byte
   Dim hKey As Long, i As Long, count As Long
   Dim name_length As Long, value_length As Long
   If RegOpenKeyEx(HKEY_CURRENT_USER, "Software\Microsoft\Internet Explorer\TypedURLs", 0&, KEY_ALL_ACCESS, hKey) <> ERROR_SUCCESS Then
      MsgBox "Error opening key."
      Exit Function
   End If
   ReDim sURLs(0)
   Do
      name_length = 1024
      value_length = 1024
      sName = Space(name_length)
      ReDim abData(value_length)
      If RegEnumValue(hKey, i, sName, name_length, 0, REG_SZ, abData(0), value_length) <> ERROR_SUCCESS Then Exit Do
      sName = Left(sName, name_length)
      sValue = Left(StrConv(abData, vbUnicode), value_length)
      ReDim Preserve sURLs(i)
      sURLs(i) = sName & "=" & sValue
      i = i + 1
   Loop
   RegCloseKey hKey
   EnumTypedURLs = sURLs
End Function

Private Sub Command1_Click()
   Dim i As Long
   For i = 0 To List1.ListCount - 1
       If List1.Selected(i) Then
          DeleteTypedURLs List1.List(i)
       End If
   Next
   UpdateListBox
End Sub

Private Sub Form_Load()
   Caption = "Typed URLs"
   Command1.Caption = "Delete"
   UpdateListBox
End Sub

Private Sub DeleteTypedURLs(ByVal sURL As String)
   Dim hKey As Long
   If RegOpenKeyEx(HKEY_CURRENT_USER, "Software\Microsoft\Internet Explorer\TypedURLs", 0&, KEY_ALL_ACCESS, hKey) <> ERROR_SUCCESS Then
      MsgBox "Error opening key."
      Exit Sub
   End If
   RegDeleteValue hKey, Split(sURL, "=")(0)
   RegCloseKey hKey
End Sub

Private Sub UpdateListBox()
   Dim vURLs As Variant
   Dim i As Long
   List1.Clear
   vURLs = EnumTypedURLs
   If IsEmpty(vURLs) Then Exit Sub
   For i = 0 To UBound(vURLs)
       List1.AddItem (vURLs(i))
   Next i
End Sub

0
 
taycuong76Author Commented:
Wow! thanks Ark, your code worked like a champ ! You surely got the point.
If you don't mind, please help me to set the remaining values (after deleting some values) so that they are in consecutive numerical order.
Microsoft say that: Values in this registry key are listed as Url1, Url2, Url3, and so on. If you delete only some values and the remaining values are not in consecutive numerical order, only some of the remaining entries are listed in the Address box, the rests are useless.
Regards.
Tran Hoai Nam
0
 
ArkCommented:
Option Explicit

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 RegEnumKey Lib "advapi32.dll" Alias "RegEnumKeyA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpName As String, ByVal cbName As Long) As Long
Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Private Declare Function RegEnumValue Lib "advapi32.dll" Alias "RegEnumValueA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpValueName As String, lpcbValueName As Long, ByVal lpReserved As Long, lpType As Long, lpData As Byte, lpcbData As Long) As Long
Private Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal lpValueName As String) As Long
Private Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpData As Any, ByVal cbData As Long) As Long

Private Const ERROR_SUCCESS = 0&
Private Const HKEY_CURRENT_USER = &H80000001
Private Const KEY_ALL_ACCESS = &HF003F
Private Const REG_SZ = 1

Private Function EnumTypedURLs() As Variant
   Dim sURLs() As String
   Dim sName As String, sValue As String
   Dim abData() As Byte
   Dim hKey As Long, i As Long, count As Long
   Dim name_length As Long, value_length As Long
   If RegOpenKeyEx(HKEY_CURRENT_USER, "Software\Microsoft\Internet Explorer\TypedURLs", 0&, KEY_ALL_ACCESS, hKey) <> ERROR_SUCCESS Then
      MsgBox "Error opening key."
      Exit Function
   End If
   ReDim sURLs(0)
   Do
      name_length = 1024
      value_length = 1024
      sName = Space(name_length)
      ReDim abData(value_length)
      If RegEnumValue(hKey, i, sName, name_length, 0, REG_SZ, abData(0), value_length) <> ERROR_SUCCESS Then Exit Do
      sName = Left(sName, name_length)
      sValue = Left(StrConv(abData, vbUnicode), value_length)
      ReDim Preserve sURLs(i)
      sURLs(i) = sName & "=" & sValue
      i = i + 1
   Loop
   RegCloseKey hKey
   EnumTypedURLs = sURLs
End Function

Private Sub Command1_Click()
   Dim i As Long, count As Long
   Dim sURL As Variant
   For i = 0 To List1.ListCount - 1
       sURL = Split(List1.List(i), "=")
       DeleteTypedURLs Trim(sURL(0))
       If List1.Selected(i) = False Then
          count = count + 1
          RestoreTypedURLs "url" & count, Trim(sURL(1))
       End If
   Next
   UpdateListBox
End Sub

Private Sub Form_Load()
   Caption = "Typed URLs"
   Command1.Caption = "Delete"
   UpdateListBox
End Sub

Private Sub RestoreTypedURLs(ByVal sName As String, sValue As String)
   Dim hKey As Long
   If RegOpenKeyEx(HKEY_CURRENT_USER, "Software\Microsoft\Internet Explorer\TypedURLs", 0&, KEY_ALL_ACCESS, hKey) <> ERROR_SUCCESS Then
      MsgBox "Error opening key."
      Exit Sub
   End If
   
   If RegSetValueEx(hKey, sName, 0&, REG_SZ, ByVal sValue, Len(sValue)) Then
      MsgBox "Error setting value."
'      Exit Function
   End If
   RegCloseKey hKey
End Sub

Private Sub DeleteTypedURLs(ByVal sName As String)
   Dim hKey As Long
   If RegOpenKeyEx(HKEY_CURRENT_USER, "Software\Microsoft\Internet Explorer\TypedURLs", 0&, KEY_ALL_ACCESS, hKey) <> ERROR_SUCCESS Then
      MsgBox "Error opening key."
      Exit Sub
   End If
   RegDeleteValue hKey, sName
   RegCloseKey hKey
End Sub

Private Sub UpdateListBox()
   Dim vURLs As Variant
   Dim i As Long
   List1.Clear
   vURLs = EnumTypedURLs
   If IsEmpty(vURLs) Then Exit Sub
   For i = 0 To UBound(vURLs)
       List1.AddItem (vURLs(i))
   Next i
End Sub

0
 
taycuong76Author Commented:
Great! thanks and best wishes.
Regard.
Tran Hoai Nam
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now