I'm relatively new to editing the registry through VB, specifically with using the RegEnumValue API call. The following code is supposed to enumerate a list of values within a particular registry key. It works fine in the IDE, I've been using it to delete Internet Explorer URL history. However, when I compile and run the executable I get an unhandled error in ntdll.dll.
Using a log file, I've determined that in the following routine, I successfully iterate once through the Do...Loop but that's it. Nothing after the log line stating "Looping to next iteration...". So obviously there's a problem with my subsequent call(s) to RegEnumValue. I don't know why!
Any suggestions are greatly appreciated!
Using a log file, I've determined that in the following routine, I successfully iterate once through the Do...Loop but that's it. Nothing after the log line stating "Looping to next iteration...". So obviously there's a problem with my subsequent call(s) to RegEnumValue. I don't know why!
Any suggestions are greatly appreciated!
Code:
Private Function EnumKeyValues(ByVal lpParentKey As Long, sKeyName As String, psaRegEnum() As String) As Long
Dim lRetVal As Long 'result of the API functions
Dim hKey As Long 'handle of opened key
Dim vValue As Variant 'setting of queried value
Dim sMsg As String
Dim Cnt As Long
Dim sName As String
Dim Data(0 To 254) As Byte
Dim Ret As Long
Dim RetData As Long
Const BUFFER_SIZE As Long = 255
On Error GoTo EnumKeyValues_Error
' ----------------------------------------------------
' Initialize Variables
' ----------------------------------------------------
LogIt gsAppLog, "Procedure: EnumKeyValues; " & "Initializing..."
EnumKeyValues = 0
Cnt = 0
sName = Space(BUFFER_SIZE)
Ret = BUFFER_SIZE
RetData = BUFFER_SIZE
' ----------------------------------------------------
' Open the key
' ----------------------------------------------------
LogIt gsAppLog, "Procedure: EnumKeyValues; " & "Opening Key " & sKeyName & "..."
lRetVal = RegOpenKeyEx(lpParentKey, sKeyName, 0, KEY_ALL_ACCESS, hKey)
LogIt gsAppLog, "Procedure: EnumKeyValues; " & "Opened Key " & sKeyName & "."
' ----------------------------------------------------
' Enumerate the values
' ----------------------------------------------------
LogIt gsAppLog, "Procedure: EnumKeyValues; " & "Calling RegEnumValue..."
[COLOR=blue]Do While RegEnumValue(hKey, Cnt, sName, Ret, 0, ByVal 0&, ByVal Data(0), RetData) <> ERROR_NO_MORE_ITEMS[/color]
LogIt gsAppLog, "Procedure: EnumKeyValues; " & "Returned from RegEnumValue; RetData = " & RetData & "."
If RetData > 0 Then
' --------------------------------------------
' Preserve value name
' --------------------------------------------
LogIt gsAppLog, "Procedure: EnumKeyValues; " & "Redimming array for element # " & Cnt & "..."
ReDim Preserve psaRegEnum(Cnt) As String
LogIt gsAppLog, "Procedure: EnumKeyValues; " & "Setting array element # " & Cnt & " to " & sName & "..."
psaRegEnum(Cnt) = sName
If EnumKeyValues = 0 Then
LogIt gsAppLog, "Procedure: EnumKeyValues; " & "Setting EnumKeyValues to 1..."
EnumKeyValues = 1
End If
End If
' ------------------------------------------------
' Prepare for next value
' ------------------------------------------------
LogIt gsAppLog, "Procedure: EnumKeyValues; " & "Preparing for next value..."
Cnt = Cnt + 1
LogIt gsAppLog, "Procedure: EnumKeyValues; " & "Successfully set Cnt = " & Cnt & "."
sName = Space(BUFFER_SIZE)
LogIt gsAppLog, "Procedure: EnumKeyValues; " & "Successfully set Ret = " & Ret & "."
Ret = BUFFER_SIZE
LogIt gsAppLog, "Procedure: EnumKeyValues; " & "Successfully set RetData = " & RetData & "."
RetData = BUFFER_SIZE
LogIt gsAppLog, "Procedure: EnumKeyValues; " & "Looping to next iteration..."
Loop
' ----------------------------------------------------
' Close the key
' ----------------------------------------------------
LogIt gsAppLog, "Procedure: EnumKeyValues; " & "Closing the Registry Key..."
RegCloseKey (hKey)
LogIt gsAppLog, "Procedure: EnumKeyValues; " & "Registry Key Closed."
EnumKeyValues_End:
Exit Function
EnumKeyValues_Error:
EnumKeyValues = 0
sMsg = "An Error has occurred while enumerating values from the system registry." & vbCrLf & vbCrLf & _
"Error " & Err.Number & ": " & Err.Description
MsgBox sMsg, vbOKOnly, "Registry Error"
Beep
End Function