0

I am learning .NET framework programming model.

When a managed exe is run(so no machine code in it),how does the CLR(mscorlib.dll) take over and do JIT compilation,or who calls the CLR or is it running all the time?

ZoomIn
  • 1,237
  • 1
  • 17
  • 35
  • Possible duplicate? http://stackoverflow.com/questions/3401324/learn-more-about-how-net-works – Matthew Layton Jun 10 '13 at 10:23
  • 1
    @series0ne - I don't think it's an exact dupe. That post doesn't explain how the OS recognizes the .exe as managed code (ie: how PE was extended to wrap IL). – J... Jun 10 '13 at 10:26
  • 1
    @series0ne hi,thanks for the comment.I know about How does C# work? What is the CLR and what does it do? What is C# code converted to? but my question doesn't have answer in the link you provided. – ZoomIn Jun 10 '13 at 10:26
  • @ZoomIn, Ah! I was delving too deep into thought here. - Even I've learned something thanks to J...'s answer. - I'm be interested to know in this respect, how Mono compiles .NET applications...surely it won't use the same PE format as this isn't traditionally a known executable format outside of the Windows domain. – Matthew Layton Jun 10 '13 at 10:37
  • @series0ne ya,it would be interesting to know how Mono compiles .NET applications. – ZoomIn Jun 10 '13 at 10:41
  • 1
    @series0ne Linux uses ELF (not PE), but I don't think Mono generates any wrapper. As far as I'm aware you have to invoke compiled assemblies directly like `mono hello.exe` (at the command line). – J... Jun 10 '13 at 11:08
  • possible duplicate of [Why is an assembly .exe file?](http://stackoverflow.com/questions/5475646/why-is-an-assembly-exe-file) – J... Jun 10 '13 at 12:34

2 Answers2

2

Check that question out, its linked questions are also good.

By the way, mscorlib.dll is not the CLR, it just contains the basic managed types (see that question).

Community
  • 1
  • 1
argaz
  • 1,458
  • 10
  • 15
  • Good find - flagging this as duplicate because this really answers all the questions. – J... Jun 10 '13 at 12:35
1

A managed executable still contains a standard Portable Executable (link)* header that contains information about :

  • what type of module it is,
  • the module creation time stamp,
  • CPU architecture (32/64),
  • entry point memory address of the _CorExeMain() (or _CorDLLMain()) function

The PE header is read by windows when launching any application - in the case of a .NET application the header identifies it as a managed assembly and execution is then transferred to the CLR.


*See : .NET, metadata, and the PE format section

J...
  • 30,968
  • 6
  • 66
  • 143
  • hi,thanks for the answer,since managed executable doesn't have machine code,how can it have machine code section. – ZoomIn Jun 10 '13 at 10:29
  • 1
    This answer isn't very correct. It used to be for old versions, Windows 2000 and earlier. But XP and up have a loader that knows how to identify a managed program directly without relying on the 5 bytes of native code in an assembly. They have built-in knowledge of the loader stub, mscoree.dll. And automatically load it to bootstrap the managed code. This also the way the considerable magic is invoked that turns a 32-bit EXE into a 64-bit process. – Hans Passant Jun 10 '13 at 11:37
  • @HansPassant - I wasn't aware of that. Does this mean that VS no longer adds the PE wrapper? That .net exe files are purely managed assemblies? Does that break backwards compatibility with Win2K, etc? – J... Jun 10 '13 at 11:52
  • @HansPassant so how does the loader in >XP know which is managed executable and which is not? – ZoomIn Jun 10 '13 at 12:01
  • 2
    I haven't seen that documented anywhere. But it certainly is very visible in the PE header. Like the manifest header in the PE wrapper, the one you see when you run dumpbin.exe with the /clrheader option. Or the _CorExeMain or _CorDllMain link dependency. – Hans Passant Jun 10 '13 at 12:08
  • @HansPassant - I've updated to clarify and add your comments. – J... Jun 10 '13 at 12:30