.Net Runtime Library for Delphi
Close
Hosting CLR
Before any managed code can be executed, the host must load and initialize the common language runtime. If your delphi applications utilises the .Net runtime library for delphi, the Common Language Runtime (CLR) will be started automatically by implicitly creating an instance of the TClrHost class. To manually start the CLR, call ClrHostManager function or explicitly create an instance of TClrHost object or call the static function GetDefault of the TClrHost class. The CLR host class are defined in the CNClrLib.Host namespace . The runtime host loads the runtime into a process, creates application domains within the process, and loads and executes user code within those application domains. The purpose of this class is to start the .net runtime library for delphi which in turn starts the .Net framework CLR and execute managed codes from delphi. Application domains provide greater reliability by isolating assemblies from one another. Assemblies can be unloaded from the process by unloading application domains. The application domain is called TClrAppDomain which is a wrapper of the AppDomain interface. This class is also defined in the CNClrLib.Host namespace .
Example
Pascal
program CreateCLRHostDemo; {$APPTYPE CONSOLE} {$R *.res} uses System.SysUtils, CNClrLib.Host, CNClrLib.Core, CNClrLib.Enums; var Console: _Console; ClrHost: TClrHost; begin try //Calling any of the following lines of code will start the .net runtime host. //NB: The CLRHost instance is created once and any attempt to recreate the instance explicitly // will cause an error. //1. //Calling this method will create an instance of the TClrHost class implicitly //and start the runtime host if the host object has not been created already. Console := CoConsole.CreateInstance; //2. //Calling this global function in the CNClrLib.Host namespace will use the default //TClrHost instance created. If the instance has not been created already, the function //automatically create a new instance of the TClrHost class. ClrHost := ClrHostManager; //3. //To Explicitly Create an instance of the host class, either call the static //method GetDefault or the Create constructor of the TClrHost Class. //a. Using GetDefault ClrHost := TClrHost.GetDefault; //b. Using Create Constructor ClrHost := TClrHost.Create; except on E: Exception do Writeln(E.ClassName, ': ', E.Message); end; end.