1

I have a 64bit operating system and an x64-based processor. However, when I compile a visual studio project, with "Any CPU" selected, it compiles as a 32bit executable?

So what is going on here and why isn't it compiling as 64bit?

The reason why I need to compile in 64bit is because I want to interact with a 64bit unmanaged C++ dll.

greenbeast
  • 364
  • 2
  • 8
  • 21
  • 1
    Note that when you're calling unmanaged code, you need to either compile the .NET code to the specific bitness of the external code, or provide both 32 and 64 bits libraries for "Any CPU" .NET binaries. – Alejandro Apr 10 '18 at 20:30
  • @greenbeast: may i ask why don't you accept my answer? – Alexander Powolozki Apr 11 '18 at 13:58
  • Alexander, I do accept your answer (and why I marked it up- thank you). I want to tick both responses too but I can only choose one. So, because Francesco gave a more detailed/descriptional answer (that also referred to your answer) I felt it was the better one to choose. – greenbeast Apr 11 '18 at 15:16

2 Answers2

2

There is a "Prefer 32-bit platform" checkbox on project build configuration page, you should uncheck it.

2

The answer to your question is yes, since .NET 4.5 and Visual Studio 2012, AnyCPU will compile to x86 by default even on 64 bit OSs. The pointer to this is actually what @Alexander Powolozki said in his answer, e.g. the "Prefer 32 bit" flag.

Quoting from this answer

If the process runs on a 64-bit Windows system, it runs as a 32-bit process. IL is compiled to x86 machine code

The reason for this setting maybe be that this default helps you with performance, since

When an application can run fine either in 32-bit or 64-bit mode, the 32-bit mode tends to be a little faster. Larger pointers means more memory and cache consumption, and the number of bytes of CPU cache available is the same for both 32-bit and 64-bit processes. Of course the WOW layer does add some overhead, but the performance numbers I've seen indicate that in most real-world scenarios running in the WOW is faster than running as a native 64-bit process

Even Visual Studio isn't 64bit for this reason (and the cost of porting all libraries to 64 bit).

So, unless you uncheck the "prefer 32 bit flag", if you don't have it yet and as you probably know, you should create the x64 platform on your Visual Studio and use it for this particular project. Get to this window, clicking on the "Any CPU" ComboBox:

enter image description here

And add the x64 bit platform:

enter image description here

Those images are for an older version of Visual Studio, but it works in a similar fashion across all versions.

Here's a written description.

Francesco B.
  • 2,729
  • 4
  • 25
  • 37
  • Thank you Francesco for your response. But I am running a 64-bit system and according to [this](https://stackoverflow.com/questions/5229768/c-sharp-compiling-for-32-64-bit-or-for-any-cpu) I should not have to set this. "Any CPU" should automatically set it to 64 bit (but it doesn't). When I do set it to a 64 bit platform, I get the windows command console response (which I am still trying to figure out why) – greenbeast Apr 10 '18 at 22:12
  • Even visual studio isn't 64bit... Lol, I didn't think about that – Francesco B. Apr 11 '18 at 05:21