create different env. variable based on Windows version - Advanced Installer Community

We’re moving our forums to a new platform.

You can still browse and search existing topics, but you can no longer create new topics or post replies.

For questions, technical issues, or feature requests, please don’t hesitate to contact our support team at support@advancedinstaller.com.

Thank you for your understanding!

shelmers
Posts: 63
Joined: Thu Sep 03, 2009 9:02 pm
Location: Andover, Massachusetts USA

create different env. variable based on Windows version

My application requires one environment variable for Windows XP and a different one for Vista/Win 7. It's not the value of the env. variable that's different -- I could accomplish that with a conditioned property -- but the actual name of the variable. Am I correct that the variable name cannot contain a property?

If that's the case, can you recommend a technique to accomplish this? Because environment variables can't contain conditions directly, the only thing I've come up with so far is to install a dummy file and set a condition on that component. Is there a better way?

Thanks,
Scott
Cosmin
Posts: 5797
Joined: Tue Jul 10, 2007 6:39 pm
Contact: Website

Re: create different env. variable based on Windows version

Hi Scott,

Unfortunately the name of an environment variable cannot be set dynamically. An approach would be to use a dummy resource (file or registry entry) for each variable and condition their component installation. Another approach would be to use a custom action to create the variable you need. You can find some sample VBScript code here.

Regards,
Cosmin
Cosmin Pirvu - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube
shelmers
Posts: 63
Joined: Thu Sep 03, 2009 9:02 pm
Location: Andover, Massachusetts USA

Re: create different env. variable based on Windows version

Thanks for confirming my suspicions that I needed to do this in a roundabout way. I like the code approach rather than creating a dummy component. I also found a bit of VBscript for creating and removing environment variable that I like a little better than what you suggested -- primarily because it's a little bit simpler.

In case it's of use to anyone else I'll post the code for creating and removing the environment variables based on the version of Windows.
  • I used an Attached Custom Action, conditioned by (Not Installed), and inserted it under WriteEnvironmentStrings for the Create code.
  • I used an Attached Custom Action, conditioned by (REMOVE="ALL"), and inserted it under RemoveEnvironmentStrings for the Remove code.

Code: Select all

Option Explicit
' Creates either LSHOST or LSFORCEHOST based on operating system type
'
' Adapted from http://www.myitforum.com/articles/11/view.asp?id=1498

Dim objShell, EnvVar
Dim WinVersion : WinVersion  = session.property("VersionNT")

' Create an instance of the wshShell object
Set objShell = CreateObject("WScript.Shell")

' Use environment variable of type = system
Set EnvVar=objShell.Environment("System")

' Windows 2000 and Windows XP
If WinVersion < 600 Then
	EnvVar("LSHOST") = "no-net"
End If

' Windows Vista and Windows 7
If WinVersion >= 600 Then
	EnvVar("LSFORCEHOST") = "no-net"
End If

Code: Select all

Option Explicit
' Removes either LSHOST or LSFORCEHOST based on operating system type
'
' Adapted from http://www.myitforum.com/articles/11/view.asp?id=1498

Dim objShell, EnvVar
Dim WinVersion : WinVersion  = session.property("VersionNT")

' Create an instance of the wshShell object
Set objShell = CreateObject("WScript.Shell")

' Use environment variable of type = system
Set EnvVar=objShell.Environment("System")

' Windows 2000 and Windows XP
If WinVersion < 600 Then
	EnvVar.Remove ("LSHOST")
End If

' Windows Vista and Windows 7
If WinVersion >= 600 Then
	EnvVar.Remove ("LSFORCEHOST")
End If
shelmers
Posts: 63
Joined: Thu Sep 03, 2009 9:02 pm
Location: Andover, Massachusetts USA

Re: create different env. variable based on Windows version

It turns out that the above was a good theory, but Vista UAC rears its ugly head. The create script ran fine on XP but failed with a permission error on Vista.

Consequently, the custom action needs to run "Deferred with no impersonation". However, a custom action run in this mode doesn't have access to session properties so I had to rewrite the script to get the OS version from the WMI service.

The result is below. If there's an easier way to do any of this, please let me know!

Code: Select all

Dim strComputer  : strComputer       = "."
Dim objWMI       : Set objWMI        = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Dim colComputers : Set colComputers  = objWMI.ExecQuery("Select * from Win32_OperatingSystem")

Dim objComputer, WinVersion
Dim objShell, EnvVar

For Each objComputer In colComputers
	' version string includes subversions so extract first part only
	WinVersion = Left(objComputer.Version, InStr(objComputer.Version,".")-1)
Next

' Create an instance of the wshShell object
Set objShell = CreateObject("WScript.Shell")
' Use environment variable of type = system
Set EnvVar = objShell.Environment("System")

' Windows 2000 and Windows XP
If WinVersion < "6" Then
	EnvVar("LSHOST") = "no-net"
End If

' Windows Vista and Windows 7
If WinVersion >= "6" Then
	EnvVar("LSFORCEHOST") = "no-net"
End If
Scott

Return to “Common Problems”