Search code examples
c#visual-studio-2012.net-4.532bit-64bitanycpu

Build platform target AnyCPU EXE still shows 32bit header in 64bit machine


Not sure this has been asked before (I could not find any). I have simple console app/ESE and have the below settings.

enter image description here

I'm running Windows 8, 63bit OS. And the EXE target framework .NET 4.5 However, when I compile this EXE, it still shows as a 32bit EXE.

enter image description here

Since this is "Any CPU", I would expect the EXE to compile as 64bit / PE32+.

Can some please help tell why this would be still 32bit?


Solution

  • You are misinterpreting CorFlags I think. Here is a CorFlags truth table:

    CPU Architecture           PE      32BITREQ   32BITPREF
    ------------------------   -----   --------   ---------
    x86 (32-bit)               PE32           1           0
    x64 (64-bit)               PE32+          0           0
    Any CPU                    PE32           0           0
    Any CPU 32-Bit Preferred   PE32           0           1
    

    As you can see, it will only report PE32+ if you compile it as 64-bit and not as Any CPU. The reason is because the header must be backward compatible. meaning if an assembly is to work in 'Any CPU', both 32 and 64 bit, then the header format must be in a format recognizable by a 32-bit operating system. PE32+ is a 64-bit only header format and if that header was applied to an assembly compiled as Any CPU, then a 32-bit operating system would not recognize the PE32+ header format.