vbscript - 当脚本使用 32 位 wscript/cscript 运行时,从注册表中获取 64 位软件的安装位置
问题描述
我的系统上分别安装了 64 位和 32 位C:\Program Files\Java\jre1.8.0_191
java C:\Program Files (x86)\Java\jre1.8.0_191
。
我有以下代码可以获取系统上软件的安装位置。
MsgBox fn_getInstallLocation("java")
function fn_getInstallLocation(strApplication)
Dim strKey, objReg, subkey, arrKeys, strRequiredPath
Const HKLM = &H80000002 'HKEY_LOCAL_MACHINE
strRequiredPath = ""
strKey = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\" '<----THIS KEY IS IMPORTANT
set objReg = getObject("winmgmts://./root/default:StdRegProv")
objReg.enumKey HKLM, strKey, arrKeys
for each subkey in arrKeys
objReg.getStringValue HKLM, strKey&subkey, "InstallLocation", strInstallLocation
if InStr(1,strInstallLocation,strApplication,1)>0 Then
strRequiredPath = strInstallLocation
Exit for
End if
next
fn_getInstallLocation = strRequiredPath
End Function
我的观察:
- 在将 strKey 值保持为
SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\
并使用 64 位 wscript 运行上述脚本时,我得到预期的输出为C:\Program Files\Java\jre1.8.0_191
. 在将 strKey 值更改为
SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\
并使用 64 位 wscript 运行脚本时,我得到预期的输出为C:\Program Files (x86)\Java\jre1.8.0_191
在将 strKey 值保持为
SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\
ORSOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\
并使用 32 位 wscript 运行上述脚本时,我得到的输出为C:\Program Files (x86)\Java\jre1.8.0_191
(32 位 Java)。使用 32 位模式执行此脚本时,有没有办法获取 64 位 java 的安装位置?
我不是在玩不同的排列和组合。我问这个是因为当我在另一台机器上执行这个脚本时,它没有给我正确的结果。该机器的规格如下:
OS: Windows 7 x64
Java: 64 bit(Not 32 bit) - In Registry Editor, it is present in the key - SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\
The script ran in 32 bit mode(cannot switch to 64-bit on that machine because I am running this script on a tool - TestComplete which runs in 32-bit mode on all machines. If I do decide to run Test complete in 64-bit mode, there will be architecture mismatch issues with ODBC drivers.)
因为脚本是用 32 位 wscript 运行的,所以无法返回 64 位 JAVA 的安装路径。那么,有什么方法可以让这个解决方案在 32 位模式下运行并获取 64 位软件的安装位置?
解决方案
当脚本在 32 位主机上运行时,我能够获得 64 位应用程序的安装位置。
MsgBox fn_getInstallLocation("java")
function fn_getInstallLocation(strApplication)
Dim strKey, objReg, subkey, arrKeys, strRequiredPath
Const HKLM = &H80000002 'HKEY_LOCAL_MACHINE
strRequiredPath = ""
strKey = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\" '<----THIS KEY IS IMPORTANT
Set oWbm = CreateObject("WbemScripting.SWbemNamedValueSet")
oWbm.Add "__ProviderArchitecture", 64 '<----Change this parameter to 32 to get the install locations of 32-bit softwares
Set oLocator = CreateObject("Wbemscripting.SWbemLocator")
Set objReg = oLocator.ConnectServer("", "root\default", "", "", , , , oWbm).Get("StdRegProv")
objReg.enumKey HKLM, strKey, arrKeys
for each subkey in arrKeys
objReg.getStringValue HKLM, strKey&subkey, "InstallLocation", strInstallLocation
if InStr(1,strInstallLocation,strApplication,1)>0 Then
strRequiredPath = strInstallLocation
Exit for
End if
next
fn_getInstallLocation = strRequiredPath
End Function
尽管在 32 位脚本主机中运行,这为我提供了注册表编辑器中存在于路径中的密钥的安装位置,SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\
而不是来自路径的安装位置。SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\
ConnectServer
对象方法的参考SWbemLocator
。
推荐阅读
- c++ - `str.find_first_of("\t\n\v\f\r ")` 会在 unix 上工作吗?
- python-3.x - Pandas for Excel 和硒循环
- docker - 如何在本地 docker 环境中添加“证书”文件夹?
- volttron - VOLTTRON 是否支持 BACNET MS/TP?
- toit - Toit 中的 := 和 ::= 有什么区别?
- sql - 是否允许在 PostgreSQL 中编写 SQL 函数以返回一种以上的数据类型?
- webpack - 带有 Webpack 的 HTML 部分
- php - PHP foreach数组检查库存是否大于
- mongodb - 检查 mongoldb 中字符串的长度
- linux - 清除linux内核中的挂起中断