Writing managed custom actions in an easy way

In my last post, I talked about hosting the CRL within a native custom action.
This week I found an easier way to execute a managed custom action, it is based on a hack and not extra C++ code is required.
Actually, you can't write a C# custom action because the compiler doesn't allow you to create a __stdcall function to be 
called from the outside.
Well, take a look to this article, it shows how to overcome that problem.

Let's use this article to write our managed custom action, I'll start writing the custom action's skeleton:

using System;

namespace CustomActions
{
    public class MyCustomAction
    {
        public static int Execute( long handle )
        {
            System.Windows.Forms.MessageBox.Show( String.Format( "Hello World {0}", handle ) );

            return 0;

        }
    }
}

When the installer calls to this custom action, it will show a message box with the current msi handler number.
As next step, you will have to read that article and do the same things, but changing the "SayHello" method by "Execute".
After finishing the last article step, you will have a managed assembly, which exports an "Execute" method.
You will able to use that method as "DllEntry" when you define the custom action in the custom actions table.

Using wix, the custom action definition should look like this:

<CustomAction Id="CustomActions" BinaryKey="CustomActions" DllEntry="Execute" />

I hope this can help you to develop more custom actions in an easy way :)

5 Comments

  • Not convinced 100% about the hack, but it works like a dream!

  • The article modification isn't quite right if you want more than one vtable entry. For example, if you have 2 methods to export, you need something like this:



    .vtfixup[2] int32 fromunmanaged at VT_01

    .data VT_01 = int32[2]



    Note the [2] for the .data entry, not (0). Then for each method, you need for example:



    .vtentry 1:1

    .export [1] as Foo1



    and



    .vtentry 1:2

    .export [2] as Foo2



    I've used it to create methods for creating and deleting MSMQs. So much easier in C# than C++, especially as I haven't touched C++ for such a long time. Now I've just got to figure out how WiX calls custom actions on install or removal. This is a long journey; I need an installer that creates an app, installs a web site, a DB into SQL Server and creates MSMQs! If I get it done correctly I might post it as an example somewhere, there are few around for WiX.





  • Peter, I agree with you. The documentation shipped in wix lacks good and complete samples.

  • Playing around, using a .NET executable seems easier as it's without all of these hacks. The simplest way is create a console project in VS, then set the build to a Windows app (this avoids VS inserting all the standard forms code, and obviously you don't want a console window opening). Seems to work fine - any reasons why it might be a bad idea?



  • Well, using a .Net executable is not a bad idea, but you can&#180;t use the installer context from it, for example, you can&#180;t query the available properties. So, if you need to develop complex custom actions, which require context information, hosting the .Net framework or using this hack are better ways to accomplish that.

Comments have been disabled for this content.