使用 vbscript 设置注册表 multiStringValue 时出现无效参数错误 [英] Invalid Parameter error when setting a registry multiStringValue using vbscript

查看:21
本文介绍了使用 vbscript 设置注册表 multiStringValue 时出现无效参数错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是参考我之前问过的现有问题,但在执行另一个子任务时,相同的条件不起作用.下面的所有变量都被正确定义为字符串.在此行上设置值时出现错误:

This is in reference to an existing question I previously asked but same conditions are not working when doing another sub. All variables below are defined correct and as strings. I am getting error when setting values on this line:

objReg.setMultiStringValue HKCU,IE_Main,mStrSecStartPages,allURLs

代码如下;

return = objReg.getMultiStringValue (HKCU,IE_Main,mStrSecStartPages,multiStringValues)
'If values found in Secondary Start Pages
If return=0 Then

    ReDim allURLs(0)
            'Read all values and only store non intranet values to array
    For Each itemname In multiStringValues
        If itemname <> strFunctionIntranet1 And itemname <> strFunctionIntranet2 And itemname <> strFunctionIntranet3 And itemname <> strFunctionIntranet4 Then
            ReDim Preserve allURLs(UBound(allURLs)+1)
            allURLs(UBound(allURLs)) = itemname
        End If
    Next
            'Remove current key holding existing values
    objReg.DeleteValue HKCU,IE_Main,mStrSecStartPages
            'Set new values based on values read and user's intranet

    if UBound(allURLs)>=0 Then
    wscript.echo "in setting"
        objReg.setMultiStringValue HKCU,IE_Main,mStrSecStartPages,allURLs
    End If
    wscript.echo "out setting"

End If

推荐答案

问题是即使 REG_MULTI_SZ 值中没有任何值,您仍然会得到一个空数组返回,这意味着当您循环遍历数组并使用

Problem is even if there isn't any values in the REG_MULTI_SZ value you will still get an empty Array returned, which means when you then loop through the array and dynamically expand it using

ReDim Preserve allURLs(UBound(allURLs)+1)

您将始终在数组中的第一个位置有一个空白元素,当传递给

You will always have a blank element in the first position in the array which when passed to

objReg.setMultiStringValue HKCU,IE_Main,mStrSecStartPages,allURLs

如果它不是您将获得的唯一元素

if it isn't the only element you will get

SWbemObjectEx:无效参数

SWbemObjectEx: Invalid parameter

这是我为证明这一点所做的一些测试

Here is some testing I did to prove this

Option Explicit
Const HKEY_LOCAL_MACHINE = &H80000002

Dim oReg
Dim strKeyPath, strValueName, arrStringValues
Dim strComputer: strComputer = "."

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

strKeyPath = "SOFTWARE\Microsoft\Internet Explorer\Main"
strValueName = "Default_Secondary_Page_URL"

Dim rtn
rtn = oReg.GetMultiStringValue(HKEY_LOCAL_MACHINE, strKeyPath, strValueName, arrStringValues)

Dim i
If rtn = 0 Then
  If IsArray(arrStringValues) Then
    For i = 0 To UBound(arrStringValues)
      WScript.Echo "arrStringValues(" & i & ") = " & arrStringValues(i)
    Next
  Else
    WScript.Echo "Not Array"
  End If
Else
  WScript.Echo "Failed to GetMultiStringValue - Return (" & rtn & ")"
End If

rtn = oReg.SetMultiStringValue(HKEY_LOCAL_MACHINE,strKeyPath,strValueName,arrStringValues)
WScript.Echo "SetMultiStringValue - Return (" & rtn & ")"

输出:

arrStringValues(0) =
SetMultiStringValue - Return (0)

添加以下行以在 IsArray() 检查下创建两个空白元素

Adding the following line to create two blank elements under the IsArray() check

ReDim Preserve arrStringValues(UBound(arrStringValues) + 1)

输出:

arrStringValues(0) =
arrStringValues(1) =
test36.vbs(31, 1) SWbemObjectEx: Invalid parameter

因此 SetMultiSringValue() 将接受一个包含空元素的数组,如果它是数组中的唯一元素,那么在您尝试添加更多元素的那一刻,您将收到如上所述的错误.

So SetMultiSringValue() will accept an Array that contains an empty element if it is the only element in the array, the minute you try to add more you will get the error as described above.

要停止在开始时创建额外的空白元素,您可以切换到使用 For 而不是 For Each 这样您就可以告诉循环只调用

To stop creating the extra blank element at the beginning you could switch to using a For instead of a For Each that way you can tell the loop to only call

ReDim Preserve allURLs(UBound(allURLs)+1)

当数组的索引大于0时

For i = 0 To UBound(multiStringValues)
  itemname = multiStringValues(i)
  If itemname <> strFunctionIntranet1 And itemname <> strFunctionIntranet2 And itemname <> strFunctionIntranet3 And itemname <> strFunctionIntranet4 Then
    'Only expand if we have more then 1 value in multiStringValues
    If i > 0 Then ReDim Preserve allURLs(UBound(allURLs)+1)
    allURLs(UBound(allURLs)) = itemname
  End If
Next

当然,您可以使用 For Each 来执行此操作,但是您必须使用另一个变量手动跟踪数组索引,在我看来,当您已经有了 For 时看起来毫无意义.

You can do this with a For Each of course but you would have to track the Array index manually using another variable, which in my opinion when you have For already seems pointless.

这篇关于使用 vbscript 设置注册表 multiStringValue 时出现无效参数错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆