WiX Installer: getting version of the product being upgraded

2.6k Views Asked by At

During a major upgrade from version X to version Y, I need to have a property/variable stating that version X is being upgraded.

When performing a Major Upgrade of a product with a setup built by WiX Installer, is there a way to get the version number being upgraded?

2

There are 2 best solutions below

1
On BEST ANSWER

Assuming you're using a WiX majorupgrade element, when an upgrade is detected the value of the WIX_UPGRADE_DETECTED property is set to the productcode of the product being upgraded. You could pass that into the C++ Win32 API MsiGetProductInfo (.... INSTALLPROPERTY_VERSIONSTRING...) to get the version string. I think there's a DTF equivalent, ProductInstallation class that you construct with that productcode and then get the version. If you didn't use the majorupgrade element there is still a property like PREVIOUSVERSIONSINSTALLED that will get set to the productcode, so it depends on exactly how you've set it up. Note that strictly speaking you could be upgrading more than one product. This would be done after FindRelatedProducts. Anyway, that's what I'd look at.

I don't think there's a built-in way to get the version. You'd need to know that the upgrade is actually going to work first, so you'd need the property that is set by the FindRelatedProducts upgrade search. There are other things you could do, but I don't know if they would work in your situation. For example if you were launching the MSI from an external program it could enumerate related products to the MSI's productcode, get the version and pass it as a property on the command line to reduce what the MSI would need to do, assuming the upgrade is going to work, but that may be useless in your situation.

3
On

I have never tried, but you might be able to use the UPGRADINGPRODUCTCODE property to retrieve the product code for the product being uninstalled. As far as I know it is set to the list of product codes identified as "to be removed" by the Upgrade table (there may be several products to uninstall).

Then you can use a simple VBScript to retrieve the version on the system for that product code. The script below has a hard coded product code to find Windows Movie Maker. Update with your own Product GUID. It also features a Main function that is called. This is to enable the script to be run both interactively and via a method call inside an MSI. The script will take some time to complete:

Call Main

Sub Main

 strComputer = "."

 Set objWMIService = GetObject("winmgmts:" & _
  "{impersonationLevel=impersonate}!\\" & _
  strComputer & _
  "\root\cimv2")

 ' Select all product details for software matching input GUID
 Set colSoftware = objWMIService.ExecQuery _
     ("SELECT * FROM Win32_Product WHERE IdentifyingNumber=" & _
     "'" & "{B862B671-59FD-7457-AFA0-C738FB7ABD60}" & "'")

 ' For completeness allow multiple results, enumerate
 If colSoftware.Count > 0 Then
    For Each objSoftware in colSoftware
      MsgBox "Product Name: " & objSoftware.Name & vbCrLf & _
      "Product Code: " & objSoftware.IdentifyingNumber & vbCrLf & _
      "Package Code: " & objSoftware.PackageCode & vbCrLf & _
      "Local Package: " & objSoftware.LocalPackage & vbCrLf & _
      "Version: " & objSoftware.Version, vbInformation, _
      "Product Info"
    Next
  Else
    MsgBox "No product found."
 End If

End Sub