如何使用 WiX 将 CustomActionData 传递给 CustomAction?

问题描述 投票:0回答:3

如何在 CustomActionData 上设置属性以通过延迟的自定义操作检索?

wix windows-installer wix3.5 custom-action
3个回答
154
投票

延迟的自定义操作不能直接访问安装程序属性(reference)。其实只有

CustomActionData
属性

session.CustomActionData

以及 here 列出的其他方法和属性在会话对象上可用。

因此,对于延迟自定义操作来检索诸如

INSTALLLOCATION
之类的属性,您必须使用类型 51 自定义操作(即设置属性自定义操作)来传递该信息,并且您将使用来自CustomAction 的 C# 代码通过
session.CustomActionData
。 (参见参考参考

下面是一个类型 51 自定义操作 (

CustomAction1
) 的示例,它将设置一个可以在
CustomAction2
中检索的属性。

<CustomAction Id="CustomAction1"
              Property="CustomAction2"
              Value="SomeCustomActionDataKey=[INSTALLLOCATION]"
/>

注意

Property
属性名称是
CustomAction2
。这个很重要。 类型 51 操作的 Property 属性的值必须等于/相同于正在使用的自定义操作的名称
CustomActionData
.
(参见reference

注意到

SomeCustomActionDataKey
属性键/值对中的名称
Value
?在使用自定义操作 (
CustomAction2
) 的 C# 代码中,您将使用以下表达式从
CustomActionData
中查找该属性:

string somedata = session.CustomActionData["SomeCustomActionDataKey"];

用于从

CustomActionData
检索值的键不是类型 51 自定义操作的
Property
属性中的值,而是来自
key=value
属性中的
Value
对的键。 (重要信息:
CustomActionData
是通过设置与使用自定义操作的 Id 同名的安装程序属性来填充的,但是
CustomActionData
键不是安装程序属性。
)(参见 reference

在我们的场景中,消费自定义操作是一个延迟的自定义操作,定义如下:

<Binary Id="SomeIdForYourBinary" SourceFile="SomePathToYourDll" />
<CustomAction Id="CustomAction2"
              BinaryKey="SomeIdForYourBinary"
              DllEntry="YourCustomActionMethodName"
              Execute="deferred"
              Return="check"
              HideTarget="no"
/>

配置安装执行顺序

当然,消费自定义操作(

CustomAction2
)必须在类型51自定义操作(
CustomAction1
)之后运行。所以你必须像这样安排他们:

<InstallExecuteSequence>
  <!--Schedule the execution of the custom actions in the install sequence.-->
  <Custom Action="CustomAction1" Before="CustomAction2" />
  <Custom Action="CustomAction2" After="[SomeInstallerAction]" />      
</InstallExecuteSequence>

11
投票

对于我们的 C++ schlubs,您可以按如下方式检索属性:

MsiGetProperty(hInstall, "CustomActionData", buf, &buflen);

然后你解析'buf'。感谢Bondbhai


5
投票

如果传递给自定义操作的值不是键/对集...

<SetProperty Id="CustomAction1" Before="CustomAction1" Value="data" Sequence="execute"/>
<CustomAction Id="CustomAction1" BinaryKey="BinaryId" DllEntry="MethodName" Execute="deferred"/>

...然后可以使用以下方法检索整个 blob:

string data = session["CustomActionData"];
© www.soinside.com 2019 - 2024. All rights reserved.