Searching the Registry


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Searching the Registry

  1. #1
    Darius Guest

    Searching the Registry


    Hi there,

    I am trying to perform a search for all ShellNew Keys within the registry...

    Is there any C++ Registry commands out there that would allow direct search
    of registry without knowing the full path?

    Is RegQueryValueEx the only option available for querying the registry? Can
    it handle wildcard *'s ?

  2. #2
    Steve Wade Guest

    Re: Searching the Registry

    There is no wildcard API to the registry. Use RegEnumKey or RegEnumKeyEx to
    do a recursive search yourself.

    "Darius" <a_khalili@hotmail.com> wrote in message
    news:39f74799$1@news.devx.com...
    >
    > Hi there,
    >
    > I am trying to perform a search for all ShellNew Keys within the

    registry...
    >
    > Is there any C++ Registry commands out there that would allow direct

    search
    > of registry without knowing the full path?
    >
    > Is RegQueryValueEx the only option available for querying the registry?

    Can
    > it handle wildcard *'s ?




  3. #3
    Will Rickards Guest

    Re: Searching the Registry

    So you don't have to write the code... well you have to turn it into C..


    Public Function ReadKey(ByVal hRoot As Long, _
    ByVal strSection As String, ByVal strKey As String, _
    Optional ByVal Default As Variant = Empty) As Variant
    ' strSection specifies the section that contains strKey
    ' strKey specifies the specific key to read
    ' Default specifies the value to return if no data
    ' ***** handles only strings, binary, and dword (not big endian) *****

    Dim lngResult As Long
    Dim hKey As Long
    Dim eType As KeyType
    Dim lngSize As Long
    Dim strValue As String
    Dim abValue() As Byte
    Dim lngValue As Long

    ' pessimistic return value
    ReadKey = Default

    ' attempt to open key
    lngResult = _
    RegOpenKeyEx(hRoot, strSection, 0&, KEY_ALL_ACCESS, hKey)

    ' if successful
    If lngResult = ERROR_SUCCESS Then

    ' get value information (size and type)
    lngResult = RegQueryValueEx(hKey, strKey, 0&, eType, 0&, lngSize)

    ' if there is data there
    If lngResult = ERROR_MORE_DATA And lngSize > 0 Then

    Select Case eType

    Case REG_NONE

    Case REG_SZ, REG_MULTI_SZ, REG_EXPAND_SZ

    ' allocate memory
    strValue = String$(lngSize, 0)

    ' retrieve data
    lngResult = _
    RegQueryValueEx(hKey, strKey, 0&, eType, _
    ByVal strValue, lngSize)

    ' if successful
    If lngResult = ERROR_SUCCESS Then

    ' if string needs expanding
    If eType = REG_EXPAND_SZ Then

    ' return expanded string
    ReadKey = ExpandEnvStrings(strValue)

    ' if string doesn't need expanding
    Else

    ' return string without null terminator
    ReadKey = StringFromBuffer(strValue)

    End If ' eType = REG_EXPAND_SZ

    End If ' lngResult = ERROR_SUCCESS

    ' deallocate memory
    strValue = vbNullString

    Case REG_BINARY

    ' allocate memory
    ReDim abValue(0 To lngSize - 1) As Byte

    ' retrieve data
    lngResult = RegQueryValueEx(hKey, strKey, 0&, eType, _
    abValue(0), lngSize)

    ' if successful
    If lngResult = ERROR_SUCCESS Then

    ' return array
    ReadKey = abValue

    End If ' lngResult = ERROR_SUCCESS

    ' deallocate memory
    Erase abValue

    Case REG_DWORD

    ' retrieve data
    lngResult = _
    RegQueryValueEx(hKey, strKey, 0&, eType, _
    lngValue, lngSize)

    ' if successful
    If lngResult = ERROR_SUCCESS Then

    ' return value
    ReadKey = lngValue

    End If ' lngResult = ERROR_SUCCESS

    Case REG_DWORD_BIG_ENDIAN

    ' retrieve data
    lngResult = RegQueryValueEx(hKey, strKey, 0&, eType, _
    lngValue, lngSize)

    ' if successful
    If lngResult = ERROR_SUCCESS Then



    End If ' lngResult = ERROR_SUCCESS

    Case REG_LINK

    Case REG_RESOURCE_LIST

    Case Else

    End Select

    ' close key
    Call RegCloseKey(hKey)

    End If ' lngResult = ERROR_SUCCESS

    End If ' lngResult = ERROR_SUCCESS

    End Function ' ReadKey

    Public Function WriteKey(ByVal hRoot As Long, _
    ByVal strSection As String, ByVal strKey As String, _
    ByVal eType As KeyType, ByRef varValue As Variant) As Boolean
    ' strSection specifies the section that contains strKey
    ' strKey specifies the specific key to write
    ' varValue specifies the value to set the key to
    ' ***** Currrently only handles strings *****

    Dim lngResult As Long
    Dim hKey As Long
    Dim lngKeyState As Long

    ' create and open key
    lngResult = RegCreateKeyEx(hRoot, strSection, 0&, vbNullString, _
    REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, ByVal 0&, hKey, lngKeyState)

    ' if successful
    If lngResult = ERROR_SUCCESS Then

    Select Case eType

    Case REG_NONE

    Case REG_SZ, REG_EXPAND_SZ

    Dim strValue As String

    ' get value in native format (with Null terminator)
    strValue = varValue & Chr(0)

    ' set key's value
    lngResult = RegSetValueEx(hKey, strKey, 0&, eType, _
    ByVal strValue, Len(strValue))

    Case REG_BINARY

    Dim abytValue() As Byte

    ' get value in native format
    abytValue = varValue

    ' set key's value
    lngResult = RegSetValueEx(hKey, strKey, 0&, eType, _
    abytValue(0), UBound(abytValue) - LBound(abytValue) + 1)

    Erase abytValue

    Case REG_DWORD

    Dim lngValue As Long

    ' get value in native format
    lngValue = varValue

    ' set key's value
    lngResult = RegSetValueEx(hKey, strKey, 0&, eType, _
    lngValue, Len(lngValue))

    Case REG_DWORD_BIG_ENDIAN

    Case REG_LINK

    Case REG_MULTI_SZ

    Case REG_RESOURCE_LIST

    Case Else

    End Select

    ' close key
    Call RegCloseKey(hKey)

    End If ' lngResult = ERROR_SUCCESS

    ' return result
    WriteKey = (lngResult = ERROR_SUCCESS)

    End Function ' WriteKey

    Public Function SubKeys( _
    ByVal eRootKey As RootKey, ByVal strSection As String) As Collection
    ' returns a collection of with the subkey names of the given key
    ' 07/10/2000 - WSR

    Dim lngNameSize As Long
    Dim lngDataSize As Long
    Dim lngResult As Long
    Dim lngIndex As Long
    Dim lngType As Long
    Dim lngNumValues As Long
    Dim lngMaxValueNameLen As Long
    Dim strKey As String
    Dim ftTemp As FILETIME
    Dim hRoot As Long
    Dim colKeys As Collection

    ' attempt to open section
    lngResult = _
    RegOpenKeyEx(eRootKey, strSection, 0&, KEY_ALL_ACCESS, hRoot)

    ' if successful
    If lngResult = ERROR_SUCCESS Then

    ' create collection
    Set colKeys = New Collection

    ' get information about the values
    lngResult = RegQueryInfoKey(hRoot, 0&, 0, 0, 0, 0, 0, _
    lngNumValues, lngMaxValueNameLen, 0, 0, ftTemp)

    Do While lngResult = ERROR_SUCCESS

    ' init parameters
    lngNameSize = 0
    lngType = 0
    lngNameSize = lngMaxValueNameLen + 1
    strKey = String$(lngNameSize, 0)

    ' get value information
    lngResult = RegEnumValue(hRoot, 0, strKey, lngNameSize, _
    0, lngType, ByVal 0, lngDataSize)

    ' if successful
    If lngResult = ERROR_SUCCESS Then

    ' add name to collection
    colKeys.Add Left$(strKey, lngNameSize)

    End If ' lngResult = ERROR_SUCCESS

    Loop

    ' return collection
    Set SubKeys = colKeys

    ' remove reference
    Set colKeys = Nothing

    End If ' lngResult = ERROR_SUCCESS

    End Function ' SubKeys

    Private Function FindRegEntry(ByRef hRoot As Long, _
    ByRef strSearch As String, ByRef strSection As String, _
    ByRef strKey As String, ByRef varData As Variant, _
    Optional ByVal blnMatchSections As Boolean = True, _
    Optional ByVal blnMatchKeys As Boolean = True, _
    Optional ByVal blnMatchData As Boolean = True) As Boolean

    Dim lngNameSize As Long
    Dim lngResult As Long
    Dim lngIndex As Long
    Dim lngType As Long
    Dim lngKeyCount As Long
    Dim lngMaxKeyLen As Long
    Dim lngSectionCount As Long
    Dim lngMaxSectionLen As Long
    Dim strTemp As String
    Dim ftTemp As FILETIME
    Dim blnContinue As Boolean
    Dim hSubSection As Long

    ' init search status
    blnContinue = True

    ' search the root section

    ' get information about the keys/sections in the root section
    lngResult = RegQueryInfoKey(hRoot, ByVal 0, 0, 0, lngSectionCount, _
    lngMaxSectionLen, 0, lngKeyCount, lngMaxKeyLen, 0, 0, ftTemp)

    ' if information retrieval was successful
    If lngResult = ERROR_SUCCESS Then

    ' adjust max lengths for terminating null
    lngMaxKeyLen = lngMaxKeyLen + 1
    lngMaxSectionLen = lngMaxSectionLen + 1

    ' if there are keys and we are searching keys/data
    If lngKeyCount > 0 And (blnMatchKeys Or blnMatchData) Then

    ' loop through the keys in the root section
    lngIndex = 0
    Do

    ' init parameters
    lngType = 0
    lngNameSize = lngMaxKeyLen
    strTemp = String$(lngNameSize, 0)

    ' get key information
    lngResult = RegEnumValue(hRoot, lngIndex, strTemp, _
    lngNameSize, 0, lngType, ByVal 0, 0)

    ' if successful
    If lngResult = ERROR_SUCCESS Then

    ' get key name
    strTemp = StringFromBuffer(strTemp)

    ' if we are searching keys
    If blnMatchKeys Then

    ' if search string found in key name
    If InStr(1, UCase$(strTemp), _
    UCase$(strSearch)) <> 0 Then

    ' return info
    strSection = vbNullString
    strKey = strTemp
    varData = ReadKey(hRoot, vbNullString, strTemp)

    ' stop search
    blnContinue = False

    End If ' search string found in key name

    ' if we are searching data and search string not found
    ElseIf blnMatchData And blnContinue Then

    ' save key name
    strKey = strTemp

    ' if type of data can be compared to search string
    Select Case lngType

    Case REG_SZ, REG_MULTI_SZ, REG_EXPAND_SZ

    ' get data
    strTemp = ReadKey(hRoot, vbNullString, _
    strTemp, vbNullString)

    ' if search string found in data
    If InStr(1, UCase$(strTemp), _
    UCase$(strSearch)) <> 0 Then

    ' return info
    strSection = vbNullString
    varData = strTemp

    ' stop search
    blnContinue = False

    ' if search string not found in data
    Else

    ' remove erroneous return info
    strKey = vbNullString

    End If ' search string found in data

    Case Else

    ' remove erroneous return info
    strKey = vbNullString

    End Select

    End If ' blnMatchKeys

    ' increment index
    lngIndex = lngIndex + 1

    End If ' lngResult = ERROR_SUCCESS

    Loop While lngResult = ERROR_SUCCESS And blnContinue

    End If ' lngKeyCount > 0 And (blnMatchKeys Or blnMatchData)

    ' if search should continue and there are sections
    If blnContinue And lngSectionCount > 0 Then

    ' search the subsections

    ' loop through subsections
    lngIndex = 0
    Do

    ' init parameters
    lngNameSize = lngMaxSectionLen
    strTemp = String$(lngNameSize, 0)

    ' get name of subsection
    lngResult = RegEnumKeyEx(hRoot, lngIndex, _
    ByVal strTemp, lngNameSize, 0, ByVal 0&, 0, ftTemp)

    ' if name retrieval was successful
    If lngResult = ERROR_SUCCESS Or _
    lngResult = ERROR_MORE_DATA Then

    ' get section name from buffer
    strTemp = StringFromBuffer(strTemp)

    ' if we are matching section names
    If blnMatchSections Then

    ' if search string found in section name
    If InStr(1, UCase$(strTemp), _
    UCase$(strSearch)) <> 0 Then

    ' return info
    strSection = strTemp

    ' end search
    blnContinue = False

    End If ' search string found in section name

    End If ' blnMatchSections

    ' if search should continue
    If blnContinue Then

    ' attempt to open subsection
    lngResult = RegOpenKeyEx(hRoot, strTemp, 0&, _
    KEY_ALL_ACCESS, hSubSection)

    ' if open successful
    If lngResult = ERROR_SUCCESS Then

    ' make a recursive call
    blnContinue = _
    Not FindRegEntry(hSubSection, strSearch, _
    strSection, strKey, varData, blnMatchSections, _
    blnMatchKeys, blnMatchData)

    ' if string found
    If Not blnContinue Then

    ' if section not set
    If Len(strSection) = 0 Then

    ' assign it section name
    strSection = strTemp

    ' if section already set
    Else

    ' prepend section name to strSection
    strSection = _
    strTemp & cstrSeparator & strSection

    End If ' Len(strSection) = 0

    End If ' Not blnContinue

    ' release handle
    Call RegCloseKey(hSubSection)

    End If ' lngResult = ERROR_SUCCESS

    End If ' blnContinue

    End If ' lngResult = ERROR_SUCCESS

    ' increment index
    lngIndex = lngIndex + 1

    Loop While lngResult = ERROR_SUCCESS And blnContinue

    End If ' blnContinue And lngSectionCount > 0

    End If ' lngResult = ERROR_SUCCESS

    ' return search status
    FindRegEntry = Not blnContinue

    End Function ' FindRegEntry

    Public Function FindKey( _
    ByVal eRootKey As RootKey, ByVal strRootSection As String, _
    ByVal strSearch As String, ByRef strSection As String, _
    ByRef strKey As String, ByRef varData As Variant, _
    Optional ByVal blnMatchSections As Boolean = True, _
    Optional ByVal blnMatchKeys As Boolean = True, _
    Optional ByVal blnMatchData As Boolean = True) As Boolean
    ' attempts to find the given search string starting at the
    ' given root section. Returns success. If found the the
    ' section, key, and data are returned through the byref parameters.
    ' 07/10/2000 - WSR

    Dim hRoot As Long
    Dim lngResult As Long

    ' attempt to open root section
    lngResult = _
    RegOpenKeyEx(eRootKey, strRootSection, 0&, KEY_ALL_ACCESS, hRoot)

    ' if successful
    If lngResult = ERROR_SUCCESS Then

    ' attempt to find registry entry
    FindKey = FindRegEntry(hRoot, strSearch, strSection, strKey, _
    varData, blnMatchSections, blnMatchKeys, blnMatchData)

    ' close registry key
    Call RegCloseKey(hRoot)

    End If ' lngResult = ERROR_SUCCESS

    End Function ' FindKey

    Will Rickards

    "Steve Wade" <stevew@prophet.co.uk> wrote in message news:39f7f4e4@news.devx.com...
    > There is no wildcard API to the registry. Use RegEnumKey or RegEnumKeyEx to
    > do a recursive search yourself.
    >
    > "Darius" <a_khalili@hotmail.com> wrote in message
    > news:39f74799$1@news.devx.com...
    > >
    > > Hi there,
    > >
    > > I am trying to perform a search for all ShellNew Keys within the

    > registry...
    > >
    > > Is there any C++ Registry commands out there that would allow direct

    > search
    > > of registry without knowing the full path?
    > >
    > > Is RegQueryValueEx the only option available for querying the registry?

    > Can
    > > it handle wildcard *'s ?

    >
    >




  4. #4
    Rick Powell Guest

    Re: Searching the Registry


    Ok, so can you supply the code to funtion ExpandEnvStrings ?

    I need to figure out how to expand a REG_EXPAND_SZ value.

    Thanks.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center