How do I read 64-bit Registry values from VBScript running as a an msi post-installation task?

前端 未结 4 1698
庸人自扰
庸人自扰 2020-12-29 14:39

I need to read the location of the Temporary ASP.NET Files folder from VBScript as part of a post-installation task in an installer created using a Visual Studio 2008 deploy

4条回答
  •  北荒
    北荒 (楼主)
    2020-12-29 15:10

    Not sure about launching the 64-bit script host version, but you should be able to access the 64-bit registry from the 32-bit script host using the WMI StdRegProv class, like this:

    Const HKEY_LOCAL_MACHINE = &H80000002
    sPath = ReadRegStr (HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\ASP.NET\2.0.50727.0", "Path", 64)
    WScript.Echo sPath
    
    ' Reads a REG_SZ value from the local computer's registry using WMI.
    ' Parameters:
    '   RootKey - The registry hive (see http://msdn.microsoft.com/en-us/library/aa390788(VS.85).aspx for a list of possible values).
    '   Key - The key that contains the desired value.
    '   Value - The value that you want to get.
    '   RegType - The registry bitness: 32 or 64.
    '
    Function ReadRegStr (RootKey, Key, Value, RegType)
        Dim oCtx, oLocator, oReg, oInParams, oOutParams
    
        Set oCtx = CreateObject("WbemScripting.SWbemNamedValueSet")
        oCtx.Add "__ProviderArchitecture", RegType
    
        Set oLocator = CreateObject("Wbemscripting.SWbemLocator")
        Set oReg = oLocator.ConnectServer("", "root\default", "", "", , , , oCtx).Get("StdRegProv")
    
        Set oInParams = oReg.Methods_("GetStringValue").InParameters
        oInParams.hDefKey = RootKey
        oInParams.sSubKeyName = Key
        oInParams.sValueName = Value
    
        Set oOutParams = oReg.ExecMethod_("GetStringValue", oInParams, , oCtx)
    
        ReadRegStr = oOutParams.sValue
    End Function
    

    NB: I'm under a 32-bit OS right now, so can't verify that this example works. Beware of bugs :-)

    See also the Requesting WMI Data on a 64-bit Platform MSDN article for more info on the subject.

提交回复
热议问题