问题描述
我一直在开发使用.NET相当一段时间,现在的应用程序。但是,我仍然不确定如何在CLR知道,.NET应用程序已启动。有没有像每个应用程序的CLR的一个实例?我不认为这是可以的情况下,只有一个GC为所有的.NET应用程序管理所有的记忆。是否CLR那种在后台运行?我很困惑。
I have been developing apps using .net for quite sometime now. But, I am still notsure how does the CLR know that a .net app has started. Is there like one instance of CLR per app? I don't think this can be the case as there is just one GC which manages all the memory for all .net apps. Does the CLR kind of run in background? I am quite confused.
推荐答案
嗯,让我来拍这个了。
-
有人建立在C#.NET应用程序,或.NET中间语言,或其他托管语言。
Somebody builds a .NET application in C#, or .NET 'Intermediate Language', or another managed language.
在编译器的csc.exe的语言(C#),或ilasm.exe(字节code和汇编),或两者,产生一个PE可执行文件。该PE可执行文件有编译器或汇编填充特定的结构。这包括:
The compiler for that language csc.exe (C#), or ilasm.exe (bytecode assembler), or whichever, produces a PE executable. The PE executable has a specific structure that the compiler or assembler populates. That includes:
- 在一个切入点,
- ,它使用的动态库的列表(导入表)。其中一个库是 MSCorEE.dll中
- 大量的元数据,包括有针对性的.NET运行库版本
在可执行文件被点击,在命令行运行,或者从一个Win32 API中,的(位于ntdll.dll之中)接管
When the executable is clicked on, ran from the command line, or executed from a Win32 API, the Windows loader implementation (in NTDLL.dll) takes over
装载机code是负责获取可执行文件到内存中,加载动态链接库,如果需要的话,映射链接库到一个地方的可执行文件code可以达到他们,并更新导入地址表与映射库的实际地址。
The loader code is responsible for getting the executable into memory, loading dynamic link libraries if needed, mapping linked libraries into a place the executable code can reach them, and updating the Import Address Table with the actual addresses of the mapped libraries.
在一切都准备好,加载器跳转到入口点(通过什么我认为是一些有心计从内核空间转换到用户空间,或保护模式,因为应用程序在它自己的受保护的32或64个运行比特存储器空间)。入口点进入MSCorEE.dll中 - .NET通用Object运行时执行引擎,它只是映射到进程的内存。 我见过被称为.NET启动垫片此DLL,它允许.NET的多个安装存在于一台机器。 MSCorEE.dll中是你使用,如果你是嵌入在自己的普通的应用程序.NET语言库。的
Once all is ready, the loader jumps to the entry point (through what I assume is some shenanigans switching from kernel space to user space, or to protected mode, since the application runs in it's own protected 32 or 64 bit memory space). The entry point goes to mscoree.dll - the .NET Common Object Runtime Execution Engine, which was just mapped into the processes memory. I've seen this DLL referred to as the .NET startup shim, and it allows the multiple installs of .NET to exist on one machine. Mscoree.dll is the library you'll use if you are embedding a .NET language in your own regular application.
看的Mscoree.dll在从PE可执行文件加载的元数据,特别是CLR头,并有针对性的.NET运行时版本。从它能够CorBindToRuntimeEx的2以正确的CLR版本。
Mscoree.dll looks at the metadata loaded from the PE executable, specifically the CLR header, and the targeted .NET runtime version. From that it can CorBindToRuntimeEx2 to the right CLR version.
在CorBindToRuntimeEx的加载正确的.NET运行时实现(并返回一个指向一个COM接口,让您援引.NET运行库。这code从dll文件加载到%WINDIR%\微软。 NET \框架符\ v #####。
The CorBindToRuntimeEx loads the correct .NET runtime implementation (and returns a pointer to a COM interface allowing you to invoke that .NET runtime. This code is loaded from the dlls in %WINDIR%\Microsoft.NET\Framework\v#####.
我不知道谁在这一点上,但可能是mscoree垫片使用.NET ICLRRuntimeHost接口指针调用方法来初始化.NET运行时,垃圾回收,IL跨preTER,JIT和IHostControl接口(即允许.NET跨preTER顶嘴托管过程中),最终讲述了国米preTER开始执行已编译应用程序的IL code。
I'm not sure who at this point, but probably the mscoree shim uses the .NET ICLRRuntimeHost interface pointer to invoke methods to initialize the .NET runtime, garbage collector, IL interpreter, JIT and IHostControl interfaces (that allow the .NET interpreter to talk back to the hosting process), and ultimately tells the Interpreter to start executing your compiled application's IL code.
(我学到了很多东西写这 - !有一吨的后面链接的信息,我当然通过这一切并没有得到)
(I learnt a lot writing this - there is a ton of information behind the links, I certainly didn't get through all of it!)
http://msdn.microsoft.com/en-us/library/xh0859k0.aspx
http://my.safaribooksonline.com/book/programming/microsoft-dotnet/0735619883/a-tour-of-the-clr-hosting-api/ch02lev1sec3
http://msdn.microsoft.com/en-us/magazine/bb985994.aspx
这篇关于当.NET应用程序启动时,会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!