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

Mep*_*toe 9 error-handling windows-installer custom-action managed

我正在编写一个托管自定义操作.我正在使用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)
Run Code Online (Sandbox Code Playgroud)

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

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

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

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

Dav*_*ner 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 );
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请参阅wix-users邮件列表中的此主题.


小智 -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);
        }
    }
Run Code Online (Sandbox Code Playgroud)

  • 这不是最广泛接受的方法,因为消息窗口可能会出现在安装程序后面,并且我认为将以不同的权限启动 (3认同)