Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens when user click .NET assembly (EXE)?

Tags:

c#

.net

Consider we have .NET Winforms application or Console Application. Can anyone tell me what will happen step-by-step until the WinForm or Console Application is launched. I would like know the internals - like how EXE will communicate with Framework, what is the role of CLR, what happens in case of exception while launching applicaiton itself.etc...

like image 616
Sathish Avatar asked May 07 '10 11:05

Sathish


People also ask

What is dotnet assembly?

NET-based applications. An assembly is a collection of types and resources that are built to work together and form a logical unit of functionality. Assemblies take the form of executable (.exe) or dynamic link library (. dll) files, and are the building blocks of . NET applications.

What is EXE in .NET framework?

NET Framework when we compile a Console Application or a Windows Application, it generates EXE, whereas when we compile a Class Library Project or ASP.NET web application, then it generates DLL. In.NET framework, both EXE and DLL are called assemblies.

What is assembly in c# net with example?

An assembly is a file that is automatically generated by the compiler upon successful compilation of every . NET application. It can be either a Dynamic Link Library or an executable file. It is generated only once for an application and upon each subsequent compilation the assembly gets updated.

What is .NET assembly explain its types?

In the Microsoft . NET framework, an assembly is a partially compiled code library for use in deployment, versioning and security. There are two types: process assemblies (EXE) and library assemblies (DLL). A process assembly represents a process which will use classes defined in library assemblies. .


2 Answers

When you double click on a .net .exe assembly:

  • Windows' PE loader kicks in
  • If you're on a Windows >= Windows XP it will detect that the executable is a managed executable and will forward it to .net by calling _CoreExeMain in mscoree.dll (_CoreDllMain if you double clicked on a managed .dll). It can use the assembly configuration file to know which runtime to use.
  • If you're on Windows < Windows XP, the .exe file contains a small native piece of code that will jump to mscoree.dll's _CoreExeMain or _CoreDllMain.
  • Then mscoree.dll initializes the .net runtime, depending on the global configuration, the assembly configuration file, and what not.
  • Then if it's a .exe, it will JIT compile its entry point method, and start executing it.
like image 187
Jb Evain Avatar answered Oct 13 '22 00:10

Jb Evain


The MSCoreEE.dll (MSCore Execution Engine.Dll only one instance for one machine) Say for example when a .Net assembly / .exe is double clicked or launched, the OS will load the windows loader which will inturn load the PE header (Portable executable) [in case of win32 executable, the PE header will contain the address of the bootstrap (static Main()) from where it will load and execute the main method, where as in .Net, the bootstrap will contain the address of MSCoreEE.Dll which will be present in C:\Windows\System32\mscoree.dll which will get executed and load the .Net runtime for which the .net assembly was targeted for. There can be multiple versions of .Net runtime installed on the machine, however, there will be only one instance of mscoreee.dll to load the specific runtimes.

The CLR will create the first APP domain itself and load the assembly (if the assembly didn’t create additional app domains in code)

The CLR creates 3 Application Domains internally 1. System App Domain a.  is responsible to load Shared and Default application domains, also loads mscorelib.dll to shared app domain b.  Create 3 seperates instances of Exceptions i. Fatal engine exception ii. Stack overflow exception iii. Out of memory exception (very important, the CLR precreates “out of memory” exception bcose when developer thinks the application can go out of memory and wants to write the exception to a log file, creating out of memory exception will take place because there was no memory left to create a new instance of this exception, so, CLR precreates this exception for future use in the application 2. Shared App Domain a. Contains the mscorlib.dll b. Other Common libraries which is used by other app domains c. However, the developer can’t push custom Dll’s into the shared app domain as its not controllable from outside of CLR, the CLR hosts these dll’s and CLR itself cannot be controlled of how it is hosted by a developer however its possible using some COM interfaces where the developer can host the CLR customary 3. Default App Domain a. All user binaries the .exe’s, Dlls gets loaded here

like image 36
Gururaj Avatar answered Oct 13 '22 00:10

Gururaj