I've made a NuGet package that has managed and unmanaged code. The managed code is written in C# and supports AnyCPU
while the unmanaged code is in C++ and has a 32-bit and 64-bit version. The managed code in this package is dependent on the unmanaged code. The structure of my package looks like this:
\build
\x86
unmanaged.dll
\x64
unmanaged.dll
package.targets
\lib
\net472
managed.dll
Within my '.targets' file, I'm checking the $(Platform)
variable to see if the user has selected x86
, x64
, or AnyCPU
and copy the correct unmanaged dll to the output directory:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Condition=" '$(Platform)' == 'x64' ">
<Content Include="$(MSBuildThisFileDirectory)x64\unmanaged.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Link>unmanaged.dll</Link>
</Content>
</ItemGroup>
<ItemGroup Condition=" '$(Platform)' == 'x86' OR '$(Platform)' == 'AnyCPU' ">
<Content Include="$(MSBuildThisFileDirectory)x86\unmanaged.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Link>unmanaged.dll</Link>
</Content>
</ItemGroup>
</Project>
The expected behavior is when a user builds their program, the managed dll is copied over to the ouput directory with the correct umanaged dll according to the selected architecture type.
Everything works as intended when the user selects x86
or x64
, but not when AnyCPU
is chosen. This is because if the user has '32-bit preferred' off and the 32-bit unmanaged dll is copied to the output directory, an exception is raised saying that the format of the 32-bit unmanaged dll doesn't match the process' architecture, which is obvious. I want to modify my '.targets' file so it can recognize if '32-bit preferred' is toggled on or off and copy the correct unmanaged dll to the output directory when the program is being built. Is there a predefined Visual Studio variable that checks if '32-bit preferred' is toggled? If not, is there another way of checking the '32-bit preferred' option within the '.targets' file?