How do I show Error Message using Managed Custom Actions with Windows Installer

大憨熊 提交于 2019-12-19 05:45:04

问题


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


来源:https://stackoverflow.com/questions/371204/how-do-i-show-error-message-using-managed-custom-actions-with-windows-installer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!