'(Wix) heat.exe could not be loaded with msbuild

I have an issue with heat.exe as soon as I build my project in MSBuild. I get this error message:

Unhandled Exception: System.BadImageFormatException: Could not load file or assembly 'file:///C:\Program Files (x86)\WiX Toolset v3.11\bin\Heat.exe' or one of its dependencies. An attempt was made to load a program with an incorrect format.

I have looked up a possible solution on stackoverflow here: Referred links

I've tryed to change my configuration in all sorts of ways but can't get a hold of what is missing.

This is how I have configured right now. I want to be able to target both x64 and x86 platform.

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x64' ">
<OutputPath>..\..\BuildArtifacts\SetupProjects\Myproject</OutputPath>
<IntermediateOutputPath>obj\$(Configuration)\</IntermediateOutputPath>
<DefineConstants>Debug</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x64' ">
<OutputPath>..\..\BuildArtifacts\SetupProjects\Myproject</OutputPath>
<IntermediateOutputPath>obj\$(Configuration)\</IntermediateOutputPath>
<DefineConstants>Release</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
<OutputPath>..\..\BuildArtifacts\SetupProjects\Myproject</OutputPath>
<IntermediateOutputPath>obj\$(Configuration)\</IntermediateOutputPath>
<DefineConstants>Debug</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
<OutputPath>..\..\BuildArtifacts\SetupProjects\Myproject</OutputPath>
<IntermediateOutputPath>obj\$(Configuration)\</IntermediateOutputPath>
<DefineConstants>Release</DefineConstants>
</PropertyGroup>

Any help appreciated,



Solution 1:[1]

Since msbuild runs as a 64bit exe, it will fail to load the 32bit heat.exe. To fix this issue, processes have to run seperately. This can be done by adding this to a PropertyGroup:

<RunWixToolsOutOfProc Condition=" '$(PROCESSOR_ARCHITECTURE)'!='x86' ">true</RunWixToolsOutOfProc>

But thats not enough. Heat does successfully ignore that Property. Instead you have to use the Property RunAsSeparateProcess:

<HeatDirectory
        ....
    RunAsSeparateProcess="$(RunWixToolsOutOfProc)" />

See: https://github.com/wixtoolset/issues/issues/2467#issuecomment-736519622

Solution 2:[2]

I managed to fix this issue by changing wixproj from

<Target Name="BeforeBuild">
  <HeatDirectory Directory="$(MSBuildThisFileDirectory)lib\" PreprocessorVariable="var.HarvestPath" OutputFile=".\clients\sftp\OmsFileServer\SFTPFileServerInstaller\SFTPFileServerInstaller\HeatGeneratedFileList.wxs" ComponentGroupName="HeatGenerated" DirectoryRefId="INSTALLFOLDER" AutogenerateGuids="true" ToolPath="$(WixToolPath)" SuppressUniqueIds="true" SuppressFragments="true" SuppressRegistry="true" SuppressRootDirectory="true"/>
</Target>

to

<PropertyGroup>
  <InstallerPlatform>x64</InstallerPlatform>
  <Platform>x64</Platform>
</PropertyGroup>
<Target Name="BeforeBuild">
  <Exec Command='"$(WIX)bin\heat.exe" dir "$(MSBuildThisFileDirectory)lib" -cg HeatGenerated -dr INSTALLFOLDER -sreg -srd -var var.HarvestPath -ag -sfrag -suid -out "$(MSBuildThisFileDirectory)HeatGeneratedFileList.wxs"'/>
</Target>

I realized because my cake script asks MSBuild-x64 to build the solution, it somehow can't run a 32bit HeatDirectory command, but I'm not sure how exactly Exec works in an x64 pipeline and HeatDirectory doesn't.

Moreover, a 64 bit application definitely can execute a 32 bit app and it's not possible only the other way. But nothing else on the internet worked for me.

Solution 3:[3]

Since i switched to Visual Studio 2022 my command MSBuild c:\project\Setup\Setup.wixproj doesn't work anymore. I solved this by directly calling the 32Bit MSBuild executable:

C:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe c:\project\Setup\Setup.wixproj

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 kuch3n
Solution 2
Solution 3 Marco G