49

I came across a situation in which I would like to set the Prefer 32-bit to true. But in Visual Studio 2012, it is showing as disabled. And no matter what I do it doesn't get enabled.

I read that Any CPU with Prefer 32-bit is the default value for all new projects. So, should I assume that although it is disabled it is set to true? Then how can you set it to be just Any CPU?

In What AnyCPU Really Means As Of .NET 4.5 and Visual Studio 11 and in many other posts and questions here in Stack Overflow it says:

overall, there are now five options for the /platform C# compiler switch: x86, Itanium, x64, anycpu, and anycpu 32bit preferred

What's more, I have some collegues that have mentioned to me that in the past that checkbox was enabled (maybe they are wrong?).

Why is the Prefer 32-bit checkbox disabled, and how can I enable it again?

My processor is 64-bit, and I have applied the Update2 to Visual Studio 2012.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Dzyann
  • 5,062
  • 11
  • 63
  • 95
  • 2
    1) Which framework version does the project target? I guess it's not available if you target older versions. 2) What kind of project is it? A class-library or an executable? For class-libraries preferring an architecture isn't really useful. – CodesInChaos May 14 '13 at 15:16

3 Answers3

60

Assuming you've got an executable project, then if you change your target platform to .NET 4.5, it should become enabled.

It's a .NET 4.5-only thing, and it's only enabled for executables.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Matthew Watson
  • 104,400
  • 10
  • 158
  • 276
  • 31
    It's also an "executable project" thang. So it's disabled for class libraries. – Jon Skeet May 14 '13 at 15:18
  • 1
    Ah yes indeed, I'll add that. – Matthew Watson May 14 '13 at 15:19
  • Thanks! I have been moving around so much in the projects, that I never noticed that it would get enabled just then:) – Dzyann May 14 '13 at 15:55
  • 2
    Horrible they set this by default with no Tools setting to turn it off. Even worse, you can't search for it since not in the csproj files unless turned off! – Dave Oct 31 '13 at 16:13
  • Probably added because of the Office Automation incompatibilities with CPUAny on a x64 machine with most folks installing 32 bit Office. – Dave Oct 31 '13 at 16:17
  • 1
    Web applications also count as DLLs not .exe, so the checkbox is disabled for those. – East of Nowhere Oct 15 '14 at 16:01
  • 2
    You can manually edit the .csproj file and find the tag, if the project is behaving like it's set but the checkbox is grayed out. You can change it to false, or delete that tag and it defaults to false. – East of Nowhere Oct 15 '14 at 16:02
  • I imagine this is why the option is default: http://blogs.msdn.com/b/rmbyers/archive/2009/06/08/anycpu-exes-are-usually-more-trouble-then-they-re-worth.aspx – Grault Dec 02 '15 at 20:48
  • What's the cyclomatic complexity of the algorithm that tells me what AnyCPU actually does? – P.Brian.Mackey Apr 19 '18 at 20:48
4

In case anybody encounters this for a .NET 4.5 executable in Visual Studio 2013 (and possibly Visual Studio 2012):

We solved it by switching the project to .NET 4.0 and then back .NET 4.5 (on the Build tab of the project properties). This reenabled the checkbox "Prefer 32 bit" for AnyCPU.

Ron Deijkers
  • 2,791
  • 2
  • 22
  • 28
2

This setting may also be disabled if your project is a unit test project. It turns out the setting wasn't my issue anyway. I got tests running by changing the Visual Studio test runner to run tests as a 64-bit process.

In Visual Studio Community 2015, I found this in the Test menu:
Test > Test Settings > Default Processor Architecture > X64

Here's more info from Microsoft:
https://msdn.microsoft.com/en-us/library/ee782531.aspx

NJS
  • 426
  • 4
  • 15