Writing a cross-platform (32-bit and 64-bit compatible) program for Windows (e.g., AnyCPU in .NET)

I was puzzled by how the "AnyCPU" function works in .NET: it loads the executable file as a native 32-bit if the system is 32-bit and 64-bit if the system is 64-bit (which you can easily confirm with the task manager). Therefore, obviously, this is not possible.

The question is, how exactly did Microsoft do it? Windows did not initially know about the .NET platform, so the Windows PE loader cannot look for any additional features in the PE headers for the CLR header; this function must be added by some kind of kernel extension. But the .NET framework doesn’t seem to install such a thing ... I will completely lose the way the same executable can be native 32-bit and 64-bit at the same time, especially since disassembling mscoree.dll doesn’t even show links to undocumented native functions.

Does anyone have any knowledge and / or reasonable assumptions about how this was done? Obviously, this is possible (so things like “this is not possible” do not say), and it makes me want to write my own cross-platform EXE ...




Edit:

As a note, note how you cannot run 32-bit executables in a 64-bit Windows PE environment ... there has to be some way to extend or change the PE loader with some kind of “plugins”, right?

+8
cross-platform winapi executable portable-executable
Jan 10 '11 at 3:28
source share
2 answers

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

Windows did not initially know about the .NET platform

In fact, starting with Windows XP, Windows IS knows the .NET executable format . And XP was the first version of Windows to support 64-bit versions.

Thus, the PE header is marked 32-bit, and its own import table refers to 32-bit mscoree , which in Windows 2000 and earlier causes 32-bit .NET to load. DllMain for mscoree runs the application JIT and changes the entry point for the main application.

Windows XP and later, knowing about .NET metadata, recognizes that it is AnyCPU and loads the corresponding infrastructure.

This is probably more than you wanted to know about the process .

So no, no native AnyCPU exe. Although you can embed a 16-bit DOS program into a 32-bit PE, you cannot have a combined 32-bit and 64-bit .exe

+5
Jan 10 '11 at 4:10
source share

In fact, you can have x64 code in the x86 executable if the system has a WOW64 emulation level (Windows Vista +, dunno about XP). http://vxheavens.com/lib/vrg02.html

I tested this method and it works on Win7 as well as on WinVista. I wrote a little stub in the assembly to allow import and loading of some C code that was compiled for AMD64.

Microsoft just says you can't go back and forth because they can change it, but I doubt it will change until the underlying architecture changes, so that something like 128 bits starts too ... at that moment I I doubt that WOW64 will be around :), WOW128 ftl.

+4
Mar 04 '11 at 19:45
source share



All Articles