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

坚强是说给别人听的谎言 提交于 2019-12-01 03:50:48

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.

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.

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