views:

24

answers:

1

I've always been curious as to how one extension, EXE, can be as versatile as it is in that if you assemble an assembly program, you get an EXE in machine code for your processor but if you compile a C# or other .Net program, you also get an EXE except that it is run in the proper runtime environment. I'm not sure if this is different from OS to OS (I imagine it is), but when an EXE is executed, how is it determined how to execute it?

On a related note, if I were writing my own programming language, how would I tie in my runtime environment into this mechanism?

+3  A: 

When compiling a .NET program to an EXE, it's more than just a blob of bytecode (like Java). There's actually native executable created that will load the .NET runtime and hand off the .NET bytecode to it, or display a friendly-ish error message indicating that the framework is not available.

The format is even more flexible than that, as every Windows EXE actually includes a DOS program at the beginning which will display an error ("cannot run in DOS mode") when executed as a DOS program.

You can read more details on the PE format on Wikipedia: http://en.wikipedia.org/wiki/Portable_Executable

Bob
To add a detail to your nice answer: At least in the case of the error message (`Cannot run in DOS mode.`), the DOS program included in the executable file was known as a _stub_.
stakx