public static IAssemblyCache GetAssemblyCacheInterface(string CLRVersionString, bool FetchRuntimeHost, out CCorRuntimeHost RuntimeHost)
        {
            IClrMetaHostPolicy clrMetaHostPolicy = null;

            RuntimeHost = null;
            GetClrMetaHostPolicy(ref _metaHostPolicyClsIdGuid, ref _metaHostPolicyGuid, out clrMetaHostPolicy);
            if (clrMetaHostPolicy == null)
            {
                return(null);
            }
            StringBuilder version = new StringBuilder("v", "v65535.65535.65535".Length);

            version.Append(CLRVersionString);
            int             capacity           = version.Capacity;
            int             imageVersionLength = 0;
            int             pdwConfigFlags     = 0;
            IClrRuntimeInfo runtimeInfo        = (IClrRuntimeInfo)clrMetaHostPolicy.GetRequestedRuntime(MetaHostPolicyFlags.MetaHostPolicyApplyUpgradePolicy, null, null, version, ref capacity, null, ref imageVersionLength, out pdwConfigFlags, _clrRuntimeInfoGuid);

            if (runtimeInfo == null)
            {
                return(null);
            }
            CoInitializeEEDelegate delegateForFunctionPointer = (CoInitializeEEDelegate)Marshal.GetDelegateForFunctionPointer(runtimeInfo.GetProcAddress("CoInitializeEE"), typeof(CoInitializeEEDelegate));

            Marshal.ThrowExceptionForHR(delegateForFunctionPointer(0));
            if (FetchRuntimeHost)
            {
                RuntimeHost = new CCorRuntimeHost(runtimeInfo);
            }
            CreateAssemblyCacheDelegate delegate3 = (CreateAssemblyCacheDelegate)Marshal.GetDelegateForFunctionPointer(runtimeInfo.GetProcAddress("CreateAssemblyCache"), typeof(CreateAssemblyCacheDelegate));
            IAssemblyCache ppAsmCache             = null;

            Marshal.ThrowExceptionForHR(delegate3(out ppAsmCache, 0));
            return(ppAsmCache);
        }
Ejemplo n.º 2
0
        static Utils()
        {
            var sysroot = Environment.GetFolderPath(Environment.SpecialFolder.Windows);
            var f40 = Path.Combine(sysroot, "Microsoft.NET", Environment.Is64BitProcess ? "Framework64" : "Framework", "v4.0.30319", "fusion.dll");
            var f20 = Path.Combine(sysroot, "Microsoft.NET", Environment.Is64BitProcess ? "Framework64" : "Framework", "v2.0.50727", "fusion.dll");

            if (File.Exists(f40)) {
                var f40dll = LoadLibrary(f40);
                if (f40dll != IntPtr.Zero) {
                    Net40Fusion = new UnmanagedLibrary(f40dll);
                    CreateAssemblyEnum40 = (CreateAssemblyEnumDelegate)Marshal.GetDelegateForFunctionPointer(Net40Fusion.GetProcAddr("CreateAssemblyEnum"), typeof (CreateAssemblyEnumDelegate));
                    CreateAssemblyNameObject40 = (CreateAssemblyNameObjectDelegate)Marshal.GetDelegateForFunctionPointer(Net40Fusion.GetProcAddr("CreateAssemblyNameObject"), typeof (CreateAssemblyNameObjectDelegate));
                    CreateAssemblyCache40 = (CreateAssemblyCacheDelegate)Marshal.GetDelegateForFunctionPointer(Net40Fusion.GetProcAddr("CreateAssemblyCache"), typeof (CreateAssemblyCacheDelegate));
                    CreateInstallReferenceEnum40 = (CreateInstallReferenceEnumDelegate)Marshal.GetDelegateForFunctionPointer(Net40Fusion.GetProcAddr("CreateInstallReferenceEnum"), typeof (CreateInstallReferenceEnumDelegate));
                }
            }

            if (File.Exists(f20)) {
                var f20dll = LoadLibrary(f20);
                if (f20dll != IntPtr.Zero) {
                    Net20Fusion = new UnmanagedLibrary(f20dll);
                    CreateAssemblyEnum20 = (CreateAssemblyEnumDelegate)Marshal.GetDelegateForFunctionPointer(Net20Fusion.GetProcAddr("CreateAssemblyEnum"), typeof (CreateAssemblyEnumDelegate));
                    CreateAssemblyNameObject20 = (CreateAssemblyNameObjectDelegate)Marshal.GetDelegateForFunctionPointer(Net20Fusion.GetProcAddr("CreateAssemblyNameObject"), typeof (CreateAssemblyNameObjectDelegate));
                    CreateAssemblyCache20 = (CreateAssemblyCacheDelegate)Marshal.GetDelegateForFunctionPointer(Net20Fusion.GetProcAddr("CreateAssemblyCache"), typeof (CreateAssemblyCacheDelegate));
                    CreateInstallReferenceEnum20 = (CreateInstallReferenceEnumDelegate)Marshal.GetDelegateForFunctionPointer(Net20Fusion.GetProcAddr("CreateInstallReferenceEnum"), typeof (CreateInstallReferenceEnumDelegate));
                }
            }
        }