kampi
kampi

Reputation: 2484

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!

Upvotes: 1

Views: 10510

Answers (2)

Safwan
Safwan

Reputation: 360

I wanted to post this as a comment for the answer provided by MBu but it is too long.

Just to clarify, this is not an issue with VBScript having a problem with getting an array of arrValueNames when there is only one value. It is actually how StdRegProv works (unfortunately). If you look at the documentation of the EnumValues method of the StdRegProv, you will understand why this is happening, below is an extract explaining this behavior:

sNames [out]

An array of named value strings. The elements of this array correspond directly to the elements of the Types parameter. Returns null if only the default value is available.

sNames [out] being arrValueNames in the code. Below is the link for the EnumValues method: https://learn.microsoft.com/en-us/previous-versions/windows/desktop/regprov/enumvalues-method-in-class-stdregprov

Upvotes: 1

MBu
MBu

Reputation: 2950

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.

Upvotes: 1

Related Questions