Writing a Cross-Platform (32-bit and 64-bit compatible) Program for Windows (like AnyCPU in .NET)

前端 未结 2 1842
庸人自扰
庸人自扰 2021-01-05 04:45

It\'s been baffling me how the \"AnyCPU\" feature in .NET works: It loads the executable as native 32-bit if the system is 32-bit, and as 64-bit if the system is 64-bit (whi

2条回答
  •  旧时难觅i
    2021-01-05 05:26

    Your question is based on a misunderstanding. Here's the mistake:

    Windows originally didn't know about the .NET framework

    Actually, since Windows XP, Windows IS aware of the .NET executable format. And XP was the first version of Windows to support 64-bits.

    So the PE header is marked 32-bit and the native import table references the 32 bit mscoree, which on Windows 2000 and earlier, causes 32-bit .NET to be loaded. DllMain for mscoree starts JITting the application code and modifies the entrypoint for the main application.

    Windows XP and later, being aware of the .NET metadata, recognizes that it is AnyCPU and loads the appropriate framework.

    Here's probably more than you ever wanted to know about the process.

    So no, there is no native AnyCPU exe. Although you can embed a 16-bit DOS program in a 32-bit PE, you can't have a combined 32-bit and 64-bit .exe

提交回复
热议问题