My question is simple: For what architecture does the .net compiler target the resulting exe? I refer to the actual exe-file, not the CLR that is loaded by the binary to execute the actual code itself. And there has to be a target cpu architecture, as the PE-Header of executables can't be platform agnostic (to my knowledge).
Why does this binary work everytime especially when looking at x64 vs IA64?
Edit: I just looked at the hex code of some x86, x64 and AnyCPU builds. For x86 and AnyCPU the target is x86, for x64 it is x64.
Now remain two questions: How does the AnyCPU change it's target architecture at runtime?? Because the PE-Header clearly says it is targeted for x86, but the program then launches as a x64 binary.
And secondly, how can a x64 exe throw a BadImageFormatException (which is clearly a CLR thing), when it can't even be executed?
Edit 2: https://msdn.microsoft.com/en-us/library/8dkk3ek4(v=vs.80).aspx describes some details that only confuse me further. It states, that the CLR reads PE-Files to run them, which makes sense. It would explain why a x86 PE-File can be launched as a x64 process.
But thats about it. How does the CLR launch with the correct process name, binary path and stuff? If the PE launches & hosts the CLR, that would be explained, but the x86-x64 change would be a mystery again. But nevertheless the BadImageFormatException is something I can't explain with those information.