2014-12-02 56 views
0

我试图从注册表中检索代理覆盖列表,以便我可以从我的主机文件添加本地条目到它。获取,然后在注册表中使用VBS设置值

要做到这一点,我使用LocalClass_StdRegProv.GetStringValue(path, key),但我似乎无法得到正确的值。

下面的代码yeilds没有错误,但override的值只是0

我在做什么错?

然后,脚本告诉我,将值设回注册表时出现错误,如果它试图将字符串值设置为整数,可能会有所期待,但如果我也做错了我会把头给起来。谢谢。

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

编辑 - 新增HKEY_CURRENT_USER的失踪宣告。

+0

我没有看到你的代码'HKEY_CURRENT_USER'的定义。 – 2014-12-02 12:21:42

+0

好地方。它确实存在(现在已经添加),但上面的代码被删节了,我意外地删除了这个定义。 – 2014-12-02 12:49:28

回答

2

override的值始终为0,因为您错误地致电GetStringValue。此:

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

应该是这样的:

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

即第四参数是out参数如docs说明。从GetStringValue返回值实际上表明成功/失败

其余的看起来不错,在result0如果SetStringValue进行写操作成功

+0

谢谢你,先生,这是工作。令我惭愧的是,我实际上审阅了文档,但仍然没有意识到我做了什么! – 2014-12-04 12:01:56