Blog Archives

C# Create a Windows Installer Class Custom Action bootstrap DLL in .Net

The standard windows installer project included with Visual Studio right out of the box is great in many deployment scenarios.

For more advanced needs, organizations and developers usually defer to 3rd party solutions, such as InstallShield, Wise, wix or NSIS (NullSoft).

These solutions are all very well tested and have minimal learning curve in a lot of cases. However, if you are tight on budget or simply looking for the experience and full customization, you can also create your own custom action installer class in Visual Studio.

Simply create a new DLL project included in the same solution as your windows installer project. From the Windows Installer Project go to “File System” and in your Application Folder add “Project Output” and select your new DLL. Now go to the “custom actions” tab and under install and/or uninstall right click and “Add Custom Action” then select the output under Application Folder. That’s it!

Make sure the class for your DLL has “RunInstaller” attributes and looks similar to the snippet below. You may also want to do some light reading on the “CustomActionData” property to see if there are any variables you may need such as “ProductCode”.

To change the custom action data property, from the custom action tab simply select your DLL output after adding it, and expand the Visual Studio Properties tab on the side.

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;

namespace MSIBootstrap
{
    [RunInstaller(true)]
    public partial class InstallHelper : Installer
    {
        public static string InstallTitle = "Discount Notes"; //used by install/uninstall to get window handle
        
        public InstallHelper()
        {
            InitializeComponent();
        }

        public override void Install(System.Collections.IDictionary stateSaver)
        {
try {
//your custom code
base.Install(stateSaver);
}
catch (Exception ex) {
throw new InstallException(ex.ToString());
}
}

 public override void Uninstall(System.Collections.IDictionary stateSaver)
        {
//mimic same try/catch design pattern if you like
base.Uninstall;
}
}

With this approach, you are essentially using the standard windows installer project to bootstrap your custom action DLL.

This is very powerful, and allows you to define your own code logic and conditions as well as custom interfaces and forms designed directly from within Visual Studio.

References
InstallShield, http://www.flexerasoftware.com/products/installshield.htm
Wise installer, http://www.wise.com/Products/Installations/WiseInstallerEvaluations.aspx
wix installer, http://wix.sourceforge.net/
Nullsoft NSIS, http://nsis.sourceforge.net/Download
Wikipedia (bootstrapping), http://en.wikipedia.org/wiki/Bootstrapping_%28computing%29
devx.com, http://www.devx.com/dotnet/Article/20849/1954
PCReview.co.uk, http://www.pcreview.co.uk/forums/install-project-custom-action-failure-t2612207.html
codefounders.com (persisting savedState), http://www.codefounders.com/community/blogs/davidg/archive/2007/06/27/persisting-state-in-a-windows-installer-managed-custom-action.aspx
MSDN, “CustomActionData”, http://msdn.microsoft.com/en-us/library/2w2fhwzz(v=vs.80).aspx

Call Cancel from Custom Action in .Net Windows Installer Project

As an alternative to using the “Rollback” or “InstallException” approach, simply use pinvoke/interop to call the cancel button on the base installer form directly.

(see my related post for more detail and full code)

ShowWindow(msiwindowhandle, ShowWindowCommands.Show);
                IntPtr cancelbuttonhandle;
                const int BM_CLICK = 0x00F5;
                msiwindowhandle = FindWindow("#32770", InstallTitle);
                cancelbuttonhandle = FindWindowEx(msiwindowhandle, IntPtr.Zero, "Button", "Cancel");
                SetActiveWindow(msiwindowhandle); //necessary for button click to fire
                SendMessage(cancelbuttonhandle, BM_CLICK, IntPtr.Zero, IntPtr.Zero);

References
http://ronniediaz.com/2011/06/20/the-savedstate-dictionary-does-not-contain-the-expected-values-and-might-have-been-corrupted/

Remove Repair Option from Custom Windows Installer

This technically can be done in various ways relating to InstallShield, Wise, WiX, NSIS or other installer platforms, but generally speaking is the same concept all throughout whether you are using third party tools or creating your own bootstrap/windows installer project/custom action.

An MSI file contains tables of data. Within these tables, there is one labeled “property” which contains a series of string/value items. The properties which control visibility of repair/modify in button are referred to as:

ARPNOMODIFY
ARPNOREPAIR

In order to modify the MSI tables and change these properties you must first be able to modify the MSI file. This can be done one of two ways:

Option 1:
Download and install the Windows SDK Components for Windows Installer Developers.

Once downloaded and installed, navigate to “\Program Files\Microsoft SDKs\Windows\v7.0\Bin” and run “Orca.msi” to begin the install for orca.

Once installed, run orca and open the propery table and add an entry for ARPNOREPAIR and set its value to 1.

Option 2:

As an alternative, unless you have some other reason for DLing the SDK, you can totally bypass the 2.5GB DL just for a little orca file by using an awesome proprietary tool called SuperOrca :).

SuperOrca IMO is much improved over the basic MS orca, but the end result is the same. Open the MSI, navigate to PROPERTY table, and add the entry with a value of 1, save, test.

For C++ or custom action programmers, you can call MsiSetProperty and pass in ARNOREPAIR for the name and 1 for the value for the same end result.

MsiSetProperty and MsiGetProperty cannot be called from your typical C# installer class/bootstrap DLL, since the handle to the installer is never passed in.

For a little background history, in my particular case, a custom action in the installer is targeting a DLL I created (separate project in the same solution). This DLL inherits the installer class and overrides the install and uninstall methods.

References
Stackoverflow, http://stackoverflow.com/questions/819722/remove-repair-option-screen-from-msi-installer
MSDN (ARPNOMODIFY), http://msdn.microsoft.com/en-us/library/aa367590%28VS.85%29.aspx
MSDN (ARPNOREPAIR), http://msdn.microsoft.com/en-us/library/aa367592%28VS.85%29.aspx
MSDN (Windows Installer SDK), http://msdn.microsoft.com/en-us/library/aa370834%28v=vs.85%29.aspx
ureader.com, http://www.ureader.com/msg/16531570.aspx
Orca, http://msdn.microsoft.com/en-us/library/aa370557%28v=vs.85%29.aspx
SuperOrca (Pantaray), http://www.pantaray.com/msi_super_orca.html
MSDN (msisetproperty), http://msdn.microsoft.com/en-us/library/aa370391%28v=VS.85%29.aspx
MSDN Blogs (msisetproperty,getproperty), http://social.msdn.microsoft.com/Forums/en/winformssetup/thread/e7726f09-79eb-4fe1-ba2b-add79514f5f5
MSDN (create a custom action), http://msdn.microsoft.com/en-us/library/d9k65z2d%28v=vs.80%29.aspx

Follow

Get every new post delivered to your Inbox.