Uninstall previous version of MSI befor installing a later one

Aug 30, 2016 at 11:08 AM
Hi! I am using WIX# to build MSI for my Client. There is a requirement from Client that the MSI installed earlier should be uninstalled automatically while installation of a fresher MSI file i.e. the installed MSI should be uninstalled first programmatically then the newer MSI will be installed.

I am trying do this using the following method:-
        private static bool uninstallProgram(string ProgramName)
{
            try
            {
                ManagementObjectSearcher mos = new ManagementObjectSearcher(
                  "SELECT * FROM Win32_Product WHERE Name = '" + ProgramName + "'");
                foreach (ManagementObject mo in mos.Get())
                {
                    try
                    {
                        if (mo["Name"].ToString() == ProgramName)
                        {
                            object hr = mo.InvokeMethod("Uninstall", null);
                            return (bool)hr;
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(string.Format("{0}- Exception Occurred: {1}", DateTime.Now, ex.Message + Environment.NewLine + ex.StackTrace + Environment.NewLine + ex.Source));
                        //this program may not have a name property, so an exception will be thrown
                    }
                }

                //was not found...
                return false;

            }
            catch (Exception ex)
            {
                Console.WriteLine(string.Format("{0}- Exception Occurred: {1}", DateTime.Now, ex.Message + Environment.NewLine + ex.StackTrace + Environment.NewLine + ex.Source));
                return false;
            }
        }
But this method is not working the Control Flow hangs up in the line:-
object hr = mo.InvokeMethod("Uninstall", null);

Is there any better approach from WIX# perspective to do this. Please sugges. Thanks in advance.
Coordinator
Aug 31, 2016 at 6:43 AM
Of course it's hard to comment but because ManagementObjectSearcher is using COM interface under the hood. And there can be many reasons for a failure.

Let's assume that it is a problem with mo.InvokeMethod("Uninstall".... This can be fixed with something more conservative as this:
string productCode = mo["ProductIdentification"]; //not sure about exact name of the property
Process.Start("msiexec.exe", "/u "+productCode).WaitForExit();
If you want to hide the console window of msiexec.exe then you can adjust StartInfo to do so:
process.StartInfo.UseShellExecute = false;

Though quite possibly you don't have to go through this pain. If the product you are installing is the same product that you need to remove then you can find quite a few Upgrading samples in Wix# downloadables. This would be the best approach.

However if it is a different product then indeed you need to uninstall it manually. When doing so please be aware that if you are at active stage of installing the product you cannot do any other MSI activity (e.g. uninstall). Thus if it is in fact what you are doing in your code then your mo.InvokeMethod("Uninstall"... supposed to fail.

MSI only allows a single setup being active at the time. Though having setup GUI collecting user input is not part of this constrain. The "SimplifiedBootstrapper" sample exercises exactly this quality. It installs an additional dependency product from UI sequence before starting the primary installation. In your case you want to uninstall instead of install.

Though... "SimplifiedBootstrapper" is a lab effort to allow chained installs/uninstalls before support for a proper WiX bootstrapper became available. Thus because now Wix# is fully integrated with Burn (WiX bootstrapper building tool) you can evaluate if it can be used for the task. Typically botstrapper chains all required installs and all uninstalls. In your case you are doing a mixed operation uninstall-on-install so I am not sure how and if Burn supports it. You may need to do some research for this.

But... for something "quick and dirty", not as I am promoting it :), you may consider SimplifiedBootstrapper.
Sep 23, 2016 at 12:43 PM
Hi Oleg,

Thanks for your reply. I have managed to uninstall the previous version of MSI before installing the newer version by following the below steps.
  1. Removing the Website from IIS
  2. Stopping Window Service
  3. Uninstalling Window Service
  4. Removing the entry of MSI name from registry in order to remove it from the Control Panel
I am doing these steps in the event "static void Msi_BeforeInstall(SetupEventArgs e){} ". Then after these steps are complete the new MSI is getting installed. But after installation of the new MSI, the System/Server is rebooting automatically, though i have not written any code for this rebooting. Please suggest a work around. My client has done escalation for this. Thanks in advance.
Coordinator
Sep 24, 2016 at 1:11 AM
It is pure MSI problem. MSI may decide that reboot needed even without your consent.

Wix# Reboot sample shows the (commented) techniques of controlling implicit MSI rebooting policies. Most likely you will be able to address the issue with:
project.RebootSupressing = RebootSupressing.ReallySuppress;
Sep 26, 2016 at 7:06 AM
Hi Oleg,

Thanks for replying. You have referred the issue as MSI problem. Does that mean, the issue is in building the MSI using wix# due to some wrong processes i have followed or there is any issue with the MSI installer in the server?

Also, i have used "ManagedProject" for building the MSI, i could not find any property "project.RebootSupressing" in it. Please advise how to handle the issue. Thanks in advance.
Coordinator
Sep 26, 2016 at 1:21 PM
No. It's not a problem. I referred the symptoms you observe as an intended (while not intuitive) behavior of MSI. And this has nothing to do with Wix#. The best way to verify this is to check the generated by Wix# wxs file. You will see that there is no any reboot scheduling there. Reboot is somehow scheduled by MSI implicitly as the result of system runtime changes that require reboot (e.g. driver isntallation).

RebootSupressing is a special 'magic' property that MSI is using to allow finer reboot control. Wix# simply adds it the XML element and tat's it:
<Property Id="REBOOT" Value="ReallySuppress" />
> ...i could not find any property "project.RebootSupressing" in it. ...
Not sure what is happening in your environment as you can see it's there :)
Image