2014-12-02 64 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