编写DWORD注册表项不在vbscript中工作

时间:2015-01-19 04:24:18

标签: vbscript scripting registry

我使用以下函数从32位脚本主机读取和写入64位注册表。

它的阅读效果很好写字符串,但当我尝试使用DWORD时它失败

这可以作为STRING

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

1 个答案:

答案 0 :(得分:1)

那是因为DWORD值的属性是uValue,而不是sValue

您可以使用Select Case语句处理:

Select Case Method
  Case "SetDWORDValue"  : oInParams.uValue = Value
  Case "SetStringValue" : oInParams.sValue = Value
End Select

请注意,处理oOutParam函数ReadRegStr中返回的数据时,您需要相同的内容。

但坦率地说,在我看来,构建注册表访问抽象的尝试是错误的,我建议坚持使用常规的WMI方法。只有在您不需要知道您尝试阅读或写入的值的类型时,抽象才会有所帮助。

相关问题