如何使用Windows Installer的托管自定义操作显示错误消息

时间:2008-12-16 12:46:59

标签: error-handling windows-installer managed custom-action

我正在编写一个托管自定义操作。我正在使用Windows Installer Xml中的DTF Framework将托管dll包装到可用的CA dll中。 CA做了它应该做的事情,但我仍然遇到错误处理问题:

Dim record As New Record(1)

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

以上代码生成MSI日志中显示的以下文本:

  

MSI(c)(C4!C6)[13:15:08:749]:产品:TestMSI - 错误27533.区分大小写的密码不匹配。

错误号是指MSI中Error表中包含的代码。上面显示的消息是正确的。

我的问题是:为什么Windows Installer不会创建一个通知用户错误的对话框?

3 个答案:

答案 0 :(得分:15)

MSI可以这样做,但你需要在messageType参数的一些额外值中使用OR。

例如

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

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

有关详细信息,请参阅wix-users邮件列表中的this thread

答案 1 :(得分:2)

我遇到了同样的问题,根据Wix:Nick Ramirez的Windows Installer XML开发人员指南,当从UI控件调用自定义操作时,日志和消息方法不起作用。

答案 2 :(得分:-3)

如果要显示包含消息的对话框,则必须自己执行此操作。

以下是我用于在运行SQL的托管自定义操作中执行错误处理的一些代码。 如果安装使用完整UI,则会显示消息框。 它在c#中,但希望你能得到这个想法。

    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);
        }
    }