Create C# (CSharp) DLL for MSI Custom Actions

Windows Installer technology offers the possibility to use Visual Basic Scripts, Javascript files or dynamic linked libraries (DLL) as custom actions to control the install/uninstall of a certain product.

When discussing about dynamic linked libraries, many think that you can only use C/C++ to create suitable ones for the Windows Installer, however, this is not the case. With the help of WIX Toolset, creating C# dlls for custom actions was never easier, and let’s see how we can achieve this.

 

Prerequisites

Let’s start with what is needed in order to create C# DLLs for the Windows Installer. This article is intended for Visual Studio 2019, if you are using older versions of Visual Studio you can check this article.

After you download & install Visual Studio 2019 (in my case the Community edition), download & install the Wix Toolset Visual Studio 2019 Extension:

If you want to learn more about WIX Toolset, you can install the full version from here, and read some of my other posts regarding this.

 

Create Project

Now that the WIX Toolset extension for Visual Studio is installed, open up Visual Studio and you will see that under Languages, a new one called WiX has been added. Select WiX and then search for C# Custom Action Project for WiX v3.

After you selected it, click on Next and give it a Project Name and select the desired Framework. For this example I ain’t going to do something crazy, so I’m going to leave .Net Framework 3.5.

Write the code

After the project is created, you will notice in the .cs file that the Microsoft.Deployment.WindowsInstaller namespace is automatically added. This is the class which offers access to all the Windows Installer APIs. A full diagram for it can be found here.

You will also notice that the namespace contains a reference for the CustomAction class. We are going to put all the desired code that we want to execute under a ActionResult Class.

For this example, I only went and created some registry keys, and the code looks as following:

The only thing we need to remember from this step is the name of the method (function) is MyMethod. We are going to need this when we place the DLL in the MSI.

After we finish with the code, we go to Build > Build Solution, or CTRL+SHIFT+B. And voila, our DLL is created:

Add Custom Action in MSI

Once we have our DLL created, we are going to open Advanced Installer and navigate to the Custom Actions Page. Once here, search for Call function from attached native DLL and click Add custom action with sequence.

Then, in the Attached DLL field, browse to the previously created DLL file. Remember I said that the method (function) we used in Visual Studio is important? This is where we tell Advanced Installer which function to use when our DLL is executed, so in the Function field write your method (function) name.

In terms of custom action configuration, I’ve used the Execution to be When the system is being modified, and the Execution Stage Condition field I set it to NOT Installed OR REINSTALL so that the action is executed during installation and repair of the MSI, but these options depend on what you want to achieve with your Custom Action.

I added some files and registry in the MSI, and then build it.

 

Test it

As a final step, install the MSI and see if your custom action performs as desired:

In this case, the result was a success and the registry keys were written during execution:

Sharing is caring!

Leave a comment

Your email address will not be published. Required fields are marked *

four × 4 =