WiX 自定义 Bootstrap - 单实例检查

标签 wix administrator wix3.7 burn

我使用以下代码来检查 CustomBA 的单个实例是否已在 CustomBA 的运行 block 中运行。

当用户通过双击启动“setup.exe”(CustomBA)时,下面的代码将返回 true,这是预期的行为。

但是,当用户右键单击并以管理员身份启动它时,代码将返回 false。这是为什么?

private bool IsSingleInstanceOfSetupRunning()
{
    bool result = true;

    Process currentProcess = Process.GetCurrentProcess();
    if (Process.GetProcessesByName(currentProcess.ProcessName).Length > 1)
    {
        result = false;
    }

    return result;
}

最佳答案

WiX 引擎似乎检测到该进程正在以管理员身份运行,并启动用于实际安装 MSI 的辅助进程。所以确实有两个同名进程在运行。

一旦您的 CustomBA 代码调用 Engine.Apply(),您就可以在非管理进程中看到相同的行为。这通常是当用户看到 UAC 提示时,引擎启动第二个提升的进程来处理实际的 MSI 安装。

由于主进程已经以管理员身份运行,并且启动第二个进程时不会出现 UAC 提示,因此引擎会立即启动它,而不是等待对 Engine.Apply() 的调用。

另请注意:如果您要执行主要升级,则升级期间将运行先前版本的卸载(以静默模式),这将导致额外的进程。即使已经有另一个进程(您的升级进程)正在运行,您也需要确保允许卸载进程运行。

一种方法是使用互斥体进行检查,但仅在 DisplayMode Display.Full 下运行时:

if (DisplayMode == Display.Full)
{
    bool mutexCreated = false;
    mutex = new Mutex(true, @"My Installer F1096BB9-CFDF-4AD1-91D8-9AA8805784A8", out mutexCreated);
    if (!mutexCreated)
    {
        MessageBox.Show("Another instance of the installer is already running. You may only run one at a time.",
                        "Installer already running", MessageBoxButton.OK,
                        MessageBoxImage.Warning);
        Log("Installer already running");
        Exit(ActionResult.NotExecuted);
    }
}

public void Exit(ActionResult actionResult)
{

    if (mutex != null)
    {
        mutex.Close();
        mutex = null;
    }

    Log(string.Format("Exiting with code {0}", actionResult));
    Engine.Quit((int) actionResult);    
}

关于WiX 自定义 Bootstrap - 单实例检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19498396/

相关文章:

Wix 主要升级 : how do I prevent Windows service reinstallation?

基于 INF 的打印机驱动程序的 WIX 安装程序

wix - 如何在 Wix 中安装后运行已安装的应用程序?

batch-file - 以管理员身份运行的批处理脚本

windows - 如何在不使用任何 cmd 快捷方式的情况下通过 cmd 命令以管理员权限运行 cmd?

具有完整文件路径的 WiX 安装程序文件搜索

delphi - 在Delphi中如何以管理员权限运行命令?

wix - 在 WiX 3.7 中结合绝对路径和相对路径

Wix 刻录在 ARP 中不显示多个条目

wix - 在 Bootstrap 期间在 Msi 包之间执行脚本