Why does VBScript return a different value for the %ProgramFiles% system variable in an HTA file vs in a VBS file?

StackOverflow https://stackoverflow.com/questions/19658387

  •  01-07-2022
  •  | 
  •  

On a 64 bit PC, the following code in a VBS file will return "C:\Program Files" as expected.

Dim oShell
Set oShell = CreateObject("WScript.Shell")
MsgBox oShell.ExpandEnvironmentStrings("%ProgramFiles%")

However, when the same code is placed inside an HTA file, it returns "C:\Program Files (x86)". The following is the HTA file.

<html>
<head>
    <title>HTA Test </title>
    <HTA:APPLICATION
        ApplicationName = "Test App"
        ID      = "test"
        WindowState = "normal "
        SysMenu = "yes "
        Scroll      = "no"
        Version = "1.0" />
</head>

<script language="VBScript">
    Dim oShell
    Set oShell = CreateObject("WScript.Shell")
    MsgBox oShell.ExpandEnvironmentStrings("%ProgramFiles%")
</script>
<body>
</body>
</html>

On the PC, the following 2 system variable exist.

ProgramFiles=C:\Program Files
ProgramFiles(x86)=C:\Program Files (x86)

Can anyone explain why this is happening?

Thanks.

有帮助吗?

解决方案

If you see HKEY_CLASSES_ROOT\htafile\Shell\Open\Command (which is used to open hta files) will see the OS is by default calling the 32bit version of MSHTA.exe, so, for a 32bit file, %programfiles% returns the value of %programfiles(x86)%

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top