Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
83 views
in Technique[技术] by (71.8m points)

c# - How to pass parameters to the custom action?

I'm trying to create a custom action with "Value" attribute, I want to pass parameters to the C# code (the TARGETDIR and the version).

However, I get an error stating that DLLENtry and Value cannot coexist. But the custom action without dllentry is invalid.

This is the code:

 <CustomAction Id="SetMAWPrefferences"
                Value="InstallDir=[TARGETDIR];Version=2.0.0.1"
                Return="check"
                Execute="commit"
                BinaryKey="ImportExportBinary"                    
                />

And for it I get this error:

Error 9 ICE68: Invalid custom action type for action 'SetMAWPrefferences'.

Any ideas how to do it?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There are two ways to pass parameters to the custom actions, one will work for the immediate execution CA and the other one will work for the deferred custom actions.

Immediate CA (Can't be rolled back):

In order to pass arguments to the immediate CA you can set a property with the required name and access it from your session.

In Wix:

<Property Id="MyProp" Value="MyValue" />

In CA:

[CustomAction]
public static ActionResult NameOfMyCA(Session session)
{
    string myArg = session["MyProp"];
}   

Deferred CA:

In order to pass arguments to the deferred CA you need to use the CustomActionData Property, this property is the only one you can access from a deferred CA.

In the case of WIX, the DTF includes a CustomActionData class which is a key/value dictionary, and you can access it using:

In Wix:

<CustomAction Id="MyCustomAction" .../>

<Property Id="MyCustomAction" Value="Arg1=value1;Arg2=value2;Arg3=value3;Arg4=[MyProperty]" />

In CA:

[CustomAction]
public static ActionResult NameOfMyCA(Session session)
{
    CustomActionData data = session.CustomActionData;

    //Access each argument like this:

    string arg1 = data["Arg1"];
    string arg2 = data["Arg2"];
    string arg3 = data["Arg3"];
}    

Immediate CA + CustomActionData:

If you want to use the CustomActionData for your Immediate CA you can do something like this:

In Wix:

<Property Id="MyCustomAction" Value="Arg1=value1;Arg2=value2;Arg3=value3;Arg4=[MyProperty]" />

In CA:

[CustomAction]
public static ActionResult NameOfMyCA(Session session)
{
    CustomActionData data = new CustomActionData(session["MyCustomAction"]);

    //Access each argument like this:

    string arg1 = data["Arg1"];
    string arg2 = data["Arg2"];
    string arg3 = data["Arg3"];
    string arg4 = session.Format(data["Arg4"]);
}

In the case of Arg4 since it contains the value of a property you will need to access it like this:

string arg4 = session.Format(data["Arg4"]);

Unfortunately this will work in immediate CA only, this means that if you want to use the value of this property in a deferred CA you will need to have two custom actions:

  • CA 1 to set the CustomActionData for the CA executed as immediate. (Remember to name the property with the same name defined for your CustomAction.

  • CA 2 the CA with the specific logic that consumes CustomActionData.

I suggest you to use the CustomActionData for all the cases, this way is easier to convert you CA from Immediate to Deferred and the code is easier to read.

References:

session.Format CustomActionData


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...