msiexecプロパティをWiX C#カスタムアクションに渡すにはどうすればよいですか?

StackOverflow https://stackoverflow.com/questions/835624

質問

Wxs 3.0で作成されたMSIファイルがあります。私のMSIは、新しい C#カスタムアクションプロジェクト

カスタムアクションにルーティングされる引数をmsiexecに渡したい-例:

msiexec / i MyApp.msi ENVIRONMENT = TEST#

.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>

私のC#カスタムアクションは次のように設定されています:

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

}

次のようにプロパティにアクセスできると期待していました:

string environmentName = session.Property [&quot; ENVIRONMENT&quot;];

しかし、これは機能していないようです。

カスタムアクションでmsiexecに渡したプロパティにアクセスするにはどうすればよいですか

役に立ちましたか?

解決

代わりに

<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"];

他のヒント

完全を期すために。上記のブログで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引数の解析はかなり見苦しいですが、動作します。誰かがもっとエレガントな方法を知っているといいのですが。

ここに私の作業コードがあります:

<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"];
    ...
}

InstallFilesの後に実行するには、カスタムアクションを遅延カスタムアクションにする必要があります。遅延カスタムアクションはプロパティにアクセスできませんが、CustomActionDataにはアクセスできます。 このブログ投稿で、どうすればいいのかを議論します。 (この例はVBScriptカスタムアクションですが、session.CustomActionDataコレクションを介して値を取得できます。)

Wix Sharpについて話している場合(XMLを使用した単純なWixではありません)、カスタムプロパティの追加は簡単です。必要なのは、管理アクションの UsesProperties プロパティを設定することだけです。

たとえば、&quot; MYPROP &quot;という名前のカスタムプロパティを追加する場合は、次のようにアクションを定義します。

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;
}

コマンドラインでプロパティが設定されていない場合、デフォルト値は空の文字列になります。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top