Click here to Skip to main content
13,287,116 members (41,676 online)
Click here to Skip to main content
Add your own
alternative version


11 bookmarked
Posted 18 Dec 2012

Using WIX with Managed Custom Action

, 18 Dec 2012
Rate this:
Please Sign up or sign in to vote.
Using WIX with Managed Custom Action

WIX is a great toolset for creating installers. In most of the cases when you need an installer, you need some custom logic to be executed. It's great that WIX supports managed custom actions. Anyway, it wasn't so easy for me to make it work, so I want to share my experience. I will demonstrate it using the WIX Visual Studio add-in (version v3.7.1217.0).

We will create WIX Setup project and C# Custom Action Project.

We will add a dummy text file in the setup project to be used as installation content and will change a little bit the auto created Product.wxs file.

<?xml version="1.0" encoding="UTF-8"?>
<Wix xmlns="">
  <Product Id="*" Name="WixWithCustomAction" 

  Language="1033" Version="" 


    <Package InstallerVersion="200" 

    Compressed="yes" InstallScope="perMachine" />

    <Media Id='1' Cabinet='' EmbedCab='yes' />

    <Directory Id='TARGETDIR' Name='SourceDir'>
      <Directory Id='ProgramFilesFolder'>
        <Directory Id='WixWithCustomAction' Name='WixWithCustomAction'>
          <Component Id="DummyContent" 

            <!-- Add the dummy file as content. -->
            <File Id="DummyFile" KeyPath="yes" 

            Source="Dummy.txt" Vital="yes" />

    <Feature Id="Complete" Title="WixWithCustomAction" Level="1">
      <ComponentRef Id='DummyContent' />

That's how our solution looks like:

If we build the WixWithCustomAction project, WixWithCustomAction.msi will be created. If we run it, WixWithCustomAction folder will be created in program files with Dummy.txt file inside. But now, we want to add a custom action which will create a file in C:\Temp folder. We will use the MyCustomActionProject for this. Let's change the CustomAction class a little bit:

using Microsoft.Deployment.WindowsInstaller;
using System.IO;

namespace MyCustomActionProject
    public class CustomActions
        public static ActionResult MyCustomAcion(Session session)
            session.Log("Executing MyCustomAcion");


            return ActionResult.Success;

Now, we just need to call this custom action from the installer. To do this, we will add a reference to this project in the setup project.  

Now let's add the custom action in the Product.wxs file. Adding the project as a reference allows as to use these variables. But adding custom action is a little bit complicated. After building the MyCustomActionProject.dll file, we will need a call to MakeSfxCA.exe and sfxca.dll in your installed WiX toolset as the DLL needs a reference to Microsoft.Deployment.WindowsInstaller.dll and has CustomAction.config attached. Calling the MakeSfxCA.exe tool will package, the project output to MyCustomActionProject.CA.dll (here you can find some additional information about this). As we use "C# Custom Action Project" there is an import added to $(MSBuildExtensionsPath)\Microsoft\WiX\v3.x\Wix.CA.targets file which will create this package on build. To check this, you can build the custom action project and see the output:

So the custom action in the Product.wxs needs to reference the .CA.dll file. That's why we cannot use $(var.MyCustomActionProject.TargetPath) as a source for the custom action binary, but we will have to construct the source path like this: $(var.MyCustomActionProject.TargetDir)$(var.MyCustomActionProject.TargetName).CA.dll. The other option is not to use the project reference but add the full path to the custom action output. So we will add the following rows to the wxs file:

<!--The source file should be the MyCustomActionProject.CA.dll file, 
that's why it's constructed this way-->
<Binary Id='CustomActionBinary' 

SourceFile='$(var.MyCustomActionProject.TargetDir)$(var.MyCustomActionProject.TargetName).CA.dll' />

<!--The DllEntry must be the name of the method to be called from the custom action project, in our case - MyCustomActionMethod 
The Execute attribute will specify the deferred status of our custom action.
And finally, HideTarget will allow us to disable logging the parameters passed 
to this custom action if security considerations so dictate.-->
<CustomAction Id='CustomActionId' BinaryKey='CustomActionBinary' 

DllEntry='MyCustomActionMethod' Execute="deferred" HideTarget="yes"/>

  <!--We want to call the custom action before the install finalizes-->
  <Custom Action='CustomActionId' Before='InstallFinalize'/>

And that's it. Now if we build the setup project and run the created msi installer, c:\temp\installed.txt will be created as a part of the installation process.

You can find the solution file here.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Vasil Trifonov
Software Developer (Senior) Telerik
Bulgaria Bulgaria
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionShort and sweet Pin
Valya-S9-Jul-15 15:19
memberValya-S9-Jul-15 15:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171207.1 | Last Updated 18 Dec 2012
Article Copyright 2012 by Vasil Trifonov
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid