Sunday, May 1, 2011

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

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?

From stackoverflow
  • 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);
      }
     }
    
  • 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.

  • Hi thanks for the info.

    Do you know if there is a way too show big messages? When i use:

    Record record = new Record(); record.FormatString = pReallyBigMessage;

                Session.Message(InstallMessage.Error | (InstallMessage)System.Windows.Forms.MessageBoxIcon.Warning |
                    (InstallMessage)System.Windows.Forms.MessageBoxButtons.OK, record);
    

    Only parts are displayed. I would like to stick with the Session.Message functionality and not use the Windows.Forms Messagebox because of the focus.

    Greetings

  • Here is a walk-through concurring with ayeko's post.

    leogdion : Here is more information on showing a dialog or messagebox in a custom installer. http://social.msdn.microsoft.com/Forums/en-US/winformssetup/thread/bbe69f12-8908-4c65-aa89-1963720d4c11

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.