tags:

views:

80

answers:

2

Why Visual Studio compiles an application to an executable if it's in the MSIL format? Shouldn't it be like java that compiles the code to a .class file?

+7  A: 

a .NET "executable" is really a tiny _un_managed stub executable that creates an AppDomain, instantiates your startup .NET object and calls into it.

a .NET dll would be the equivalent of a java .class

Edit: -----

Jb Evain points out in comments that .NET dlls also contain an unmanaged stub. The stub just jumps to the appropriate entrypoint in mscoree.dll that does the actual work. CorExeMain for exe's and CorDllMain for dll's.

John Knoeller
Excellent answer. This is the key difference between a .net executable and class library.
David Pfeffer
Is the AppDomain part of the .NET framework?
Gabriel
@Gabriel: I'm not sure I understand the question, but I think the answer would be yes. the code would reside in mscoree.dll
John Knoeller
Good answer, +1 from me, here is another way of saying it, the EXE from a .NET perspective, is really a small loader that initializes the environment in preparation for the .NET code to run, and more importantly, the code are pre-jitted to make it run as fast.
tommieb75
It's not really exact. The unmanaged part of a managed .exe is executed only on windows 2000 and windows 98. Since Windows XP, Windows's PE loader has the ability to detect a managed executable, and will give it to the the .net CLR. The stub is useless in most of the cases now. Plus it doesn't create any domain or call into a managed object, it just jumps straight to CorExeMain or CorDllMain in mscoree.dll.
Jb Evain
@Jb: Sure WinXP has an optimization that bypasses the stub, but it doesn't change the fact that a .net EXE has an unmanaged stub that bootstraps into managed code.
John Knoeller
@John: sure, but the stub itself doesn't create an AppDomain nor does it instantiate anything. It just jumps to mscoree.dll.
Jb Evain
@Jb: which creates an AppDomain and instantiates an object. Look if you want to chime and add some details then feel free. But you have no call to mark this post down for correct information just because it isn't as detailed as you would like it to be.
John Knoeller
@John: Maybe you shouldn't be downvoted for this answer, yes, but I think JB adds some really important points to the discussion. I'm upvoting both of you. "Cant we all just... get along?" -Rodney King
Dave Markle
@Dave: Thanks, I'm delighted to have JB point out that the stub is unused in XP, I'll bet he could add a _lot_ of good details about what happens inside mscoree.dll. I've never looked at the code, I only know what the results are.
John Knoeller
@John, it's not about detailing, it's about correcting. Both a managed dll and a managed exe contain a stub. Both stubs are just a native jump to mscoree which is in charge of the initialization of the CLR, and the launch of the user code.
Jb Evain
As to what happens in CorExeMain, I can only guess as well. But it certainly initializes the whole CLR using the assembly metadata version. Then it has to create the main domain, and JIT compile the entry point method found in the assembly headers, and jump to it.
Jb Evain
@Jb: so you felt the need to 'correct' because 99.9% of the code executed by the stub lives in a dll, instead of the 99.5% that I implied? Seriously? Or did you really think I was trying to say the stub creates an AppDomain _without calling any dll helper code_?
John Knoeller
Well, I don't feel any special need to correct anyone. It's just that the wording of the answer is incorrect. I feel sorry that you feel so offended by the fact that am merely pointing out that the answer is both imprecise and inexact.
Jb Evain
+1  A: 

Because .exe means something to the OS - Windows doesn't have to register another executable type to run. .EXE files "just work" from the Run menu, the command line, and the shell.

But probably more importantly, because people have come to expect the behavior of a .exe file. I mean you've used .EXE files as the main executable file format on MS systems for the better part of 30 years now. If they compiled executable .net apps to, say, a ".CLR" extension, not everyone would know what to make of it, and that could have slowed the adoption of .Net, especially in the early days...

Dave Markle