Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help w/ Reg Value Enum/Deletion 1

Status
Not open for further replies.

techkate

Programmer
Feb 23, 2004
118
US
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!

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
 
You are using the value <ERROR_NO_MORE_ITEMS>. Is it declared elsewhere?
Const ERROR_NO_MORE_ITEMS = 259&

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first.
'If we're supposed to work in Hex, why have we only got A fingers?'
Drive a Steam Roller
 
I believe I have all the Registry associated constants that I need declared at the global level:

' ----------------------------------------------------
' Declare the value data types
' ----------------------------------------------------
Const REG_SZ As Long = 1 ' Registry string
Const REG_DWORD As Long = 4 ' Registry number (32-bit number)

' ----------------------------------------------------
' Declare the keys that should exist.
' Typically applications will put information under HKEY_CURRENT_USER
' ----------------------------------------------------
Global Const HKEY_CLASSES_ROOT = &H80000000
Global Const HKEY_CURRENT_USER = &H80000001
Global Const HKEY_LOCAL_MACHINE = &H80000002
Global Const HKEY_USERS = &H80000003

' ----------------------------------------------------
' Errors
' ----------------------------------------------------
Global Const ERROR_NONE = 0
Global Const ERROR_BADDB = 1
Global Const ERROR_BADKEY = 2
Global Const ERROR_CANTOPEN = 3
Global Const ERROR_CANTREAD = 4
Global Const ERROR_CANTWRITE = 5
Global Const ERROR_OUTOFMEMORY = 6
Global Const ERROR_INVALID_PARAMETER = 7
Global Const ERROR_ACCESS_DENIED = 8
Global Const ERROR_INVALID_PARAMETERS = 87
Global Const ERROR_NO_MORE_ITEMS = 259

' ----------------------------------------------------
' Gives all users full access to the key
' ----------------------------------------------------
Global Const KEY_ALL_ACCESS = &H3F

' ----------------------------------------------------
' Creates a key that is persistent
' ----------------------------------------------------
Global Const REG_OPTION_NON_VOLATILE = 0


 
Are you sure:

Do While RegEnumValue(hKey, Cnt, sName, Ret, 0, ByVal 0&, [red]ByVal[/red] Data(0), RetData) <> ERROR_NO_MORE_ITEMS

 
Hmmm...strongm, I am actually not sure. I'll try removing the ByVal, it can't hurt.

The API Declare Statement is as follows:

Code:
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
 
Thank you, strongm. I removed the 'byval' and now it works without a hitch!

You make it look so easy...

Kate
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top