CLR寄宿(下) 托管宿主

CLR寄宿()  托管宿主

通过托管代码来管理CLR方式,称为托管宿主。想实现托管宿主很简单,只要实现System.AppDomainManager类就可以AppDomainManager定义如代码清单1-1所示。

代码清单1-1  AppDomainManager类定义

 [ComVisibleAttribute(true)]

[SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.Infrastructure)]

[SecurityPermissionAttribute(SecurityAction.InheritanceDemand, Flags = SecurityPermissionFlag.Infrastructure)]

public classAppDomainManager : MarshalByRefObject

要实现自己托管宿主需要实现一个AppDomainManager子类,重写其中虚方法。代码清单1-2自定义XHAppDomainManager类来实现简单托管宿主。

代码清单1-2自定义XHAppDomainManager

namespace 托管宿主

{

    [GuidAttribute("F4D15099-3407-4A7E-A607-DEA440CF3891")]

    [SecurityPermissionAttribute(SecurityAction.LinkDemand,

        Flags = SecurityPermissionFlag.Infrastructure)]

 [SecurityPermissionAttribute(SecurityAction.InheritanceDemand,         Flags = SecurityPermissionFlag.Infrastructure)]

    class XHAppDomainManager:AppDomainManager

    {

        private HostSecurityManagerXHHostSecurityManager = null;

        public override void InitializeNewDomain(AppDomainSetupappDomainInfo)

        {

            Console.WriteLine(" XH AppDomain Manager ");

            XHHostSecurityManager = AppDomain.CurrentDomain.CreateInstanceAndUnwrap(

                "XHSecurityManager, Version=1.0.0.3, Culture=neutral, "+

                "PublicKeyToken=5659fc598c2a503e",

                "托管宿主.XHHostSecurityManager") as HostSecurityManager;

            Console.WriteLine(" Custom Security Manager Created.");

        }

        public override HostSecurityManager  HostSecurityManager

        {

            get

            {

                return XHHostSecurityManager;

            }

        }

        public override AssemblyEntryAssembly

        {

            get

            {

                return base.EntryAssembly;

            }

        }

    }

}

现在通过代码清单3-14创建宿主应用程序能够参与新应用程序域创建。在代码中声明两个权限,重写两个属性,在实际编码中可以按照这样简单模式进行扩展,真正参与CLR管理。

编写托管宿主之后,如何让它发挥作用呢?一般情况有三种选择:

非托管API

关于非托管APICLR寄宿一节进行详细介绍,但是在讨论CLR寄宿时候只讨论完全采用非托管API实现非托管宿主实施寄宿。那么想要通过非托管API来使托管宿主发挥作用话,必须在非托管API启动CLR之后把控制权转交给托管宿主。宿主在调用 CorBindToRuntimeEx之后,将请求指向 ICorRuntimeHost 接口指针,利用该指针转向到托管宿主。

环境变量

APPDOMAIN_MANAGER_ASM APPDOMAIN_MANAGER_TYPE 环境变量中标识替代AppDomainManager程序集和类型。该程序集必须完全受信任,并且包含在起始应用程序全局程序集缓存或目录中。环境变量中类型和程序集名称必须是完全限定。例如:

set APPDOMAIN_MANAGER_TYPE=MyNamespace.TestAppDomainManager

set APPDOMAIN_MANAGER_ASM=customappDomainmanager, Version=1.0.0.0, Culture=neutral, PublicKeyToken=f1368f7b12a08d72

注意只有被授予 FullTrust 程序集(如全局程序集缓存中程序集或者在 AppDomain.CreateDomain(String, Evidence, AppDomainSetup, PermissionSet, array<StrongName>[]()[]) 方法中标识为fullTrustAssemblies程序集)才可以在 AppDomainManager 构造函数和 InitializeNewDomain方法中加载。

注册表值

在启动CLR之前,HKEY_LOCAL_MACHINE(or HKEY_CURRENT_USER)\Software\Microsoft\.NETFramework\键值必须设置两个值,一个是APPDOMAIN_MANAGER_ASM,另一个是APPDOMAIN_MANAGER_TYPEAPPDOMAIN_MANAGER_ASM必须是安装到全局程序集缓存程序集,APPDOMAIN_MANAGER_TYPE是自定义托管宿主类。

 --------------------------------注:本文部分内容改编自《.NET 安全揭秘》


作者:玄魂
出处:http://www.cnblogs.com/xuanhun/
原文链接:http://www.cnblogs.com/xuanhun/ 更多内容,请访问我的个人站点 对编程,安全感兴趣的,加qq群:hacking-1群:303242737,hacking-2群:147098303,nw.js,electron交流群 313717550。
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
关注我:关注玄魂的微信公众号

原文地址:https://www.cnblogs.com/xuanhun/p/2559358.html