How to Manage?

A common question is whether custom actions can be written in managed code, C#, VB.NET or something similar. After all, these runtime environments offer a much richer set of features, besides, there might be some programmers working with these languages who are less familiar with other programming languages.

Earlier, in the WiX version 2 era, writing custom actions in managed code was only possible with hacks and was considered very bad and dangerous practice. Version 3 brought changes with the introduction of its Deployment Tools Foundation (DTF), a set of .NET class libraries and related resources. If you can accept the obvious dependency limitations (you have to make sure .NET is present on the installation machine, possibly using a bootstrap installer first, and you can have problems during uninstallation as well, if the user removes the .NET Framework before uninstalling your application), here is our previous sample custom action ported to C#:

There is one tiny bit we have to modify in our source code of SampleAskKeyNET. The name of the DLL will be different because the straight managed DLL has to be wrapped into a special package bridging the gap between Windows Installer and the managed world:

<Binary Id="CheckPID" SourceFile="CheckPIDPackage.dll" />

Also prepare a small CustomAction.config file. It will describe the runtime your managed custom action is dependent upon.

There is a price to pay for the relative simplicity of working with a .NET language: the build process is more complicated. If you use an IDE, it might be simpler, you can find a sample project file under the DTF branch of the WiX source package. Here we only can show you the command line way. Look for Microsoft.Deployment.WindowsInstaller.dll, MakeSfxCA.exe, and sfxca.dll in your installed WiX toolset, you will find them there. Where we indicated a path, you have to supply an absolute full path or else MakeSfxCA will give an error message and the resulting DLL will not be usable.