David Gard
David Gard

Reputation: 12047

Get and then set a value in the registry using VBS

I'm trying to retrieve the proxy override list from the registry so that I can add local entries from my hosts file to it.

To do this I am using LocalClass_StdRegProv.GetStringValue(path, key), but I cannot seem to get the correct value.

The below code yeilds no errors, but the value of override is simply 0.

What am I doing wrong?

The script is then telling me there was an error with setting the value back to registry, which is somewhat to be expected if it's trying to set a string value to an integer, but if I'm doing that wrong as well I'd apprciate the heads up. Thanks.

'** Set the HKCU as a constant *'
Const HKEY_CURRENT_USER = &H80000001

Dim LocalClass_StdRegProv
Set LocalClass_StdRegProv = GetObject("winmgmts:{impersonationlevel=impersonate}!\\.\root\default:StdRegProv")

'** Set the proxy override list *'
Dim overrideList(1)
overrideList(0) = "local.latestwordpress.com"
overrideList(1) = "local.fileshare.com"

'** Set the proxy override string to include entries from localhost *'
Dim result, override
override = LocalClass_StdRegProv.GetStringValue(HKEY_CURRENT_USER, "Software\Microsoft\Windows\CurrentVersion\Internet Settings", "ProxyOverride")
override = updateOverride(override, overrideList)
result = LocalClass_StdRegProv.SetStringValue(HKEY_CURRENT_USER, "Software\Microsoft\Windows\CurrentVersion\Internet Settings", "ProxyOverride", override)
checkResult(result)

'** Reset the objects *'
Call resetObjects

'** Quit the script *'
WScript.Quit


'**
 ' Update the proxy override list (if required) to include any local sites
 '
 ' @param required string override      The proxy override string to check
 ' @param required array overrideList   A list of addresses to include in the proxy override list
 '*
Function updateOverride(override, overrideList)

    Dim item

    '** Loop through each item in the override list... *'
    For Each item In overrideList

        '** Check to see if the current item is missing from the proxy override string... *'
        If Not InStr(override, item) Then
            override = item & ";" & override    ' Add the current to the proxy override string
        End If

    Next

    updateOverride = override

End Function


'**
 ' Check the result of a Registry key edit to ensure that it was valid
 '
 ' @param required integer result   The result of the Registry key edit to check
 '*
Function checkResult(result)

    '** Check to see if there is a VBS error or if the regestry set failed *'
    If Err.Number <> 0 Or result <> 0 Then

        '** Display an error message to the user *'
        Dim message, title
        message = "An error occured while updating your proxy settings." & vbCrLf & vbCrlF & "In order to use the internet you must manually set your proxy settings via Internet Explorer."
        title = "Error setting proxy"
        MsgBox message, vbCritical, title

        '** Reset the objects *'
        Call resetObjects

        '** Quit the script *'
        WScript.Quit

    End If

End Function


'**
 ' Reset the objects that have been used in this script
 '*
Function resetObjects()

    Set LocalClass_StdRegProv = Nothing

End Function

Edit - Added missing declaration of HKEY_CURRENT_USER.

Upvotes: 1

Views: 1797

Answers (1)

Jobbo
Jobbo

Reputation: 1418

The value of override is always 0 because you're calling GetStringValue incorrectly. This:

override = LocalClass_StdRegProv.GetStringValue(HKEY_CURRENT_USER, "Software\Microsoft\Windows\CurrentVersion\Internet Settings", "ProxyOverride")

Should be this:

LocalClass_StdRegProv.GetStringValue HKEY_CURRENT_USER, "Software\Microsoft\Windows\CurrentVersion\Internet Settings", "ProxyOverride", override

i.e. the 4th parameter is an out parameter as stated in the docs. The return value from GetStringValue actually indicates success/failure

The rest looks ok, in that result will be 0 if the write performed by SetStringValue was successful

Upvotes: 3

Related Questions