Search code examples
wixwindows-installervisual-studio-2022wix4

WiX Toolset Use User-defined variables


I'd like to use User-defined variables in my WiX Toolset Project. If I try to use them, the building process will fail and I get the Error Message WIX0150 Undefined preprocessor variable '$(MyVariable)'.

I followed the documention from WiX Toolset itself: https://wixtoolset.org/docs/tools/preprocessor/

My project looks like this:

wixproj-file

<Project Sdk="WixToolset.Sdk/4.0.3">
    <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
        <DefineConstants>
            <?define MyVariable = "Filepath" ?>
        </DefineConstants>
    </PropertyGroup>
</Project>

Component-file

<?xml version="1.0" encoding="UTF-8"?>
<Wix xmlns="http://wixtoolset.org/schemas/v4/wxs">
    <Fragment>
        <Component Id="HandlerCmp" Directory="INSTALLFOLDER">
            <File Source="$(MyVariable)" />
        </Component>
    </Fragment>
</Wix>

Solution

  • So I stumpled in the projects properties over the solution. Under Project -> Properties -> Build you can define Preprocessor variables. This will create an DefineConstants entry in the wixproj file like the follow one:

    <Project Sdk="WixToolset.Sdk/4.0.3">
        <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
            <DefineConstants>MyVariable=Filepath;MyVariable2=Filepath2</DefineConstants>
        </PropertyGroup>
    </Project>
    

    The usage stase the same like descriped in the question:

    <?xml version="1.0" encoding="UTF-8"?>
    <Wix xmlns="http://wixtoolset.org/schemas/v4/wxs">
        <Fragment>
            <Component Id="HandlerCmp" Directory="INSTALLFOLDER">
                <File Source="$(MyVariable)" />
            </Component>
        </Fragment>
    </Wix>