How do I show Error Message using Managed Custom A

2019-02-16 17:15发布

问题:

I am writing a managed custom action. I am using the DTF Framework from Windows Installer Xml to wrap the managed dll into a usable CA dll. The CA does what it is supposed to, but I am still having trouble with error handling:

Dim record As New Record(1)

' Field 0 intentionally left blank
' Field 1 contains error number
record(1) = 27533
session.Message(InstallMessage.Error, record)

The above code produces the following text shown in the MSI log:

MSI (c) (C4 ! C6) [13:15:08:749]: Product: TestMSI -- Error 27533. The case-sensitive passwords do not match.

The error number refers to the code contained in the Error table within the MSI. The Message shown above is correct.

My problem is: Why does Windows Installer NOT create a dialog notifying the user about the error?

回答1:

MSI can do this, but you need to OR in some extra values for the messageType argument.

eg.

Record record = new Record();
record.FormatString = string.Format("Something has gone wrong!");

session.Message(
    InstallMessage.Error | (InstallMessage) ( MessageBoxIcon.Error ) |
    (InstallMessage) MessageBoxButtons.OK,
    record );

See this thread from the wix-users mailing list for more details.



回答2:

I have run into the same problem, according to Wix: A Developer's Guide to Windows Installer XML by Nick Ramirez, the log and message methods don't work when a custom action is called from a UI control.



回答3:

If you want a dialog to show up that contains the message, you must do it yourself.

Here's some code I use to do error handling in managed custom actions that run SQL. It shows a messagebox if the installation is operating with a full UI. It's in c# but hopefully you'll get the idea.

    private void _handleSqlException(SqlException ex)
    {
        StringBuilder errorMessage = new StringBuilder();
        errorMessage.Append("A SQL error has occurred.");
        for (int i = 0; i < ex.Errors.Count; i++)
        {
            errorMessage.Append("Index #" + i + "\n" +
                "Message: " + ex.Errors[i].Message + "\n" +
                "LineNumber: " + ex.Errors[i].LineNumber + "\n" +
                "Source: " + ex.Errors[i].Source + "\n" +
                "Procedure: " + ex.Errors[i].Procedure + "\n");
        }
        session.Log(errorMessage);
        if (session["UILevel"] == "5")
        {
            MessageBox.Show(errorMessage);
        }
    }