Visual Basic, .NET, ASP, VBScript
 

   
   
     

Форум - ASP и VBScript

Страница: 1 |

 

  Вопрос: Скрипт по редактированию реестра Добавлено: 04.08.11 08:06  

Автор вопроса:  gruver
Здравствуйте.
Может кто-нибудь подсказать реализацию такого а/р на любом языке:
1) Задаём "переменную для поиска" и переменную для замены (можно правкой вручную, можно с окном запроса);
2) Ищем переменную для поиска в реестре;
3) Заменяем найденную переменную на "переменную для замены";

Заранее благодарен за любую помощь.

Ответить

  Ответы Всего ответов: 2  

Номер ответа: 1
Автор ответа:
 Just



Вопросов: 4
Ответов: 330
 Профиль | | #1 Добавлено: 04.08.11 09:22
есть пример поиска в реестре на vb6 (ну а примеров записи ключа полно)

кидаешь в форму
  1.  
  2. Option Explicit
  3.  
  4. Dim WithEvents cReg As cRegSearch
  5.  
  6. Private Sub Form_Load()
  7. Set cReg = New cRegSearch
  8.  
  9. cReg.RootKey = HKEY_LOCAL_MACHINE
  10. cReg.SubKey = "SOFTWARE\Microsoft"
  11. cReg.SearchFlags = KEY_NAME * 1 + VALUE_NAME * 1 + VALUE_VALUE * 1 + WHOLE_STRING * 0
  12. cReg.SearchString = "WindowsUpdate"
  13. Me.Caption = "Searching....."
  14. cReg.DoSearch
  15. Me.Caption = "Done!"
  16. End Sub
  17.  
  18.  
  19. Private Sub cReg_SearchFound(ByVal sRootKey As String, ByVal sKey As String, ByVal sValue As Variant, ByVal lFound As FOUND_WHERE)
  20.    Dim sTemp As String
  21.    Select Case lFound
  22.          Case FOUND_IN_KEY_NAME
  23.               sTemp = "KEY NAME"
  24.          Case FOUND_IN_VALUE_NAME
  25.               sTemp = "VALUE NAME"
  26.          Case FOUND_IN_VALUE_VALUE
  27.               sTemp = "VALUE VALUE"
  28.    End Select
  29.    
  30. MsgBox sRootKey & " " & sKey & " " & sValue & " " & sTemp
  31. End Sub




создаешь класс с именем "cRegSearch"
  1.  
  2. Option Explicit
  3.  
  4. 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
  5. 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
  6. Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
  7. 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
  8.  
  9. Enum ROOT_KEYS
  10.      HKEY_ALL = &H0&
  11.      HKEY_CLASSES_ROOT = &H80000000
  12.      HKEY_CURRENT_USER = &H80000001
  13.      HKEY_LOCAL_MACHINE = &H80000002
  14.      HKEY_USERS = &H80000003
  15.      HKEY_PERFORMANCE_DATA = &H80000004
  16.      HKEY_CURRENT_CONFIG = &H80000005
  17.      HKEY_DYN_DATA = &H80000006
  18. End Enum
  19.  
  20. Enum SEARCH_FLAGS
  21.      KEY_NAME = 0
  22.      VALUE_NAME = 1
  23.      VALUE_VALUE = 2
  24.      WHOLE_STRING = 4
  25. End Enum
  26.  
  27. Enum FOUND_WHERE
  28.      FOUND_IN_KEY_NAME
  29.      FOUND_IN_VALUE_NAME
  30.      FOUND_IN_VALUE_VALUE
  31. End Enum
  32.  
  33. Private Const STANDARD_RIGHTS_ALL = &H1F0000
  34. Private Const KEY_QUERY_VALUE = &H1
  35. Private Const KEY_SET_VALUE = &H2
  36. Private Const KEY_CREATE_SUB_KEY = &H4
  37. Private Const KEY_ENUMERATE_SUB_KEYS = &H8
  38. Private Const KEY_NOTIFY = &H10
  39. Private Const KEY_CREATE_LINK = &H20
  40. Private Const SYNCHRONIZE = &H100000
  41. Private Const KEY_ALL_ACCESS = ((STANDARD_RIGHTS_ALL Or KEY_QUERY_VALUE Or KEY_SET_VALUE Or KEY_CREATE_SUB_KEY Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY Or KEY_CREATE_LINK) And (Not SYNCHRONIZE))
  42. Const KEY_READ = &H20019  ' ((READ_CONTROL Or KEY_QUERY_VALUE Or
  43.                           ' KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY) And (Not
  44.                           ' SYNCHRONIZE))
  45.  
  46. Private Const ERROR_SUCCESS = 0&
  47. Private Const ERR_MORE_DATA = 234&
  48. Private Const ERROR_NO_MORE_ITEMS = 259&
  49.  
  50. Private Const REG_NONE = 0
  51. Private Const REG_SZ = 1
  52. Private Const REG_EXPAND_SZ = 2
  53. Private Const REG_BINARY = 3
  54. Private Const REG_DWORD = 4
  55. Private Const REG_DWORD_LITTLE_ENDIAN = 4
  56. Private Const REG_DWORD_BIG_ENDIAN = 5
  57. Private Const REG_LINK = 6
  58. Private Const REG_MULTI_SZ = 7
  59. Private Const REG_RESOURCE_LIST = 8
  60. Private Const REG_FULL_RESOURCE_DESCRIPTOR = 9
  61. Private Const REG_RESOURCE_REQUIREMENTS_LIST = 10
  62.  
  63. Private Const MAX_KEY_SIZE = 260
  64. Private Const MAX_VALUE_SIZE = 4096
  65.  
  66. Private Declare Sub CopyMem Lib "kernel32" Alias "RtlMoveMemory" (pDest As Any, pSource As Any, ByVal ByteLen As Long)
  67.  
  68. Public Event SearchFound(ByVal sRootKey As String, ByVal sKey As String, ByVal sValue As Variant, ByVal lFound As FOUND_WHERE)
  69. Public Event SearchFinished(ByVal lReason As Long)
  70. Public Event SearchKeyChanged(ByVal sFullKeyName As String)
  71.  
  72. Private mvarRootKey As ROOT_KEYS
  73. Private mvarSearchFlags As SEARCH_FLAGS
  74. Private mvarSearchString As String
  75. Private mvarSubKey As String
  76.  
  77. Dim lStopSearch As Long
  78.  
  79. Public Property Let SubKey(ByVal vData As String)
  80.     mvarSubKey = vData
  81. End Property
  82.  
  83. Public Property Let SearchString(ByVal vData As String)
  84.     mvarSearchString = vData
  85. End Property
  86.  
  87. Public Property Let SearchFlags(ByVal vData As SEARCH_FLAGS)
  88.     mvarSearchFlags = vData
  89. End Property
  90.  
  91. Public Property Let RootKey(ByVal vData As ROOT_KEYS)
  92.     mvarRootKey = vData
  93. End Property
  94.  
  95. Public Sub DoSearch()
  96.     If mvarRootKey <> HKEY_ALL Then
  97.        If (mvarSearchFlags And VALUE_NAME) = VALUE_NAME Or (mvarSearchFlags And VALUE_VALUE) = VALUE_VALUE Then
  98.           Call EnumRegValues(mvarRootKey, mvarSubKey)
  99.        End If
  100.        Call EnumRegKeys(mvarRootKey, mvarSubKey)
  101.     Else
  102.        Call EnumRegKeys(HKEY_CLASSES_ROOT, mvarSubKey)
  103.        If lStopSearch Then GoTo Search_Terminated
  104.        Call EnumRegKeys(HKEY_CURRENT_USER, mvarSubKey)
  105.        If lStopSearch Then GoTo Search_Terminated
  106.        Call EnumRegKeys(HKEY_LOCAL_MACHINE, mvarSubKey)
  107.        If lStopSearch Then GoTo Search_Terminated
  108.        Call EnumRegKeys(HKEY_USERS, mvarSubKey)
  109.        If lStopSearch Then GoTo Search_Terminated
  110.        Call EnumRegKeys(HKEY_PERFORMANCE_DATA, mvarSubKey)
  111.        If lStopSearch Then GoTo Search_Terminated
  112.        Call EnumRegKeys(HKEY_CURRENT_CONFIG, mvarSubKey)
  113.        If lStopSearch Then GoTo Search_Terminated
  114.        Call EnumRegKeys(HKEY_DYN_DATA, mvarSubKey)
  115.     End If
  116. Search_Terminated:
  117.     RaiseEvent SearchFinished(lStopSearch)
  118.     lStopSearch = 0
  119. End Sub
  120.  
  121. Public Sub StopSearch()
  122.     lStopSearch = 1
  123. End Sub
  124.  
  125. Private Sub EnumRegKeys(ByVal lKeyRoot As Long, ByVal sSubKey As String)
  126.     Dim curidx As Long
  127.     Dim KeyName As String
  128.     Dim hKey As Long
  129.     Dim sTemp As String
  130.     If lStopSearch Then Exit Sub
  131.     On Error GoTo ErrEnum
  132.     If RegOpenKeyEx(lKeyRoot, sSubKey, 0, KEY_READ, hKey) Then Exit Sub
  133.     Do
  134.       DoEvents
  135.       KeyName = Space$(MAX_KEY_SIZE)
  136.       If RegEnumKey(hKey, curidx, KeyName, MAX_KEY_SIZE) <> ERROR_SUCCESS Then Exit Do
  137.       curidx = curidx + 1
  138.       KeyName = TrimNull(KeyName)
  139.       If sSubKey <> "" Then
  140.          sTemp = sSubKey & "\" & KeyName
  141.       Else
  142.          sTemp = KeyName
  143.       End If
  144. '****************************************************
  145. 'This event is used for showing currently viewing key.
  146. 'Usually you don't need this.
  147. 'To increase performance, remove this event
  148.       If lStopSearch = 0 Then RaiseEvent SearchKeyChanged(RootKeyName(lKeyRoot) & "\" & sTemp)
  149. '****************************************************
  150.       If (mvarSearchFlags And KEY_NAME) = KEY_NAME Then
  151.          If CheckMatching(KeyName) Then
  152.             RaiseEvent SearchFound(RootKeyName(lKeyRoot), sTemp, "*", FOUND_IN_KEY_NAME)
  153.          End If
  154.       End If
  155.       If (mvarSearchFlags And VALUE_NAME) = VALUE_NAME Or (mvarSearchFlags And VALUE_VALUE) = VALUE_VALUE Then
  156.          Call EnumRegValues(lKeyRoot, sTemp)
  157.       End If
  158.       Call EnumRegKeys(lKeyRoot, sTemp)
  159.     Loop
  160. ErrEnum:
  161.     If Err Then lStopSearch = Err
  162.     RegCloseKey hKey
  163. End Sub
  164.  
  165. Private Sub EnumRegValues(ByVal lKeyRoot As Long, ByVal sSubKey As String)
  166.    Dim curidx As Long, ValueName As String, ValueValue As String
  167.    Dim hKey As Long
  168.    Dim lType As Long
  169.    Dim arrData() As Byte
  170.    Dim cbDataSize As Long
  171.    If lStopSearch Then Exit Sub
  172.    On Error GoTo ErrEnum
  173.    If RegOpenKeyEx(lKeyRoot, sSubKey, 0, KEY_READ, hKey) Then Exit Sub
  174.    Do
  175.      ValueName = String(MAX_KEY_SIZE, 0)
  176.      cbDataSize = MAX_VALUE_SIZE
  177.      ReDim arrData(cbDataSize - 1)
  178.      If RegEnumValue(hKey, curidx, ValueName, MAX_KEY_SIZE, ByVal 0&, lType, arrData(0), cbDataSize) <> ERROR_SUCCESS Then Exit Do
  179.      If cbDataSize < 1 Then cbDataSize = 1
  180.      ReDim Preserve arrData(cbDataSize - 1)
  181.      ValueName = TrimNull(ValueName)
  182.      If (mvarSearchFlags And VALUE_NAME) = VALUE_NAME Then
  183.         If CheckMatching(ValueName) Then RaiseEvent SearchFound(RootKeyName(lKeyRoot), sSubKey & "\" & ValueName, GetRegData(lType, arrData), FOUND_IN_VALUE_NAME)
  184.      End If
  185.      If (mvarSearchFlags And VALUE_VALUE) = VALUE_VALUE Then
  186.         ValueValue = TrimNull(GetRegData(lType, arrData))
  187.         If CheckMatching(ValueValue) Then
  188.            RaiseEvent SearchFound(RootKeyName(lKeyRoot), sSubKey & "\" & ValueName, ValueValue, FOUND_IN_VALUE_VALUE)
  189.         End If
  190.      End If
  191.      curidx = curidx + 1
  192.    Loop
  193. ErrEnum:
  194.    If Err Then lStopSearch = Err
  195.    RegCloseKey hKey
  196. End Sub
  197.  
  198. Private Function TrimNull(startstr As String) As String
  199.    Dim pos As Integer
  200.    pos = InStr(startstr, Chr$(0))
  201.    If pos Then
  202.       TrimNull = Left$(startstr, pos - 1)
  203.       Exit Function
  204.    End If
  205.    TrimNull = startstr
  206. End Function
  207.  
  208. Private Function CheckMatching(ByVal sCheck As String) As Boolean
  209.    If (mvarSearchFlags And WHOLE_STRING) = WHOLE_STRING Then
  210.       CheckMatching = (UCase(sCheck) = UCase(mvarSearchString))
  211.    Else
  212.       CheckMatching = InStr(1, sCheck, mvarSearchString, vbTextCompare)
  213.    End If
  214. End Function
  215.  
  216. Private Function GetRegData(ByVal lType As Long, abData() As Byte) As String
  217.    Dim lData As Long, i As Long
  218.    Dim sTemp As String
  219.    sTemp = ""
  220.    Select Case lType
  221.         Case REG_SZ, REG_MULTI_SZ
  222.              GetRegData = TrimNull(StrConv(abData, vbUnicode))
  223.         Case REG_DWORD
  224.              CopyMem lData, abData(0), 4&
  225.              GetRegData = "0x" & Format(Hex(lData), "00000000") & "(" & lData & ")"
  226.         Case REG_BINARY
  227.              For i = 0 To UBound(abData)
  228.                  sTemp = sTemp & Right("00" & Hex(abData(i)), 2) & " "
  229.              Next i
  230.              GetRegData = Left(sTemp, Len(sTemp) - 1)
  231.         Case Else
  232.              GetRegData = "Temporary unsupported"
  233.    End Select
  234. End Function
  235.  
  236. Private Function RootKeyName(lKey As Long) As String
  237.    Select Case lKey
  238.        Case HKEY_CLASSES_ROOT: RootKeyName = "HKEY_CLASSES_ROOT"
  239.        Case HKEY_CURRENT_USER: RootKeyName = "HKEY_CURRENT_USER"
  240.        Case HKEY_LOCAL_MACHINE: RootKeyName = "HKEY_LOCAL_MACHINE"
  241.        Case HKEY_USERS: RootKeyName = "HKEY_USERS"
  242.        Case HKEY_PERFORMANCE_DATA: RootKeyName = "HKEY_PERFORMANCE_DATA"
  243.        Case HKEY_CURRENT_CONFIG: RootKeyName = "HKEY_CURRENT_CONFIG"
  244.        Case HKEY_DYN_DATA: RootKeyName = "HKEY_DYN_DATA"
  245.    End Select
  246. End Function
  247. Private Sub Class_Initialize()
  248.    mvarRootKey = HKEY_ALL
  249.    mvarSubKey = ""
  250.    mvarSearchString = ""
  251. End Sub
  252.  
  253. Private Sub Class_Terminate()
  254.   lStopSearch = 1
  255. End Sub

Ответить

Номер ответа: 2
Автор ответа:
 chialexus



ICQ: 321922 

Вопросов: 3
Ответов: 53
 Web-сайт: chialexus.narod.ru
 Профиль | | #2
Добавлено: 07.08.11 18:06
Есть возможность работы с реестром используя WMI через VBScript. Описание и примеры тут - http://script-coding.com/WMI_StdRegProv.html.

Ответить

Страница: 1 |

Поиск по форуму



© Copyright 2002-2011 VBNet.RU | Пишите нам