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

About these ads

About Ronnie Diaz

Ronnie Diaz is an enterprise software engineer responsible for front-end and back-end development for companies in many industries. Heavily involved in cloud development, online retail, e-commerce and electronic ordering, fulfillment and customer relational systems.

Posted on June 20, 2011, in Programming & Development and tagged , , , , , , , , , . Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: