|
-
Apr 11th, 2001, 10:25 AM
#1
How can I search the entire registry for a value and replace that value for every instance found?
-
Apr 12th, 2001, 11:42 AM
#2
Addicted Member
Here's an example...not pretty, but functional. Call the GetRegistryValues routine first to load a global array with all the keys, then call WriteRegistryValues to cycle through the array, get the values for the keys, check the values and replace as necessary.
Be cafeful with MY code, since it's purpose was to change all "C:\" into "E:\". Make your modifications before running this code!!!
Routines:
Code:
Public Sub WriteRegistryValues(RootToCheck As Long)
Dim sValueName As String
Dim lValueLen As Long
Dim lDataType As Long
Dim bData(0 To 254) As Byte
Dim lDataLen As Long
Dim sDataString As String
Dim hKey As Long
Dim lIndex As Long
Dim c As Long
Dim lRetVal As Long
Dim sAddItem As String
Dim lCount As Long
For lCount = 1 To UBound(aryKeys)
If Trim(aryKeys(lCount)) = "" Then Exit For
lRetVal = RegOpenKeyEx(RootToCheck, aryKeys(lCount), 0, KEY_READ_WRITE, hKey)
If lRetVal <> ERROR_SUCCESS Then
'Not used
Else
lIndex = 0
While lRetVal = 0
sValueName = Space(255)
lValueLen = 255
lDataLen = 255
lRetVal = RegEnumValue(hKey, lIndex, sValueName, lValueLen, 0, lDataType, bData(0), lDataLen)
If lRetVal = 0 Then
sValueName = Left(sValueName, lValueLen)
'This IF statement is used to exclude values we dont want to change...
If ((sValueName = "ProgramFilesDir") Or (sValueName = "CommonFilesDir")) And (aryKeys(lCount) = EXCLUDE_NODE) Then
GoTo SkipKey
End If
'This IF statement is used to change a SystemDrive value
If (sValueName = "ProgramFilesPath") And (aryKeys(lCount) = EXCLUDE_NODE) Then
Select Case lDataType
Case REG_SZ, REG_EXPAND_SZ
If lDataLen > 1 Then
sDataString = Space(lDataLen - 1)
CopyMemory ByVal sDataString, bData(0), lDataLen - 1
sDataString = UCase(sDataString)
If InStr(sDataString, "%SYSTEMDRIVE%") <> 0 Then
sDataString = Replace(sDataString, "%SYSTEMDRIVE%", "c:\")
sAddItem = sDataString & vbNullChar
lRetVal = RegSetValueEx(hKey, sValueName, 0, REG_SZ, ByVal sAddItem, Len(sAddItem))
End If
End If
End Select
GoTo SkipKey
End If
Select Case lDataType
Case REG_SZ, REG_EXPAND_SZ
If lDataLen > 1 Then
sDataString = Space(lDataLen - 1)
CopyMemory ByVal sDataString, bData(0), lDataLen - 1
If InStr(sDataString, "c:\") <> 0 Or InStr(sDataString, "C:\") <> 0 Then
sDataString = Replace(sDataString, "C:\", "E:\")
sDataString = Replace(sDataString, "c:\", "e:\")
sAddItem = sDataString & vbNullChar
lRetVal = RegSetValueEx(hKey, sValueName, 0, REG_SZ, ByVal sAddItem, Len(sAddItem))
End If
End If
End Select
End If
SkipKey:
lIndex = lIndex + 1
Wend
' Close the registry key.
lRetVal = RegCloseKey(hKey)
End If
Next lCount
End Sub
Public Sub GetRegistryKeys(RootToCheck As Integer)
Dim i As Integer
Dim lngKeyHandle As Long
Dim lngResult As Long
Dim lngCurIdx As Long
Dim strValue As String
Dim lngValueLen As Long
Dim strClass As String
Dim lngClassLen As Long
Dim strResult As String
Dim lngTime As FILETIME
Dim strSearch As String
Dim intSearchLen As Integer
Dim blnMatch As Boolean
Dim lArrayElementsUsed As Long
Dim lRetVal As Long
i = 0
strSearch = ""
intSearchLen = Len(strSearch)
Select Case RootToCheck
Case 1
lBranch = HKEY_CLASSES_ROOT
Case 2
lBranch = HKEY_CURRENT_USER
Case 3
lBranch = HKEY_LOCAL_MACHINE
Case 4
lBranch = HKEY_USERS
Case 5
lBranch = HKEY_CURRENT_CONFIG
Case 6
lBranch = HKEY_DYN_DATA
End Select
' Open the Root Branch to search
lngResult = RegOpenKeyEx(lBranch, "", 0&, KEY_READ, lngKeyHandle)
If lngResult <> ERROR_SUCCESS Then
Exit Sub
Else
ReDim aryKeys(1 To 100000) As String
lngCurIdx = 0
Do
lngValueLen = 2000
strValue = String(lngValueLen, 0)
lngClassLen = 2000
strClass = String(lngClassLen, 0)
' Enumerate all the sub keys
lngResult = RegEnumKeyExL(lngKeyHandle, _
lngCurIdx, _
ByVal strValue, _
lngValueLen, _
0&, _
ByVal strClass, _
lngClassLen, _
lngTime)
' Increment the index of found keys
lngCurIdx = lngCurIdx + 1
If lngResult = ERROR_SUCCESS Then
' Trim the current key to its actual length
strResult = Left(strValue, lngValueLen)
blnMatch = True
strValue = strResult
' Populate the array with keys that match the search criteria
If blnMatch Then
i = i + 1
aryKeys(i) = strValue
End If
End If
' Keep looking for more keys
Loop While lngResult = ERROR_SUCCESS
'This how many keys directly under the root key
lArrayElementsUsed = i
' Close the Root Branch
lngResult = RegCloseKey(lngKeyHandle)
i = 0
For lRetVal = 1 To 100000
If Trim(aryKeys(lRetVal)) = "" Then Exit For
' Open the Root Branch to search
lngResult = RegOpenKeyEx(lBranch, aryKeys(lRetVal), 0&, KEY_READ, lngKeyHandle)
lngValueLen = 2000
strValue = String(lngValueLen, 0)
lngClassLen = 2000
strClass = String(lngClassLen, 0)
lngCurIdx = 0
'Search through array elements for more keys
Do
lngValueLen = 2000
strValue = String(lngValueLen, 0)
lngClassLen = 2000
strClass = String(lngClassLen, 0)
' Enumerate all the sub keys
lngResult = RegEnumKeyExL(lngKeyHandle, _
lngCurIdx, _
ByVal strValue, _
lngValueLen, _
0&, _
ByVal strClass, _
lngClassLen, _
lngTime)
' Increment the index of found keys
lngCurIdx = lngCurIdx + 1
If lngResult = ERROR_SUCCESS Then
' Trim the current key to its actual length
strResult = Left(strValue, lngValueLen)
blnMatch = True
strValue = strResult
' Populate the array with keys that match the search criteria
If blnMatch Then
lArrayElementsUsed = lArrayElementsUsed + 1
aryKeys(lArrayElementsUsed) = aryKeys(lRetVal) & "\" & strValue
End If
End If
' Keep looking for more keys
Loop While lngResult = ERROR_SUCCESS
Next lRetVal
'Find values for the keys in the array
WriteRegistryValues lBranch
' Close the Root Branch
lngResult = RegCloseKey(lngKeyHandle)
End If
End Sub
Declares:
Code:
Private Declare Function RegOpenKeyEx Lib "advapi32.dll" _
Alias "RegOpenKeyExA" _
(ByVal hKey As Long, ByVal lpSupKey As String, _
ByVal ulOptions As Long, ByVal samDesired As Long, _
phkResult 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 RegQueryValueEx Lib "advapi32.dll" _
Alias "RegQueryValueExA" ( _
ByVal hKey As Long, _
ByVal lpszValueName As String, _
ByVal lpdwReserved As Long, _
lpdwType As Long, _
lpData As Any, _
lpcbData As Long) As Long
Building A Better Body Albeit Left Out Under Intense Extrapolation
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|