5

I am writing a C# application using Visual Studio 2015. This application targets "Any CPU" (without the "Prefer 32-bit" option enabled), meaning that the application compiles to a single build target that will run in 32-bit mode on 32-bit operating systems and 64-bit mode on 64-bit operating systems.

This application requires that a certain native DLL be copied to its output folder (i.e., the bin/Debug or bin/Release folder). There are separate x86 and x64 versions of this DLL, and the correct one needs to be copied to the output folder depending on the developer's operating system.

So far I've figured out that I can copy files to the output folder conditionally by adding something like the following to my .csproj file:

<ItemGroup Condition="MY CONDITION HERE">
    <Content Include="MyNativeLib.dll">
        <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </Content>
</ItemGroup>

So my question is, how do I write a condition equivalent to "developer's operating system is x86" or "... x64"?

(To be super clear, I'm not asking how to copy a file conditionally upon the platform build target, which in my case is always "Any CPU". I'm asking how to copy a file conditionally depending on the OS architecture on which Visual Studio happens to be running.)

Walt D
  • 4,491
  • 6
  • 33
  • 43
  • I would properly just do it in a post build event. – Allan S. Hansen May 23 '16 at 05:37
  • @AllanS.Hansen Yeah, that was my first instinct (I already use post-build to copy a bunch of other files); I just couldn't find any tips at all about copying files *conditionally* in post-build events, nevermind conditional upon OS architecture. If anyone can tell me how to do this in a post-build event, that'd be 100% acceptable! – Walt D May 23 '16 at 05:44
  • Have a look at this: http://stackoverflow.com/questions/3832552/conditionally-use-32-64-bit-reference-when-building-in-visual-studio – Alex May 23 '16 at 05:45
  • @Alex Thanks! The $(PROCESSOR_ARCHITECTURE) variable looks promising. I'll look into that! – Walt D May 23 '16 at 05:50
  • Please keep us posted on your progress. – Alex May 23 '16 at 06:24
  • I figured out the solution and posted below. Thanks for your help! – Walt D May 23 '16 at 14:51

1 Answers1

2

Thanks to a couple helpful comments to the original question above that pointed me in the right direction, I've figured out how to solve this:

I decided to copy the file in a post-build event and used batch script commands to check the PROCESSOR_ARCHITECTURE and PROCESSOR_ARCHITEW6432 environment variables. (More info on these variables here.)

Here's an example of how to do this in the post-build events:

set isX64=FALSE
if "%PROCESSOR_ARCHITECTURE%"=="AMD64" set isX64=TRUE
if "%PROCESSOR_ARCHITEW6432%"=="AMD64" set isX64=TRUE
if "%isX64%"=="TRUE" (
    echo "Copying x64 dependencies..."
    copy "$(ProjectDir)Dependencies\x64\MyNativeLib.dll" "$(TargetDir)"
) ELSE (
    echo "Copying x86 dependencies..."
    copy "$(ProjectDir)Dependencies\x86\MyNativeLib.dll" "$(TargetDir)"
)

Presumably I could also use these environment variables in the .csproj file as I thought about doing in the original question, but doing it in the post-build event seemed a bit easier and clearer to me, and I was already using post-build to copy some other files.

Walt D
  • 4,491
  • 6
  • 33
  • 43