Using a WiX custom action to set a property's value

14,833

Solution 1

After doing some more research into custom actions, I believe I've got all of this figured out. I added a <Binary> tag to the .wxs file to identify where the custom action resides. I then referenced the Binary tag's ID in the CustomAction. Finally, I added a Custom tag to the InstallExecuteSequence section that referenced the CustomAction tag by ID.

The final Custom tag mentioned above needs to go into the InstallUISequence section, not the InstallExecuteSequence section, as the custom action needs to be called before the dialog is displayed.

As for the implementation of the Custom Action itself, I added a new C# Custom Action library project to the solution. In there, I implemented a method, decorated with the [CustomAction] attribute. This method uses the values of properties stored in the Session object passed as a parameter to the method and determines the path of the current version's executable file. It then does the work needed to locate the values in the program's Configuration file that need to be preserved across versions and writes them to other properties for the upgrade script.

Solution 2

Example:

    [CustomAction]
    public static ActionResult SetProperty(Session session)
    {
        try
        {
            session.Log("Begin SetProperty action");

            session["PROPERTY_NAME"] = "value"


        }
        catch (Exception exception)
        {
            session.Log("ERROR in custom action SetProperty {0}", exception.ToString());

            return ActionResult.Failure;
        }

        return ActionResult.Success;
    }

Solution 3

Read the following sections of WiX tutorial:

  1. Extra Actions: gives an overview of how to add a Custom Action to MSI;
  2. What's Not in the Book: provides an example how to implement a Custom Action in DLL.
Share:
14,833

Related videos on Youtube

Tony Vitabile
Author by

Tony Vitabile

Updated on June 04, 2022

Comments

  • Tony Vitabile
    Tony Vitabile about 2 years

    I am modifying an existing WiX installer to handle updating an existing installation of one of our products. There are several values whose defaults are specified in properties. These properties are displayed to the user for editing and are then written to a custom configuration file by the existing installer.

    My code needs to be smart enough to detect if it is doing a brand new install versus installing an older version. If it is doing a brand new install, it needs to set the properties to default values. But if it is doing an upgrade, the code needs to retrieve the valus of those properties from the existing configuration file and display those to the user.

    From the reading I've done, it seems to me I need to use a type 51 custom action to set the properties. But how do I implement this custom action?

    I'm thinking that I have to first define the custom action to put it in the custom action table, and then I need to stick a tag somewhere to call it. And then I need to define it.

    How can I do this? What would some example code be?

  • Tony Vitabile
    Tony Vitabile almost 13 years
    That's exectly what I did & I came up with the solution I posted.
  • daniol
    daniol over 3 years
    How do you reference it in the wxs file?
  • sagar
    sagar about 3 years
    Is this to be put under installer class ?
  • Jaroslav Daníček
    Jaroslav Daníček over 2 years
    How did you assign the property, this is very vague answer of what you did and you haven't provided any code.