通过此处的示例,我创建了一个 vbscript,它使用 WMI 注册表对象来枚举注册表项“HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Components”上的子键”。当我使用 WScript 对其进行测试时,vbscript 运行良好并产生了我需要的结果。
当我将 vbscript 作为自定义操作嵌入到安装程序中时,EnumKey 调用返回错误 wbemErrNotFound(2)。但是,如果我枚举其他一些键,它可能会返回成功的结果。是权限问题吗?我尝试以提升的权限运行安装程序。如何让它与安装程序一起使用?
为了演示这个问题,下面是我正在使用的 vbscript 的精简版本:
Const HKEY_LOCAL_MACHINE = &H80000002
Const KEY_ENUMERATE_SUB_KEYS = &H00000008
Function CountSubKeys(nHiveRoot, sKeyPath)
Const sComputer = "." ' Use . for current machine
Dim nRet
Dim nSum
MsgBox "EnumKey: " & sKeyPath, vbOkOnly, "CountSubKeys"
' Set oReg = GetObject( "winmgmts:{impersonationLevel=impersonate}!//" & sComputer & "/root/default:StdRegProv" )
Set oReg = GetObject( "winmgmts:{impersonationLevel=impersonate}!//" & sComputer & "/root/cimv2:StdRegProv" )
Dim bGranted
nRet = oReg.CheckAccess(nHiveRoot, sKeyPath, KEY_ENUMERATE_SUB_KEYS, bGranted)
If (nRet = 0) Then
If bGranted = True Then
MsgBox "Access to key: " & sKeyPath & " is granted", vbOkOnly, "CountSubKeys"
Else
MsgBox "Access to key: " & sKeyPath & " is denied", vbOkOnly, "CountSubKeys"
End If
Else
MsgBox "Failed to check key access: " & sKeyPath & ", nRet: " & nRet, vbOkOnly, "CountSubKeys"
End If
nRet = oReg.EnumKey(nHiveRoot, sKeyPath, arrSubKeys)
If (nRet = 0) Then
If isArray(arrSubKeys) Then
nSum = UBound(arrSubKeys) + 1
MsgBox "Number of sub keys: " & nSum, vbOkOnly, "CountSubKeys"
Else
MsgBox "EnumKey return no sub key on path" & sKeyPath, vbOkOnly, "CountSubKeys"
nSum = 0
End If
Else
MsgBox "Failed to enum key: " & sKeyPath & ", Err: " & nRet, vbOkOnly, "CountSubKeys"
nSum = -1
End If
CountSubKeys = nSum
End Function
Sub TestEnumKey()
Const sInstalledComponentKey = "SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Components\"
nCount = CountSubKeys(HKEY_LOCAL_MACHINE, sInstalledComponentKey)
MsgBox "nCount: " & nCount, vbOkOnly, "TestEnumKey"
End Sub
TestEnumKey()
自定义操作使用 Sub TestEnumKey() 作为 vbscrpt 的入口点。