ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

跨 Appdomain 数据共享

2019-09-09 16:50:50  阅读:278  来源: 互联网

标签:IntPtr void Appdomain System 数据共享 private Guid out


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Text;

namespace SGSMWebTest
{
    public partial class Default : System.Web.UI.Page
    {
        private static FirstEliteValidateLib.Validator MyValidator = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            var sb = new StringBuilder();
            _AppDomain defaultAppDomain;
            var appdomainList = CLRUtil.EnumAppDomains(out defaultAppDomain).ToList();
            sb.AppendLine("***********Start**************");

            foreach (var appdomain in appdomainList)
                sb.AppendLine("AppDomainName:" + appdomain.FriendlyName + (appdomain.FriendlyName == defaultAppDomain.FriendlyName ? " [DefaultAppDomain]" : string.Empty));

            sb.AppendLine("***********End**************");
            sb.AppendLine("Current Appdomain:" + AppDomain.CurrentDomain.FriendlyName);

            if (defaultAppDomain.GetData("Validator") == null)
            {
                MyValidator = new FirstEliteValidateLib.Validator();
                defaultAppDomain.SetData("Validator", MyValidator);
                sb.AppendLine("Set Validator");
            }
            else
            {
                MyValidator = defaultAppDomain.GetData("Validator") as FirstEliteValidateLib.Validator;
                if(MyValidator != null)
                    sb.AppendLine("Validator Get Success");
                else
                    sb.AppendLine("Validator Get null");
            }

            Label1.Text = "<pre>" + sb.ToString() + "</pre>";
        }

        public static class CLRUtil
        {
            public static List<_AppDomain> EnumAppDomains(out _AppDomain defaultAppDomain)
            {
                // Obtain ICLRMetaHost interface
                object objHost;
                int hr = CLRCreateInstance(ref CLSID_CLRMetaHost, ref IID_CLRMetaHost, out objHost);
                if (hr < 0) throw new COMException("Cannot create meta host", hr);
                var host = (ICLRMetaHost)objHost;

                // Obtain ICLRRuntimeInfo interface
                var vers = Environment.Version;
                var versString = string.Format("v{0}.{1}.{2}", vers.Major, vers.Minor, vers.Build);
                var objRuntime = host.GetRuntime(versString, ref IID_CLRRuntimeInfo);
                var runtime = (ICLRRuntimeInfo)objRuntime;
                bool started;
                uint flags;
                runtime.IsStarted(out started, out flags);
                if (!started) throw new COMException("CLR not started??");

                // Obtain legacy ICorRuntimeHost interface and iterate appdomains
                var result = new List<_AppDomain>();
                var V2Host = (ICorRuntimeHost)runtime.GetInterface(ref CLSID_CorRuntimeHost, ref IID_CorRuntimeHost);
                V2Host.GetDefaultDomain(out defaultAppDomain);
                IntPtr hDomainEnum;
                V2Host.EnumDomains(out hDomainEnum);
                for (; ; )
                {
                    _AppDomain domain = null;
                    V2Host.NextDomain(hDomainEnum, out domain);
                    if (domain == null) break;
                    result.Add(domain);
                }
                V2Host.CloseEnum(hDomainEnum);
                return result;
            }

            private static Guid CLSID_CLRMetaHost = new Guid(0x9280188d, 0xe8e, 0x4867, 0xb3, 0xc, 0x7f, 0xa8, 0x38, 0x84, 0xe8, 0xde);
            private static Guid IID_CLRMetaHost = new Guid(0xD332DB9E, 0xB9B3, 0x4125, 0x82, 0x07, 0xA1, 0x48, 0x84, 0xF5, 0x32, 0x16);
            private static Guid IID_CLRRuntimeInfo = new Guid(0xBD39D1D2, 0xBA2F, 0x486a, 0x89, 0xB0, 0xB4, 0xB0, 0xCB, 0x46, 0x68, 0x91);
            private static Guid CLSID_CorRuntimeHost = new Guid(0xcb2f6723, 0xab3a, 0x11d2, 0x9c, 0x40, 0x00, 0xc0, 0x4f, 0xa3, 0x0a, 0x3e);
            private static Guid IID_CorRuntimeHost = new Guid(0xcb2f6722, 0xab3a, 0x11d2, 0x9c, 0x40, 0x00, 0xc0, 0x4f, 0xa3, 0x0a, 0x3e);

            [DllImport("mscoree.dll")]
            private static extern int CLRCreateInstance(ref Guid clsid, ref Guid iid,
                [MarshalAs(UnmanagedType.Interface)] out object ptr);

            [ComImport, Guid("D332DB9E-B9B3-4125-8207-A14884F53216"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
            private interface ICLRMetaHost
            {
                [return: MarshalAs(UnmanagedType.Interface)]
                object GetRuntime(string version, ref Guid iid);
                // Rest omitted
            }

            [ComImport, Guid("BD39D1D2-BA2F-486a-89B0-B4B0CB466891"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
            private interface ICLRRuntimeInfo
            {
                void GetVersionString(char[] buffer, int bufferLength);
                void GetRuntimeDirectory(char[] buffer, int bufferLength);
                bool IsLoaded(IntPtr hProcess);
                void LoadErrorString(uint id, char[] buffer, int bufferLength, int lcid);
                void LoadLibrary(string path, out IntPtr hMdodule);
                void GetProcAddress(string name, out IntPtr addr);
                [return: MarshalAs(UnmanagedType.Interface)]
                object GetInterface(ref Guid clsid, ref Guid iid);
                bool IsLoadable();
                void SetDefaultStartupFlags(uint flags, string configFile);
                void GetDefaultStartupFlags(out uint flags, char[] configFile, int configFileLength);
                void BindAsLegacyV2Runtime();
                void IsStarted(out bool started, out uint flags);
            }

            [ComImport, Guid("CB2F6722-AB3A-11d2-9C40-00C04FA30A3E"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
            private interface ICorRuntimeHost
            {
                void CreateLogicalThreadState();
                void DeleteLogicalThreadState();
                void SwitchinLogicalThreadState(IntPtr cookie);
                void SwitchoutLogicalThreadState(out IntPtr cookie);
                void LocksHeldByLogicalThread(out int count);
                void MapFile(IntPtr hFile, out IntPtr address);
                void GetConfiguration(out IntPtr config);
                void Start();
                void Stop();
                void CreateDomain(string name, object identity, out _AppDomain domain);
                void GetDefaultDomain(out _AppDomain domain);
                void EnumDomains(out IntPtr hEnum);
                void NextDomain(IntPtr hEnum, out _AppDomain domain);
                void CloseEnum(IntPtr hEnum);
                // rest omitted
            }
        }
    }
}

 

标签:IntPtr,void,Appdomain,System,数据共享,private,Guid,out
来源: https://www.cnblogs.com/nanfei/p/11492683.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有