Alezis
Alezis

Reputation: 2759

MSI how to detect is Excel running

Our application is Excel add-in. We use Wix 3.10 to build MSI. Currently we can perform uninstall of the app even if Excel running with connected add-in. I need to interrupt uninstall process if Excel is running. We don't need to close Excel, I just need to detect is it running. If so, I need to show appropriate message to user and stop uninstall process. How to detect is Excel (any app) running during uninstall process ? Can I achieve desired behavior using "standard" MSI/Wix capabilities or should I write some custom action ?

Upvotes: 1

Views: 753

Answers (2)

Alezis
Alezis

Reputation: 2759

Finally I implemented my own custom action. This action fires on any installation mode: install/uninstall/repair etc. The action show message box that asks user to close Excel and has two buttons 'Retry' and 'Cancel'. If user press 'Retry' code repeat checking is Excel running, 'Cancel' leads to another dialog when user is asked to Proceed or really cancel. There is action in C# (Please note that this code is used Wix SDK: Microsoft.Deployment.WindowsInstaller.dll):

[CustomAction]
public static ActionResult IsExcelRunning(Session session)
{
    session.Log("Begin IsExcelRunning.");

    MessageResult msgBoxResult = MessageResult.Cancel;
    do
    {
        session.Log("Try to find running Excel.");

        bool isExcelRunning = false;
        try
        {
            var obj = Marshal.GetActiveObject("Excel.Application");
            isExcelRunning = null != obj;
            session.Log("IsExcelRunning = " + isExcelRunning);

            if (null != obj)
            {
                Marshal.FinalReleaseComObject(obj);
                obj = null;
                GC.Collect();
            }
        }
        catch (Exception ex)
        {
            session.Log("Exception:" + ex);
        }

        if (!isExcelRunning)
        {
            session.Log("End IsExcelRunning.");
            return ActionResult.Success;
        }
        else
        {
            var errorRecord = new Record(1);
            errorRecord.SetInteger(1, 30000);
            try
            {
                msgBoxResult =
                    session.Message(InstallMessage.Error | (InstallMessage) MessageButtons.RetryCancel,
                        errorRecord);

            }
            catch (InstallCanceledException)
            {
                var questionRecord = new Record(1);
                questionRecord.SetInteger(1, 1602);

                if (MessageResult.Yes ==
                    session.Message(
                        InstallMessage.Error | (InstallMessage)MessageButtons.YesNo |
                        (InstallMessage)MessageBoxIcon.Question, questionRecord))
                {
                    session.Log("End SetIsExcelRunning.");
                    return ActionResult.UserExit;
                }
                else
                {
                    msgBoxResult = MessageResult.Retry;
                }
            }
            catch (Exception ex)
            {
                session.Log("Unexpected exception:" + ex);
                throw;
            }
        }
    } while (MessageResult.Retry == msgBoxResult);

    session.Log("End IsExcelRunning.");
    return ActionResult.UserExit;
}

Upvotes: 0

Christopher Painter
Christopher Painter

Reputation: 55581

You'll need a custom action to query Windows if the excel process is running and set a property with the result. Then you can use that in a Launch Condition (Condition element) to notify the user and block installation.

Upvotes: 3

Related Questions