Search code examples
c#wixcustom-action

Wix Toolset confirmation dialog before exit


I am developing an installation msi file using Wix toolset (in VS 2012) and faced with the problem that I can't add a confirmation dialog when user press button 'Cancel'. I tried to add also custom action dll with the message box, but if the function returns ActionResult.Failure, then my setup shows error dialog.

Original control:

<Control Type="PushButton" Id="progressCancelBtn" Width="56" Height="17" X="296" Y="244">
   <Text>Cancel</Text>
   <Publish Event="EndDialog" Value="Exit" />
</Control>

Try number 1:

public class CustomActions
    {
        [CustomAction]
        public static ActionResult ShowExitMessageBox(Session session)
        {
            DialogResult result = MessageBox.Show("Exit installation?", "Exit", MessageBoxButtons.YesNo);

            if (result == DialogResult.Yes)
            {
                return ActionResult.Failure;
            }
            else
            {
                return ActionResult.Success;
            }
        }
    }

Custom action:

 <CustomAction Id='ShowExitMsgBox' BinaryKey='CustomActions' DllEntry='ShowExitMessageBox' Execute='immediate'               Return='ignore'/>

    <Binary Id='CustomActions' SourceFile='$(var.CASourcesPath)\CustomActions.CA.dll'/>

And tried to use:

<Control Type="PushButton" Id="cancelBtn" Width="56" Height="17" X="300" Y="244" Cancel="yes">
    <Text>Cancel</Text>
    <Publish Event="DoAction" Value="ShowExitMsgBox">1</Publish>
    <!--Publish Event="EndDialog" Value="Exit" /-->
</Control>

But it shows like failure, instead of cancellation.

Thank you for help in advance.

Update:

I changed my custom action C# code to the next:

public class CustomActions
    {
        [CustomAction]
        public static ActionResult ShowExitMessageBox(Session session)
        {
            DialogResult result = MessageBox.Show("Exit installation?", "Exit", MessageBoxButtons.YesNo);

            if (result == DialogResult.Yes)
            {
                session["CANCELEDPROP"] = "1";
            }
            else
            {
                session["CANCELEDPROP"] = "0";
            }

            return ActionResult.Success;
        }
    }

and WIX part:

// in Product.wxs
<Property Id="CANCELEDPROP" Secure="yes">0</Property> 
// in UIFile.wxs
<Control Type="PushButton" Id="cancelBtn" Width="56" Height="17" X="300" Y="244" Cancel="yes">
    <Text>Cancel</Text>
    <Publish Event="DoAction" Value="ShowExitMsgBox">1</Publish>
    <Publish Event="EndDialog" Value="Exit">CANCELEDPROP=1</Publish>
</Control>

It works on the first page, but I get an error on progress page: "Windows installer has stopped working" after I pressed some button on my message box. :( I have found out if I don't use message box, then everything works. It is strange.

Update 2:

I tried to use session.Message instead of Windows.Forms, but message box just doesn't appear. Strange.

[CustomAction]
public static ActionResult ShowExitMessageBox(Session session)
{
    Record record = new Record();
    record.FormatString = "Exit installation?";

    MessageResult result = session.Message(InstallMessage.Warning 
                                                    | (InstallMessage)System.Windows.Forms.MessageBoxIcon.Warning 
                                                    | (InstallMessage)System.Windows.Forms.MessageBoxButtons.YesNo,
                                                    record);

    if (result == MessageResult.Yes)
    {
        session["CANCELEDPROP"] = "1";
    }
    else
    {
        session["CANCELEDPROP"] = "0";
    }

    return ActionResult.Success;
}

Solution

  • Finally I have found a solution by using CancelDlg from WixUIExtension.dll (http://wixtoolset.org/documentation/manual/v3/wixui/dialog_reference/wixui_dialogs.html)

    1. Add reference to WixUIExtension.dll (path on my computer is the next C:\Program Files (x86)\WiX Toolset v3.11\bin)

    2. Add reference to icon that would be on the message box (Product.wxs):

      < Binary Id="WixUI_Ico_Info" SourceFile="Images/MyIcon.ico" />

    3. Control itself:

      < Control Type="PushButton" Id="cancelBtn" Width="56" Height="17" X="300" Y="244" Cancel="yes"> < Text>Cancel < Publish Event="SpawnDialog" Value="CancelDlg">1 < /Control>