Build platform target AnyCPU EXE still shows 32bit

2019-07-11 05:08发布

问题:

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

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.

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?

回答1:

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.



回答2:

The exe files doesn't contain any 32-bit or 64-bit code, it only contains IL code.

The JIT compiler creates machine code from the IL code, and the platform target determines what kind of machine code the JIT compiler is allowed to create.