编写 DWORD 注册表项在 vb 脚本中不起作用
Writng a DWORD registry key not working in vb script
我正在使用以下函数从 32 位脚本主机读取和写入 64 位注册表。
它可以很好地读取和写入字符串,但是当我尝试使用 DWORD 时它失败了
这作为一个字符串
strResult = WriteRegStr (Write_REG_SZ, HKEY_LOCAL_MACHINE, "Software\_TEST", "SubKey1", "1", 64)
但不是 DWORD,错误是 VBScript 运行时错误:对象不支持此 属性 或方法:'oInParams.sValue'
strResult = WriteRegStr (Write_REG_DWORD, HKEY_LOCAL_MACHINE, "Software\_TEST", "SubKey1", 1, 64)
感谢任何帮助
'---------------------------------------------------
' Declared Constants
'---------------------------------------------------
Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20
Const HKEY_LOCAL_MACHINE = &H80000002
Const Read_REG_SZ = "GetStringValue"
Const Write_REG_SZ = "SetStringValue"
Const Read_REG_DWORD = "GetDWORDValue"
Const Write_REG_DWORD = "SetDWORDValue"
Const Success = 0
Const Failure = 1
'---------------------------------------------------
' Function Read Registry String
'---------------------------------------------------
Function ReadRegStr (Method, RootKey, Key, Value, RegType)
Dim oCtx, oLocator, oReg, oInParams, oOutParams
Set oCtx = CreateObject("WbemScripting.SWbemNamedValueSet")
oCtx.Add "__ProviderArchitecture", RegType
Set oLocator = CreateObject("Wbemscripting.SWbemLocator")
Set oReg = oLocator.ConnectServer("", "root\default", "", "", , , , oCtx).Get("StdRegProv")
Set oInParams = oReg.Methods_(Method).InParameters
oInParams.hDefKey = RootKey
oInParams.sSubKeyName = Key
oInParams.sValueName = Value
Set oOutParams = oReg.ExecMethod_(Method, oInParams, , oCtx)
Select Case Method
Case "GetDWORDValue" : ReadRegStr = oOutParams.uValue
Case "GetStringValue" : ReadRegStr = oOutParams.sValue
End Select
'ReadRegStr = oOutParams.sValue
set oCtx = Nothing
set oLocator = Nothing
End Function
'---------------------------------------------------
' Function Write Registry String
'---------------------------------------------------
Function WriteRegStr (Method, RootKey, Key, ValueName, Value, RegType)
Dim oCtx, oLocator, oReg, oInParams, oOutParams
Set oCtx = CreateObject("WbemScripting.SWbemNamedValueSet")
oCtx.Add "__ProviderArchitecture", RegType
Set oLocator = CreateObject("Wbemscripting.SWbemLocator")
Set oReg = oLocator.ConnectServer("", "root\default", "", "", , , , oCtx).Get("StdRegProv")
Set oInParams = oReg.Methods_(Method).InParameters
oInParams.hDefKey = RootKey
oInParams.sSubKeyName = Key
oInParams.sValueName = ValueName
oInParams.sValue = Value
Set oOutParams = oReg.ExecMethod_(Method, oInParams, , oCtx)
WriteRegStr = oOutParams.ReturnValue
Set oCtx = Nothing
Set oLocator = Nothing
End Function
那是因为 DWORD 值的 属性 是 uValue
,而不是 sValue
。
您可以使用 Select Case
语句来处理:
Select Case Method
Case "SetDWORDValue" : oInParams.uValue = Value
Case "SetStringValue" : oInParams.sValue = Value
End Select
请注意,在 ReadRegStr
函数中处理 oOutParam
中返回的数据需要相同的方法。
但坦率地说,在我看来,这种为注册表访问构建抽象的尝试是错误的,我建议坚持使用常规的 WMI 方法。只有在您不需要知道您尝试读取或写入的值的类型时,抽象才有用。
我正在使用以下函数从 32 位脚本主机读取和写入 64 位注册表。
它可以很好地读取和写入字符串,但是当我尝试使用 DWORD 时它失败了
这作为一个字符串
strResult = WriteRegStr (Write_REG_SZ, HKEY_LOCAL_MACHINE, "Software\_TEST", "SubKey1", "1", 64)
但不是 DWORD,错误是 VBScript 运行时错误:对象不支持此 属性 或方法:'oInParams.sValue'
strResult = WriteRegStr (Write_REG_DWORD, HKEY_LOCAL_MACHINE, "Software\_TEST", "SubKey1", 1, 64)
感谢任何帮助
'---------------------------------------------------
' Declared Constants
'---------------------------------------------------
Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20
Const HKEY_LOCAL_MACHINE = &H80000002
Const Read_REG_SZ = "GetStringValue"
Const Write_REG_SZ = "SetStringValue"
Const Read_REG_DWORD = "GetDWORDValue"
Const Write_REG_DWORD = "SetDWORDValue"
Const Success = 0
Const Failure = 1
'---------------------------------------------------
' Function Read Registry String
'---------------------------------------------------
Function ReadRegStr (Method, RootKey, Key, Value, RegType)
Dim oCtx, oLocator, oReg, oInParams, oOutParams
Set oCtx = CreateObject("WbemScripting.SWbemNamedValueSet")
oCtx.Add "__ProviderArchitecture", RegType
Set oLocator = CreateObject("Wbemscripting.SWbemLocator")
Set oReg = oLocator.ConnectServer("", "root\default", "", "", , , , oCtx).Get("StdRegProv")
Set oInParams = oReg.Methods_(Method).InParameters
oInParams.hDefKey = RootKey
oInParams.sSubKeyName = Key
oInParams.sValueName = Value
Set oOutParams = oReg.ExecMethod_(Method, oInParams, , oCtx)
Select Case Method
Case "GetDWORDValue" : ReadRegStr = oOutParams.uValue
Case "GetStringValue" : ReadRegStr = oOutParams.sValue
End Select
'ReadRegStr = oOutParams.sValue
set oCtx = Nothing
set oLocator = Nothing
End Function
'---------------------------------------------------
' Function Write Registry String
'---------------------------------------------------
Function WriteRegStr (Method, RootKey, Key, ValueName, Value, RegType)
Dim oCtx, oLocator, oReg, oInParams, oOutParams
Set oCtx = CreateObject("WbemScripting.SWbemNamedValueSet")
oCtx.Add "__ProviderArchitecture", RegType
Set oLocator = CreateObject("Wbemscripting.SWbemLocator")
Set oReg = oLocator.ConnectServer("", "root\default", "", "", , , , oCtx).Get("StdRegProv")
Set oInParams = oReg.Methods_(Method).InParameters
oInParams.hDefKey = RootKey
oInParams.sSubKeyName = Key
oInParams.sValueName = ValueName
oInParams.sValue = Value
Set oOutParams = oReg.ExecMethod_(Method, oInParams, , oCtx)
WriteRegStr = oOutParams.ReturnValue
Set oCtx = Nothing
Set oLocator = Nothing
End Function
那是因为 DWORD 值的 属性 是 uValue
,而不是 sValue
。
您可以使用 Select Case
语句来处理:
Select Case Method
Case "SetDWORDValue" : oInParams.uValue = Value
Case "SetStringValue" : oInParams.sValue = Value
End Select
请注意,在 ReadRegStr
函数中处理 oOutParam
中返回的数据需要相同的方法。
但坦率地说,在我看来,这种为注册表访问构建抽象的尝试是错误的,我建议坚持使用常规的 WMI 方法。只有在您不需要知道您尝试读取或写入的值的类型时,抽象才有用。