代码之家  ›  专栏  ›  技术社区  ›  David Laing

如何将msiexec属性传递给wix c自定义操作?

  •  27
  • David Laing  · 技术社区  · 16 年前

    我有一个使用WXS 3.0创建的msi文件。我的MSI引用了一个C自定义操作,使用新的 C# Custom Action project .

    我想将一个参数传递给msiexec,该参数将被路由到我的自定义操作-例如:

    msiexec/i myapp.msi environment=测试#

    在.wxs文件中,我这样引用自定义操作:

    <Property Id="ENVIRONMENT"/>
    <Binary Id="WixCustomAction.dll"  SourceFile="$(var.WixCustomAction.Path)" />
    <CustomAction Id="WixCustomAction" BinaryKey="WixCustomAction.dll"    DllEntry="ConfigureSettings"/>
    <InstallExecuteSequence>
       <Custom Action="WixCustomAction" After="InstallFiles"></Custom>
    </InstallExecuteSequence>
    

    我的自定义操作设置如下:

    [CustomAction]
    public static ActionResult ConfigureSettings(Session session)
    {
    
    }
    

    我希望能够访问这样的财产:

    字符串environmentname=session.property[“environment”];

    但这似乎行不通。

    如何访问在自定义操作中传递给msiexec的属性?

    5 回复  |  直到 9 年前
        1
  •  30
  •   liwp    12 年前

    如果不是

    <CustomAction Id="SetCustomActionDataValue"
                  Return="check"
                  Property="Itp.Configurator.WixCustomAction"
                  Value="[ENVIRONMENT],G2,[CONFIGFILE],[TARGETDIR]ITP_v$(var.VERSION_MAJOR)" />
    

    你写这个:

    <CustomAction Id="SetCustomActionDataValue"
                  Return="check"
                  Property="Itp.Configurator.WixCustomAction"
                  Value="Environment=[ENVIRONMENT];G=G2;ConfigFile=[CONFIGFILE];TargetDir=[TARGETDIR]ITP_v$(var.VERSION_MAJOR)" />
    

    然后您将能够引用这样的变量:

    string env=session.CustomActionData["Environment"];
    
        2
  •  14
  •   Peter Mortensen Pieter Jan Bonestroo    10 年前

    只是为了完整性;使用Jeremy Lew在上面的博客中描述的方法可以实现以下目的:

    打电话:

    msiexec /i ITP.Platform.2.msi ENVIRONMENT=QA CONFIGFILE=EnvironmentConfig.xml
    

    在.wxs文件中包含此内容:

    <Property Id="ENVIRONMENT" Secure="yes" />
    <Property Id="CONFIGFILE" Secure="yes" />
    <Binary Id="Itp.Configurator.WixCustomAction.dll"
            SourceFile="$(var.Itp.Configurator.WixCustomAction.Path)" />
    
    <CustomAction Id="SetCustomActionDataValue"
                  Return="check"
                  Property="Itp.Configurator.WixCustomAction"
                  Value="[ENVIRONMENT],G2,[CONFIGFILE],[TARGETDIR]ITP_v$(var.VERSION_MAJOR)" />
    
    <CustomAction Id="Itp.Configurator.WixCustomAction"
                  Return="check"
                  Execute="deferred"
                  BinaryKey="Itp.Configurator.WixCustomAction.dll"
                  DllEntry="ConfigureItpBrandSettings" />
    
    <InstallExecuteSequence>
      <Custom Action="SetCustomActionDataValue" After="InstallFiles"></Custom>
      <Custom Action="Itp.Configurator.WixCustomAction" After="SetCustomActionDataValue"></Custom>
    </InstallExecuteSequence>
    

    使用自定义操作:

        /// <summary>
        /// CustomAction keys should be Environment,BrandId,ConfigPath,itpBasePath
        /// </summary>
        /// <param name="session"></param>
        /// <returns></returns>
        [CustomAction]
        public static ActionResult ConfigureItpBrandSettings(Session session)
        {
            string[] arguments = GetCustomActionDataArguments(session);
    
            string environmentName = arguments[0];
            string brandId = arguments[1];
            string configPath = arguments[2];
            string itpBasePath = arguments[3];
    
            //Do stuff
    
            return ActionResult.Success;
        }
    
        private static string[] GetCustomActionDataArguments(Session session)
        {
            string[] keys = new string[session.CustomActionData.Keys.Count];
            session.CustomActionData.Keys.CopyTo(keys,0);
            return keys[0].Split(',');
        }
    

    作品。

    解析customActionData参数是非常难看的,但它确实有效。希望有人知道更优雅的方法。

        3
  •  8
  •   Dave Andersen    11 年前

    这是我的工作代码:

    <Binary Id="MyCA" SourceFile="..\bin\ChainerRun.CA.exe" />
    
    <CustomAction Id="SetCustomActionDataValue" Return="check" Property="CustomActionData" Value="TARGETDIR=[TARGETDIR];AA=Description;" />
    
    <CustomAction Id="ReadAndSet" 
                BinaryKey="MyCA" 
                DllEntry="ReadAndSet" 
                Execute="immediate"
                HideTarget="no" 
                Return="check" />
    
    <InstallExecuteSequence>
        <Custom Action="SetCustomActionDataValue" Before="InstallFiles" />
        <Custom Action="ReadAndSet" After="SetCustomActionDataValue" />
    </InstallExecuteSequence>
    

    在C自定义操作功能中:

    [CustomAction]
    public static ActionResult ReadAndSet(Session session)
    {
        ActionResult retCode = ActionResult.NotExecuted;
    
        System.Diagnostics.Debug.Assert(false);
    
        session.Log("ReadAndSet() begins ...");
    
        string installLocation = session.CustomActionData["TARGETDIR"];
        string hostName = session.CustomActionData["AA"];
        ...
    }
    
        4
  •  8
  •   Peter Mortensen Pieter Jan Bonestroo    10 年前

    您的自定义操作需要是延迟的自定义操作,以便在安装文件之后运行。延迟的自定义操作不具有对属性的访问权限,但它们具有对CustomActionData的访问权限。见 this blog post 讨论如何处理它。(此示例是一个vbscript自定义操作,但您可以通过session.customActionData集合检索该值。)

        5
  •  0
  •   Funbit    9 年前

    如果我们谈论的是wix-sharp(而不是简单的wix及其XML内容),那么添加自定义属性是小菜一碟。你所要做的就是设定 使用特性 托管操作的属性。

    例如,如果要添加名为“”的自定义属性, 麦道普 “,只需这样定义您的操作:

    new ElevatedManagedAction(nameof(CustomActions.MyCustomAction))
    {
        Condition = Condition.Installed,
        When = When.Before,
        Step = Step.RemoveFiles,
        Return = Return.check,
        Execute = Execute.deferred,
        UsesProperties = "MYPROP"
    }
    

    通过msiexec命令行设置属性值:

    msiexec /i my.msi MYPROP=MYVALUE
    

    然后您就可以从自定义操作访问它了:

    [CustomAction]
    public static ActionResult MyCustomAction(Session session)
    {
        session.Log("MYPROP VALUE: " + session.CustomActionData["MYPROP"]);
        return ActionResult.Success;
    }
    

    如果未通过命令行设置属性,则默认值将为空字符串。

    推荐文章