Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create custom actions in c# and bind it on a wix setup project

How do I create custom actions and link it to my WiX setup project?

I have:

  • WiX 3.11
  • Visual Studio
like image 506
SomePerson Avatar asked Jan 11 '19 10:01

SomePerson


People also ask

What are custom actions?

Custom actions are actions entirely defined by the user. They can be executable files, dynamic linked libraries, Visual Basic scripts or JavaScript files. They can be scheduled at any time during the installation.

How do I create a custom action in MSI?

To add a custom action: 1. In the View List under Behavior and Logic, click Custom Actions and Sequences (in Basic MSI, InstallScript MSI, MSI Database, and Transform projects) or Custom Actions (in DIM, Merge Module, and MSM Database projects).

Where do custom actions execute?

Because a custom action can be scheduled in both the UI and execute sequence tables, and can be executed either in the service or client process, a custom action can potentially execute multiple times. Note that the installer: Executes actions in a sequence table immediately by default.


1 Answers

You need to create a new C# Custom Action Project for WiX v3 in your solution.
That should look like this:

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Deployment.WindowsInstaller;

namespace CustomAction
{
    public class CustomActions
    {
        [CustomAction]
        public static ActionResult CustomAction(Session session)
        {
            session.Log("Begin CustomAction");

            return ActionResult.Success;
        }
    }
}

Change the function name to a name that fits your function.
After that right click on the References Folder on your WiX setup project and choose Add Reference... .
Click the tab Projects and choose your custom action Project.

For the last step you need to add this code to your Product.wxs:

<Binary Id="CustomActionBinary" SourceFile="$(var.CUSTOMACTIONSNAME.TargetDir)$(var.CUSTOMACTIONSNAME.TargetName).CA.dll" />
<CustomAction Id="CUSTOMACTIONAME" Impersonate="no" BinaryKey="CustomActionBinary" DllEntry="CUSTOMACTIONFUNCTION" Return="check" />

You only need to change a few names here:

  • CUSTOMACTIONSNAME = The name of the custom action that was added in the references folder (Default is "CustomActions")
  • CUSTOMACTIONNAME = Choose a name for your custom action like "CreateConfig".
  • CUSTOMACTIONFUNCTION = The name of the function in your custom action project you want to call.

Thats it.

If you now want to call the custom action in your setup project, you only need to create a "Custom" element with an Action attribute with your custom action id as value like this:

<Custom Action="CreateConfig" ... />

You can insert the custom action into the UI sequence or the install sequence as follows:

<!--User Interface Sequence-->
<InstallUISequence>
    <Custom Action='CustomAction1' Before='ExecuteAction' />
</InstallUISequence>

<!--Installation Sequence-->
<InstallExecuteSequence>
    <Custom Action='CustomAction1' After='InstallInitialize'>NOT Installed</Custom>
</InstallExecuteSequence>

You can also call a custom action from an event in a dialog box (snippet only - somewhat involved):

 <...>

 <Control Id="Next" Type="PushButton" X="236" Y="243" Width="56" Height="17" Default="yes" Text="&amp;Next">

   <Publish Event="DoAction" Value="CustomAction1">1</Publish>

 <...>
like image 51
SomePerson Avatar answered Oct 12 '22 12:10

SomePerson