Search code examples
windowsvbscript

How to read the (Default) value from registry in VBScript?


in the registry ther is a key, which has only value, the (Default) value. This default entry has a value what i need. I found a script to read the registry values.

const HKEY_LOCAL_MACHINE = &H80000002
const RegistryLocation   = "SOFTWARE\SAP BusinessObjects\Suite XI 4.0\Xcelsius\Keycodes"

ReadRegistry( RegistryLocation )

Function ReadRegistry( RegistryLocation )

strComputer = "."

Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")

oReg.EnumValues HKEY_LOCAL_MACHINE, RegistryLocation, arrValueNames, arrValueTypes

    Wscript.Echo "Key Name: " & arrValueNames(0)
'RegKeyName = arrValueNames(0)

oReg.GetStringValue HKEY_LOCAL_MACHINE, RegistryLocation, arrValueNames(0), strValue
Wscript.Echo "Value: " & strValue
'RegKeyValue = strValue

'ChangeRegistryValue RegistryLocation, arrValueNames(i), NewSerial

End Function 'ReadRegistry

It works fine IF there are more keys. If only the (Default) value exists, i get a type mismatch error. If i create a new key, then i can read the default entry value.

So my question is, what i am doing wrong, and how should i do it?

Thanks!


Solution

  • It seems that VBScript has problems with getting an array of ValueNames when there is only one value with empty name. You can read the default value without enumerating value names - just supply empty string for the value name:

    Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")
    oReg.GetStringValue HKEY_LOCAL_MACHINE, RegistryLocation, "", strValue
    Wscript.Echo "Value: " & strValue
    

    It works in both cases: when default value is the only value and when the are more values under the given key.