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

Asked
Viewd28172

7

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 deployment project.

I thought I would do something like this:

Set oShell = CreateObject("Wscript.Shell")
strPath = oShell.RegRead("HKLM\SOFTWARE\Microsoft\ASP.NET.0.50727.0\Path")

and then concatenate strPath with "\Temporary ASP.NET Files" and be done with it.

On an x64 system, however, I am getting the value from the WOW6432Node (HKLM\SOFTWARE\Wow6432Node\Microsoft\ASP.NET\2.0.50727.0), which gives me the 32-bit framework path (C:\Windows\Microsoft.NET\Framework\v2.0.50727), but on an x64 system, I actually want the 64-bit path, i.e. C:\Windows\Microsoft.NET\Framework64\v2.0.50727.

I understand that this happens because the .vbs file is run using the 32-bit script host due to the parent process (the installer) being 32-bit itself.

How can I run the script using the 64-bit script host - or - how can I read the 64-bit values even if the script is run using the 32-bit script host?

4 ответов

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.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.

0

Я думаю что-то вроде этого, но я не разобрался, как обрабатывать выходные значения.

 strComputer = "."
Const HKLM = &h80000002
Set objCtx = CreateObject("WbemScripting.SWbemNamedValueSet")
objCtx.Add "__ProviderArchitecture", 64
objCtx.Add "__RequiredArchitecture", TRUE
Set objLocator = CreateObject("Wbemscripting.SWbemLocator")
Set objServices = objLocator.ConnectServer("","root\default","","",,,,objCtx)
Set objStdRegProv = objServices.Get("StdRegProv") 

' Use ExecMethod to call the GetStringValue method
Set Inparams = objStdRegProv.Methods_("EnumValues").Inparameters
Inparams.Hdefkey = HKLM
Inparams.Ssubkeyname = "SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL\"
'Inparams.Svaluename = "Logging"
set Outparams = objStdRegProv.ExecMethod_("EnumValues", Inparams,,objCtx)

'Show output parameters object and the registry value HKLM\SOFTWARE\
WScript.Echo Outparams.GetObjectText_
WScript.Echo "WMI Logging is set to  " & Outparams.SValue
 
1

Используя документированный подход Microsoft, ответ Хелен абсолютно правильный.

Однако, согласно моим собственным тестам, оказалось, что достаточно указать флаг контекста __ProviderArchitecture только во время установления соединения с поставщиком StdRegProv.
Это значительно упрощает работу, поскольку в отдельной функции необходимо инкапсулировать только настройку поставщика, в противном случае можно использовать обычный API.

 set reg64 = MakeRegLocator(64)
reg64.GetStringValue , "SOFTWARE\Microsoft\ASP.NET.0.50727.0", "Path", path

WScript.Echo path

' Establish a connection to the local 32 or 64 bit registry hive as requested.
' Parameters:
'   RegType - The registry bitness: 32 or 64.
function MakeRegLocator(bitness)
    set ctx = CreateObject("WbemScripting.SWbemNamedValueSet")
    ctx.Add "__ProviderArchitecture", bitness

    set locator = CreateObject("Wbemscripting.SWbemLocator")
    set services = locator.ConnectServer("", "root\default", "", "", , , , ctx)
    set reg = services.Get("StdRegProv")

    set MakeRegLocator = reg
end function
 
-2

Проверьте это:

Установить oShell = CreateObject ("Wscript.Shell") strPath = oShell.RegRead ("HKLM64 \ SOFTWARE \ Microsoft \ ASP.NET \ 2.0.50727.0 \ Путь")

https://www.autoitscript.com/autoit3/docs/functions/ RegRead.htm