makerofthings7
makerofthings7

Reputation: 61493

How do I cancel and rollback a custom action in VS2010 Windows Installer?

I have a custom action that adds/removes a certificate from the Trusted Root Certificates via the Windows Installer. I achieve this by using a CustomAction

It's possible that the user may not have permissions to add the certificate to TrustedRoots, or they may select "Cancel", how do I roll back the previous actions, and tell the installer that I've cancelled the process?

As it stands now the Windows Installer is always reporting a success response even if it fails.

Upvotes: 4

Views: 5012

Answers (3)

Scott Boettger
Scott Boettger

Reputation: 3667

You should set up your custom action to the a function with return type of ActionResult that way you can return the failure type if the cancel happens or another exception.

using Microsoft.Deployment.WindowsInstaller;
namespace CustomAction1
{
    public class CustomActions
    {
        [CustomAction]
        public static ActionResult ActionName(Session session)
        {
            try
            {
                session.Log("Custom Action beginning");

                // Do Stuff...
                if (cancel)
                {
                    session.Log("Custom Action cancelled");
                    return ActionResult.Failure;
                }

                session.Log("Custom Action completed successfully");
                return ActionResult.Success;
            }
            catch (SecurityException ex)
            {
                session.Log("Custom Action failed with following exception: " + ex.Message);
                return ActionResult.Failure;
            }
         }
    }
}

NOTE: This is a WIX compatible custom action. I find WIX to allow for more control over the MSI creation.

Upvotes: 6

Samich
Samich

Reputation: 30175

Try to throw an InstallException. In this case installer will detect thomething wrong with the installation and rollback actions.

   public override void Commit(IDictionary savedState)
   {
      base.Commit(savedState);
      Console.WriteLine("Commit ...");
      // Throw an error if a particular file doesn't exist.
      if(!File.Exists("FileDoesNotExist.txt"))
         throw new InstallException();
      // Perform the final installation if the file exists.
   }

Upvotes: 3

Cosmin
Cosmin

Reputation: 21436

This can be done only from win32 DLL or VBScript custom actions by returning 1602. If you are using an EXE or an installer class action, any non-zero return value will be treated as a failure.

Upvotes: 2

Related Questions