【问题标题】:WIX execute custom action with admin privilegeWIX 以管理员权限执行自定义操作
【发布时间】:2020-03-10 17:45:21
【问题描述】:

我为我的 WIX 安装程序编写了一个自定义操作。该操作的 Execute-attribute 设置为 deferred 和 Impersonate 并在 InstallFinalize 之前运行,但它在此操作中遇到了一个问题,即缺少管理员权限。该操作在 INSTALLFOLDER 中创建一个文件,即 Program File (x86)

这是我的 WIX 代码:

    <?xml version="1.0" encoding="UTF-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
    <Product Id="*" 
           Name="WixTesterSetup" 
           Language="1033" 
           Version="1.0.0.0" 
           Manufacturer="WixTester" 
           UpgradeCode="77b7ed9a-5394-43e9-aecb-cd9985368ef6">
        <Package InstallerVersion="200" Compressed="yes" InstallScope="perMachine" />

        <MajorUpgrade DowngradeErrorMessage="A newer version of [ProductName] is already installed." />
        <MediaTemplate />

    <Feature Id="Core" Title="Core" Level="1" ConfigurableDirectory="INSTALLFOLDER" />

    <UI>
      <UIRef Id="WixUI_Minimal" />
      <Publish  Dialog="ExitDialog"
                Control="Finish"
                Event="DoAction"
                Value="LaunchApplication">WIXUI_EXITDIALOGOPTIONALCHECKBOX = 1 and NOT Installed</Publish>
    </UI>

    <Property Id="WIXUI_EXITDIALOGOPTIONALCHECKBOXTEXT" Value="Launch Wix Tester" />
    <Property Id="WixShellEecxTarget" Value="[#WixTester.exe]" />
    <CustomAction Id="LaunchApplication" BinaryKey="WixCA" DllEntry="WixShellExec" Impersonate="yes" />

    <Binary Id="CustomActionBinary" SourceFile="$(var.RegistrationInfoCustomAction.TargetDir)$(var.RegistrationInfoCustomAction.TargetName).CA.dll"/>
    <CustomAction Id="RegistrationInfoCustomAction" BinaryKey="CustomActionBinary" DllEntry="SaveUserInfo" Execute="deferred" Impersonate="no" />

    <InstallExecuteSequence>
      <Custom Action='RegistrationInfoCustomAction' Before='InstallFinalize'>NOT Installed</Custom>
    </InstallExecuteSequence>

    <Directory Id="TARGETDIR" Name="SourceDir">
            <Directory Id="ProgramFilesFolder">
        <Directory Id="INSTALLFOLDER" Name="WixTesterSetup">
          <Component Feature="Core">
            <File Id="WixTester.exe" Source="$(var.WixTester.TargetPath)" KeyPath="yes" Checksum="yes"/>
          </Component>
        </Directory>
            </Directory>
        </Directory>

  </Product>

</Wix>

简单的自定义操作:

    public class CustomActions
{
    [CustomAction]
    public static ActionResult SaveUserInfo(Session session)
    {
        File.Create(System.IO.Path.Combine(session.GetTargetPath("INSTALLFOLDER"), "test.xml"));

        return ActionResult.Success;
    }
}

WixTester 不感兴趣:

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Test Started");
        Console.ReadLine();
    }
}

【问题讨论】:

    标签: c# wix admin wix3


    【解决方案1】:

    诊断:我怀疑除了权限之外还有其他问题。请尝试以下方法:

    Verbose Log File:请创建详细日志文件:

    msiexec.exe /I "C:\file.msi" /QN /L*V "C:\msilog.log"
    

    Hot Log 解读提示:在日志文件中搜索 "value 3" 以查找 Rob Mensching 解释的错误(Wix & Orca 作者)。否则,MSI 日志文件可能会不堪重负。

    更多:How to interpret an MSI Log File(和PDF format from WayBack)。

    调试自定义操作:您是否将调试器附加到相关自定义操作?请在此处查找信息:WIxsharp debug custom action in console - 和 a direct link to an Advanced Installer demonstration video。以及指向MSDN / Microsoft Docs 的链接。

    调试简述:显示一个消息框并附加到它。


    XML 文件:XML 文件可以使用WiX XML features 安装,不应通过自定义操作生成。您还可以在启动时在用户可写的位置从应用程序本身创建文件。以下是后者的几个链接:

    建议:我不知道哪种方法适合你。建议您通过应用程序生成文件并保存在 用户资料。每个用户一个 xml 文件。


    链接:

    【讨论】:

    • 感谢您的回复。如果我附加到 msiexec.exe 进程,我不会达到我的自定义操作。
    【解决方案2】:

    问题在于延迟的自定义操作无权访问session["PropertyName"],解决方案是使用session.CustomActionData["PORTProperty"] 并将变量传递给自定义操作类型 51。新的 WIX 代码如下所示:

        <?xml version="1.0" encoding="UTF-8"?>
    <Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
        <Product Id="*" 
               Name="WixTesterSetup" 
               Language="1033" 
               Version="1.0.0.0" 
               Manufacturer="WixTester" 
               UpgradeCode="77b7ed9a-5394-43e9-aecb-cd9985368ef6">
            <Package InstallerVersion="200" Compressed="yes" InstallScope="perMachine" />
    
            <MajorUpgrade DowngradeErrorMessage="A newer version of [ProductName] is already installed." />
            <MediaTemplate />
    
        <Feature Id="Core" Title="Core" Level="1" ConfigurableDirectory="INSTALLFOLDER" />
    
        <UI>
          <UIRef Id="WixUI_Minimal" />
          <Publish  Dialog="ExitDialog"
                    Control="Finish"
                    Event="DoAction"
                    Value="LaunchApplication">WIXUI_EXITDIALOGOPTIONALCHECKBOX = 1 and NOT Installed</Publish>
        </UI>
    
        <Property Id="WIXUI_EXITDIALOGOPTIONALCHECKBOXTEXT" Value="Launch Wix Tester" />
        <Property Id="WixShellEecxTarget" Value="[#WixTester.exe]" />
        <CustomAction Id="LaunchApplication" BinaryKey="WixCA" DllEntry="WixShellExec" Impersonate="yes" />
    
        <Binary Id="CustomActionBinary" SourceFile="$(var.RegistrationInfoCustomAction.TargetDir)$(var.RegistrationInfoCustomAction.TargetName).CA.dll"/>
        <CustomAction Id="RegistrationInfoCustomAction" BinaryKey="CustomActionBinary" DllEntry="SaveUserInfo" Execute="deferred" Impersonate="no" />
        <CustomAction Id="CustomAction51" Property="RegistrationInfoCustomAction" Value="INSTALLFOLDER=[INSTALLFOLDER]" />
    
        <InstallExecuteSequence>
          <Custom Action="CustomAction51" Before='InstallFinalize' />
          <Custom Action='RegistrationInfoCustomAction' After='CustomAction51'>NOT Installed</Custom>
        </InstallExecuteSequence>
    
        <Directory Id="TARGETDIR" Name="SourceDir">
                <Directory Id="ProgramFilesFolder">
            <Directory Id="INSTALLFOLDER" Name="WixTesterSetup">
              <Component Feature="Core">
                <File Id="WixTester.exe" Source="$(var.WixTester.TargetPath)" KeyPath="yes" Checksum="yes"/>
              </Component>
            </Directory>
                </Directory>
            </Directory>
    
      </Product>
    
    </Wix>
    

    自定义操作现在看起来像:

        using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Net.Mime;
    using System.Text;
    using System.Windows.Forms;
    using Microsoft.Deployment.WindowsInstaller;
    
    namespace RegistrationInfoCustomAction
    {
        public class CustomActions
        {
            [CustomAction]
            public static ActionResult SaveUserInfo(Session session)
            {
                File.Create(System.IO.Path.Combine(session.CustomActionData["INSTALLFOLDER"], "test.xml"));
    
                return ActionResult.Success;
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-03-31
      • 1970-01-01
      • 2018-03-24
      • 1970-01-01
      • 1970-01-01
      • 2011-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多