5

I've studied the information about building apps with a different target platforms options in visual studio, but I still can't understand the following things:

when do we need to set x86 or x64 target?
what advantages gives us setting a specific target platform (x86 or x64) over setting 'any cpu'?
isn't it easier always to set 'any cpu'?

user1178399
  • 1,028
  • 8
  • 17
  • 32
  • 2
    For example when you need to link to native code module which is only 32 or 64bit you need to force the application to run in that mode. – Sami Kuhmonen Jun 20 '18 at 14:50
  • 1
    I like Scott Hanselmans blog on this: https://www.hanselman.com/blog/BackToBasics32bitAnd64bitConfusionAroundX86AndX64AndTheNETFrameworkAndCLR.aspx – JayV Jun 20 '18 at 14:53

1 Answers1

3

There are three options in the platfrom target as of now. x86, x64, Any CPU. To complicate thing even worse, there is a checkbox called "Prefer 32bit".

When you are developing applications you have to make sure its Any CPU is Enabled and Prefer 32bit is disabled for maximum compatibility.

However some times you will be using native calles to platfrom specific APIs (eg: you are calling a 32bit native dll). Then enabling Any CPU will run your application as 64bit process in a 64bit operating system. This will indroduce exceptions in the runtime. You should be targeted to x86 platform. Same goes for 64 bit native calles, you should target it as x64 only.

Lets see why Prefer 32bit is there. Windows has a new target type called ARM (Windows 8 ARM 32bit as of the date Prefer 32bit is introduced). When this option is enabled and Any CPU is selected. A .NET application compiled to x86 will fail to run on an ARM Windows system, but an “Any CPU 32-bit preferred” application will run successfully.

Going forwards disable Prefer 32bit since nobody used Windows 8 ARM. Windows 10 ARM have resolved this issue.

Vibeeshan Mahadeva
  • 7,147
  • 8
  • 52
  • 102