user2130499
user2130499

Reputation: 25

Writng a DWORD registry key not working in vb script

im using the functions below to read and write to a 64bit registry from a 32bit scripting host.

It works fine reading & writing strings, but when I try and use a DWORD it fails

This works as a STRING

strResult = WriteRegStr (Write_REG_SZ, HKEY_LOCAL_MACHINE, "Software\_TEST", "SubKey1", "1", 64)

But not as a DWORD, error is VBScript runtime error: Object doesn't suport this property or method: 'oInParams.sValue'

 strResult = WriteRegStr (Write_REG_DWORD, HKEY_LOCAL_MACHINE, "Software\_TEST", "SubKey1", 1, 64)

Appreciate any help

'---------------------------------------------------
' 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

Upvotes: 2

Views: 1259

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200293

That's because the property for DWORD values is uValue, not sValue.

You could handle that with a Select Case statement:

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

Note that you need the same for handling the data returned in oOutParam in the ReadRegStr function.

But frankly, in my opinion this attempt to build an abstraction for registry access is misguided, and I'd recommend to stick with the regular WMI methods. An abstraction would only be helpful if you wouldn't need to know the type of the value you're trying to read or write.

Upvotes: 1

Related Questions