首页 > 解决方案 > 当脚本使用 32 位 wscript/cscript 运行时,从注册表中获取 64 位软件的安装位置

问题描述

我的系统上分别安装了 64 位和 32 位C:\Program Files\Java\jre1.8.0_191java 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

我的观察:

  1. 在将 strKey 值保持为SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\并使用 64 位 wscript 运行上述脚本时,我得到预期的输出为C:\Program Files\Java\jre1.8.0_191.
  2. 在将 strKey 值更改为SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\并使用 64 位 wscript 运行脚本时,我得到预期的输出为C:\Program Files (x86)\Java\jre1.8.0_191

  3. 在将 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 位软件的安装位置?

标签: vbscriptwmi

解决方案


当脚本在 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

参考SWbemNamedValueSetSWbemNamedValue


推荐阅读