批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程
[批处理文件精品]批处理版照片整理器[批处理文件精品]纯批处理备份&还原驱动在线第三方下载
返回列表 发帖

[转贴] VBS脚本注册表操作类代码

从国外的一个庞大脚本提取出来的注册表操作类,喜欢的朋友可以收藏下

  1. Option Explicit
  2. Const WBEM_MAX_WAIT = &H80
  3. ' Registry Hives
  4. Const HKEY_LOCAL_MACHINE = &H80000002
  5. Const HKEY_CURRENT_USER = &H80000001
  6. Const HKEY_CLASSES_ROOT = &H80000000
  7. Const HKEY_USERS = &H80000003
  8. Const HKEY_CURRENT_CONFIG = &H80000005
  9. Const HKEY_DYN_DATA = &H80000006
  10. ' Reg Value Types
  11. Const REG_SZ = 1
  12. Const REG_EXPAND_SZ = 2
  13. Const REG_BINARY = 3
  14. Const REG_DWORD = 4
  15. Const REG_MULTI_SZ = 7
  16. ' Registry Permissions
  17. Const KEY_QUERY_VALUE = &H00001
  18. Const KEY_SET_VALUE = &H00002
  19. Const KEY_CREATE_SUB_KEY = &H00004
  20. Const KEY_ENUMERATE_SUB_KEYS = &H00008
  21. Const KEY_NOTIFY = &H00016
  22. Const KEY_CREATE = &H00032
  23. Const KEY_DELETE = &H10000
  24. Const KEY_READ_CONTROL = &H20000
  25. Const KEY_WRITE_DAC = &H40000
  26. Const KEY_WRITE_OWNER = &H80000
  27. Class std_registry
  28. Private Sub Class_Initialize()
  29. Set objRegistry = Nothing
  30. End Sub
  31. ' Connect to the reg provider for this registy object
  32. Public Function ConnectProvider32( sComputerName )
  33. ConnectProvider32 = False
  34. Set objRegistry = Nothing
  35. 'On Error Resume Next
  36. Dim oLoc : Set oLoc = CreateObject("Wbemscripting.SWbemLocator")
  37. Dim oCtx : Set oCtx = CreateObject("WbemScripting.SWbemNamedValueSet")
  38. ' Force 64 Bit Registry
  39. Call oCtx.Add("__ProviderArchitecture", 32 )
  40. Call oCtx.Add("__RequiredArchitecture", True)
  41. Dim oSvc : Set oSvc = oLoc.ConnectServer(sComputerName,"root\default","","",,,WBEM_MAX_WAIT,oCtx)
  42. Set objRegistry = oSvc.Get("StdRegProv")
  43. If Err.Number = 0 Then
  44. ConnectProvider32 = True
  45. End If
  46. End Function
  47. ' Connect to the reg provider for this registy object
  48. Public Function ConnectProvider64( sComputerName )
  49. ConnectProvider64 = False
  50. Set objRegistry = Nothing
  51. On Error Resume Next
  52. Dim oLoc : Set oLoc = CreateObject("Wbemscripting.SWbemLocator")
  53. Dim oCtx : Set oCtx = CreateObject("WbemScripting.SWbemNamedValueSet")
  54. ' Force 64 Bit Registry
  55. Call oCtx.Add("__ProviderArchitecture", 64 )
  56. Call oCtx.Add("__RequiredArchitecture", True)
  57. Dim oSvc : Set oSvc = oLoc.ConnectServer(sComputerName,"root\default","","",,,WBEM_MAX_WAIT,oCtx)
  58. Set objRegistry = oSvc.Get("StdRegProv")
  59. If Err.Number = 0 Then
  60. ConnectProvider64 = True
  61. End If
  62. End Function
  63. Public Function IsValid()
  64. IsValid = Eval( Not objRegistry Is Nothing )
  65. End Function
  66. ' Used to read values from the registry, Returns 0 for success, all else is error
  67. ' ByRef data contains the registry value if the functions returns success
  68. ' The constants can be used for the sRootKey value:
  69. ' HKEY_LOCAL_MACHINE
  70. ' HKEY_CURRENT_USER
  71. ' HKEY_CLASSES_ROOT
  72. ' HKEY_USERS
  73. ' HKEY_CURRENT_CONFIG
  74. ' HKEY_DYN_DATA
  75. ' The constants can be used for the sType value:
  76. ' REG_SZ
  77. ' REG_MULTI_SZ
  78. ' REG_EXPAND_SZ
  79. ' REG_BINARY
  80. ' REG_DWORD
  81. Public Function ReadValue(ByVal hkRoot , ByVal nType , ByVal sKeyPath, ByVal sValueName , ByRef Data)
  82. On Error Resume Next
  83. ReadValue = -1
  84. Dim bReturn, Results
  85. If hkRoot = HKEY_LOCAL_MACHINE Or hkRoot = HKEY_CURRENT_USER Or hkRoot = HKEY_CLASSES_ROOT Or hkRoot = HKEY_USERS Or hkRoot = HKEY_CURRENT_CONFIG Or hkRoot = HKEY_DYN_DATA Then
  86. 'Read Value
  87. Select Case nType
  88. Case REG_SZ
  89. ReadValue = objRegistry.GetStringValue(hkRoot,sKeyPath,sValueName,Data)
  90. Case REG_MULTI_SZ
  91. ReadValue = objRegistry.GetMultiStringValue(hkRoot,sKeyPath,sValueName,Data)
  92. Case REG_EXPAND_SZ
  93. ReadValue = objRegistry.GetExpandedStringValue(hkRoot,sKeyPath,sValueName,Data)
  94. Case REG_BINARY
  95. ReadValue = objRegistry.GetBinaryValue(hkRoot,sKeyPath,sValueName,Data)
  96. Case REG_DWORD
  97. ReadValue = objRegistry.GetDWORDValue(hkRoot,sKeyPath,sValueName,Data)
  98. End Select
  99. End If
  100. End Function
  101. ' Used to write registry values, returns 0 for success, all else is falure
  102. '
  103. ' The constants can be used for the hkRoot value:
  104. ' HKEY_LOCAL_MACHINE
  105. ' HKEY_CURRENT_USER
  106. ' HKEY_CLASSES_ROOT
  107. ' HKEY_USERS
  108. ' HKEY_CURRENT_CONFIG
  109. ' HKEY_DYN_DATA
  110. ' The constants can be used for the nType value:
  111. ' REG_SZ
  112. ' REG_MULTI_SZ
  113. ' REG_EXPAND_SZ
  114. ' REG_BINARY
  115. ' REG_DWORD
  116. Function WriteValue( ByVal hkRoot , ByVal nType , ByVal sKeyPath, ByVal sValueName , ByVal Data)
  117. On Error Resume Next
  118. WriteValue = -1 'Default error
  119. If hkRoot = HKEY_LOCAL_MACHINE Or hkRoot = HKEY_CURRENT_USER Or hkRoot = HKEY_CLASSES_ROOT Or hkRoot = HKEY_USERS Or hkRoot = HKEY_CURRENT_CONFIG Or hkRoot = HKEY_DYN_DATA Then
  120. Call objRegistry.CreateKey( hkRoot , sKeyPath ) 'Create the key if not existing...
  121. 'Read Value
  122. Select Case nType
  123. Case REG_SZ
  124. WriteValue = objRegistry.SetStringValue(hkRoot,sKeyPath,sValueName,Data)
  125. Case REG_MULTI_SZ
  126. WriteValue = objRegistry.SetMultiStringValue(hkRoot,sKeyPath,sValueName,Data)
  127. Case REG_EXPAND_SZ
  128. WriteValue = objRegistry.SetExpandedStringValue(hkRoot,sKeyPath,sValueName,Data)
  129. Case REG_BINARY
  130. WriteValue = objRegistry.SetBinaryValue(hkRoot,sKeyPath,sValueName,Data)
  131. Case REG_DWORD
  132. WriteValue = objRegistry.SetDWORDValue(hkRoot,sKeyPath,sValueName,Data)
  133. End Select
  134. End If
  135. End Function
  136. Function DeleteValue( ByVal hkRoot , ByVal sKeyPath , ByVal sValueName )
  137. On Error Resume Next
  138. DeleteValue = -1 'Default error
  139. If hkRoot = HKEY_LOCAL_MACHINE Or hkRoot = HKEY_CURRENT_USER Or hkRoot = HKEY_CLASSES_ROOT Or hkRoot = HKEY_USERS Or hkRoot = HKEY_CURRENT_CONFIG Or hkRoot = HKEY_DYN_DATA Then
  140. DeleteValue = objRegistry.DeleteValue( hkRoot , sKeyPath , sValueName )
  141. End If
  142. End Function
  143. Public Function DeleteKey( hkRoot , ByVal sKeyPath )
  144. DeleteKey = -1
  145. On Error Resume Next
  146. If hkRoot = HKEY_LOCAL_MACHINE Or hkRoot = HKEY_CURRENT_USER Or hkRoot = HKEY_CLASSES_ROOT Or hkRoot = HKEY_USERS Or hkRoot = HKEY_CURRENT_CONFIG Or hkRoot = HKEY_DYN_DATA Then
  147. Dim arrSubKeys
  148. Dim sSubKey
  149. Call objRegistry.EnumKey( hkRoot, sKeyPath, arrSubkeys )
  150. If IsArray(arrSubkeys) Then
  151. For Each sSubKey In arrSubkeys
  152. Call DeleteKey( hkRoot, sKeyPath & "\" & sSubKey , bForce)
  153. Next
  154. End If
  155. DeleteKey = objRegistry.DeleteKey( hkRoot, sKeyPath )
  156. End If
  157. End Function
  158. ' Members Variables
  159. Private objRegistry
  160. End Class
  161. Dim str
  162. Dim r : Set r = New std_registry
  163. If r.ConnectProvider32( "." ) Then
  164. If r.ReadValue( HKEY_LOCAL_MACHINE , REG_EXPAND_SZ , "SYSTEM\CurrentControlSet\Control\Session Manager\Environment" , "ComSpec" , str )=0 Then
  165. Wsh.echo str
  166. Else
  167. Wsh.echo str
  168. End If
  169. End If
复制代码

转自:http://www.jb51.net/article/27134.htm

TOP

这有什么作用??

TOP

返回列表