Monday, January 31, 2011

Bugs that May Never be Fixed

I'm guessing that you are most likely familiar with Windows Environment variables, since most of the readers here are pretty savvy about Windows stuff.  So, you should be pretty familiar with how 64-bit Windows variables are spread out too…

%programfiles%

%programfiles(x86)%

…and so on.

Well, guess what?  VBscript (WSH), has a little bug regarding how it handles the [programfiles] variable on 64-bit Windows 7.  To demonstrate, try this…

  • Open a CMD console
  • Type SET and press Enter
  • Observe the values for [ProgramFiles], and [ProgramFiles(x86)]
  • Type echo %programfiles% and press Enter.  Observe the output.
  • Now run the following VBscript code and observe the output
Set objShell = CreateObject("Wscript.Shell")
wscript.echo objShell.ExpandEnvironmentStrings("%ProgramFiles%")



Notice anything odd?  While the echo statement returns "C:\Program Files", the script result returns "C:\Program Files (x86)", as will "%programfiles(x86)%".  So how do you get the 64-bit native app install path of "C:\Program Files"?  Well, using the ExpandEnvironmentStrings method - you don't.



And you might expect that you could rely upon the Environment property of the WshShell object, like this…




Set WshSysEnv = objShell.Environment("PROCESS")
wscript.Echo WshSysEnv("ProgramFiles")



But, sadly, it too returns "C:\Program Files (x86)"



And just when you thought that maybe something like KiXtart could ride up on a stallion wearing a shiney suit of armor and save the day with …




Break ON
? ExpandEnvironmentVars("%ProgramFiles%")


Well, think again.

There is one workaround, but it's not very "elegant" in terms of methodology (but it works, hey):




Set objShell = CreateObject("Wscript.Shell")
sysdrv = objShell.ExpandEnvironmentStrings("%SystemDrive%")
progfiles = sysdrv & "\Program Files"




The same approach works for KiXtart and others as well.

Conclusion



Given that Microsoft left VBscript at the wedding altar, sobbing and destitute, and ran off in a stolen convertible to Vegas with PowerShell, I wouldn't expect any changes to this behavior - ever.

Post a Comment