代码之家  ›  专栏  ›  技术社区  ›  Gurmanjot Singh

使用32位wscript/cscript运行脚本时从注册表获取64位软件的安装位置

  •  0
  • Gurmanjot Singh  · 技术社区  · 7 年前

    我的系统上安装了64位和32位java C:\Program Files\Java\jre1.8.0_191 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. SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\ 使用64位wscript运行上面的脚本,得到预期的输出 .
    2. SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\ C:\ProgramFiles(x86)\Java\jre1.8.0\U 191

    3. 关于保持strKey值为 软件\Microsoft\Windows\CurrentVersion\Uninstall\ 或 软件\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\ 使用32位wscript运行上面的脚本,得到的输出如下 C:\ProgramFiles(x86)\Java\jre1.8.0\U 191 (32位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位软件的安装位置?

    1 回复  |  直到 7 年前
        1
  •  1
  •   Gurmanjot Singh    7 年前

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

    SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\ 而不是从小路上 SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\ 尽管在32位脚本主机中运行。

    ConnectServer 方法 SWbemLocator 对象

    参考 SWbemNamedValueSet SWbemNamedValue