The preprocessor in WiX allows extensibilty at a few levels. This sample will demonstrate how to add a PreprocessorExtension to your WixExtension that will handle variables and functions you define in your own namespace.
This sample assumes you have already reviewed the Creating a Simple Extension topic.
using Microsoft.Tools.WindowsInstallerXml;
public class SamplePreprocessorExtension : PreprocessorExtension
private SamplePreprocessorExtension preprocessorExtension; public override PreprocessorExtension PreprocessorExtension { get { if (this.preprocessorExtension == null) { this.preprocessorExtension = new SamplePreprocessorExtension(); } return this.preprocessorExtension; } }
private static string[] prefixes = { "sample" }; public override string[] Prefixes { get { return prefixes; } }
public override string GetVariableValue(string prefix, string name) { string result = null; // Based on the namespace and name, define the resulting string. switch (prefix) { case "sample": switch (name) { case "ReplaceMe": // This could be looked up from anywhere you can access from your code. result = "replaced"; break; } break; } return result; } public override string EvaluateFunction(string prefix, string function, string[] args) { string result = null; switch (prefix) { case "sample": switch (function) { case "ToUpper": if (0 < args.Length) { result = args[0].ToUpper(); } else { result = String.Empty; } break; } break; } return result; }
You can now pass your extension on the command line to Candle and expect variables and functions in your namespace to be passed to your extension and be evaluated. To demonstrate this, try adding the following properties to your WiX source file:
<Property Id="VARIABLETEST" Value="$(sample.ReplaceMe)" /> <Property Id="FUNCTIONTEST" Value="$(sample.ToUpper(uppercase))" />The resulting .msi file will have entries in the Property table with the values "replaced" and "UPPERCASE" in the Property table.