Exemplo n.º 1
0
        internal IProxyProvider GetProxyProvider()
        {
            if (null == monikerSupportLibrary)
            {
                lock (this)
                {
                    if (null == monikerSupportLibrary)
                    {
                        getCODelegate = null;
                        using (RegistryHandle regKey = RegistryHandle.GetCorrectBitnessHKLMSubkey((IntPtr.Size == 8), ServiceModelInstallStrings.WinFXRegistryKey))
                        {
                            string            file        = regKey.GetStringValue(ServiceModelInstallStrings.RuntimeInstallPathName).TrimEnd('\0') + "\\" + fileName;
                            SafeLibraryHandle tempLibrary = UnsafeNativeMethods.LoadLibrary(file);
                            tempLibrary.DoNotFreeLibraryOnRelease();

                            monikerSupportLibrary = tempLibrary;
                            if (monikerSupportLibrary.IsInvalid)
                            {
                                monikerSupportLibrary.SetHandleAsInvalid();
                                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(Error.ServiceMonikerSupportLoadFailed(file));
                            }
                        }
                    }
                }
            }

            if (null == getCODelegate)
            {
                lock (this)
                {
                    if (null == getCODelegate)
                    {
                        try
                        {
                            IntPtr procaddr = UnsafeNativeMethods.GetProcAddress(monikerSupportLibrary, functionName);
                            getCODelegate = (DelegateDllGetClassObject)Marshal.GetDelegateForFunctionPointer(procaddr, typeof(DelegateDllGetClassObject));
                        }
                        catch (Exception e)
                        {
                            if (Fx.IsFatal(e))
                            {
                                throw;
                            }

                            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ComPlusProxyProviderException(SR.GetString(SR.FailedProxyProviderCreation), e));
                        }
                    }
                }
            }

            IClassFactory  cf            = null;
            IProxyProvider proxyProvider = null;

            try
            {
                getCODelegate(ClsidProxyInstanceProvider, typeof(IClassFactory).GUID, ref cf);

                proxyProvider = cf.CreateInstance(null, typeof(IProxyProvider).GUID) as IProxyProvider;
                Thread.MemoryBarrier();
            }
            catch (Exception e)
            {
                if (Fx.IsFatal(e))
                {
                    throw;
                }

                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ComPlusProxyProviderException(SR.GetString(SR.FailedProxyProviderCreation), e));
            }
            finally
            {
                if (null != cf)
                {
                    Marshal.ReleaseComObject(cf);
                    cf = null;
                }
            }

            return(proxyProvider);
        }
        public static Type ResolveClsidToType(Guid clsid)
        {
            string name = @"software\classes\clsid\{" + clsid.ToString() + @"}\InprocServer32";

            using (RegistryKey key = Registry.LocalMachine.OpenSubKey(name, false))
            {
                if (key != null)
                {
                    using (RegistryKey key2 = key.OpenSubKey(typeof(TypeCacheManager).Assembly.ImageRuntimeVersion))
                    {
                        string str2 = null;
                        if (key2 == null)
                        {
                            name = null;
                            foreach (string str3 in key.GetSubKeyNames())
                            {
                                name = str3;
                                if (!string.IsNullOrEmpty(name))
                                {
                                    using (RegistryKey key3 = key.OpenSubKey(name))
                                    {
                                        str2 = (string)key3.GetValue("Assembly");
                                        if (!string.IsNullOrEmpty(str2))
                                        {
                                            break;
                                        }
                                    }
                                }
                            }
                        }
                        else
                        {
                            str2 = (string)key2.GetValue("Assembly");
                        }
                        if (!string.IsNullOrEmpty(str2))
                        {
                            foreach (Type type in Assembly.Load(str2).GetTypes())
                            {
                                if (type.IsClass && (type.GUID == clsid))
                                {
                                    return(type);
                                }
                            }
                        }
                        return(null);
                    }
                }
            }
            using (RegistryHandle handle = RegistryHandle.GetBitnessHKCR(IntPtr.Size != 8))
            {
                if (handle != null)
                {
                    using (RegistryHandle handle2 = handle.OpenSubKey(@"CLSID\{" + clsid.ToString() + @"}\InprocServer32"))
                    {
                        using (RegistryHandle handle3 = handle2.OpenSubKey(typeof(TypeCacheManager).Assembly.ImageRuntimeVersion))
                        {
                            string stringValue = null;
                            if (handle3 == null)
                            {
                                name = null;
                                foreach (string str in handle2.GetSubKeyNames())
                                {
                                    if (!string.IsNullOrEmpty(str))
                                    {
                                        using (RegistryHandle handle4 = handle2.OpenSubKey(str))
                                        {
                                            stringValue = handle4.GetStringValue("Assembly");
                                            if (!string.IsNullOrEmpty(stringValue))
                                            {
                                                break;
                                            }
                                        }
                                    }
                                }
                            }
                            else
                            {
                                stringValue = handle3.GetStringValue("Assembly");
                            }
                            if (!string.IsNullOrEmpty(stringValue))
                            {
                                foreach (Type type2 in Assembly.Load(stringValue).GetTypes())
                                {
                                    if (type2.IsClass && (type2.GUID == clsid))
                                    {
                                        return(type2);
                                    }
                                }
                            }
                            return(null);
                        }
                    }
                }
            }
            return(null);
        }
        internal IProxyProvider GetProxyProvider()
        {
            if (this.monikerSupportLibrary == null)
            {
                lock (this)
                {
                    if (this.monikerSupportLibrary == null)
                    {
                        this.getCODelegate = null;
                        using (RegistryHandle handle = RegistryHandle.GetCorrectBitnessHKLMSubkey(IntPtr.Size == 8, @"SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Client"))
                        {
                            string libFilename = handle.GetStringValue("InstallPath").TrimEnd(new char[1]) + @"\ServiceMonikerSupport.dll";
                            this.monikerSupportLibrary = UnsafeNativeMethods.LoadLibrary(libFilename);
                            this.monikerSupportLibrary.DoNotFreeLibraryOnRelease();
                            if (this.monikerSupportLibrary.IsInvalid)
                            {
                                this.monikerSupportLibrary.SetHandleAsInvalid();
                                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(Error.ServiceMonikerSupportLoadFailed(libFilename));
                            }
                        }
                    }
                }
            }
            if (this.getCODelegate == null)
            {
                lock (this)
                {
                    if (this.getCODelegate == null)
                    {
                        try
                        {
                            IntPtr procAddress = UnsafeNativeMethods.GetProcAddress(this.monikerSupportLibrary, "DllGetClassObject");
                            this.getCODelegate = (DelegateDllGetClassObject)Marshal.GetDelegateForFunctionPointer(procAddress, typeof(DelegateDllGetClassObject));
                        }
                        catch (Exception exception)
                        {
                            if (Fx.IsFatal(exception))
                            {
                                throw;
                            }
                            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ComPlusProxyProviderException(System.ServiceModel.SR.GetString("FailedProxyProviderCreation"), exception));
                        }
                    }
                }
            }
            IClassFactory  ppv      = null;
            IProxyProvider provider = null;

            try
            {
                this.getCODelegate(ClsidProxyInstanceProvider, typeof(IClassFactory).GUID, ref ppv);
                provider = ppv.CreateInstance(null, typeof(IProxyProvider).GUID) as IProxyProvider;
                Thread.MemoryBarrier();
            }
            catch (Exception exception2)
            {
                if (Fx.IsFatal(exception2))
                {
                    throw;
                }
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ComPlusProxyProviderException(System.ServiceModel.SR.GetString("FailedProxyProviderCreation"), exception2));
            }
            finally
            {
                if (ppv != null)
                {
                    Marshal.ReleaseComObject(ppv);
                    ppv = null;
                }
            }
            return(provider);
        }
        public static Type ResolveClsidToType(Guid clsid)
        {
            string keyName = String.Concat("software\\classes\\clsid\\{", clsid.ToString(), "}\\InprocServer32");

            using (RegistryKey clsidKey = Registry.LocalMachine.OpenSubKey(keyName, false))
            {
                if (clsidKey != null)
                {
                    using (RegistryKey assemblyKey = clsidKey.OpenSubKey(typeof(TypeCacheManager).Assembly.ImageRuntimeVersion))
                    {
                        string assemblyName = null;
                        if (assemblyKey == null)
                        {
                            keyName = null;
                            foreach (string subKeyName in clsidKey.GetSubKeyNames())
                            {
                                keyName = subKeyName;
                                if (String.IsNullOrEmpty(keyName))
                                {
                                    continue;
                                }
                                using (RegistryKey assemblyKeyAny = clsidKey.OpenSubKey(keyName))
                                {
                                    assemblyName = (string)assemblyKeyAny.GetValue("Assembly");
                                    if (String.IsNullOrEmpty(assemblyName))
                                    {
                                        continue;
                                    }
                                    else
                                    {
                                        break;
                                    }
                                }
                            }
                        }
                        else
                        {
                            assemblyName = (string)assemblyKey.GetValue("Assembly");
                        }
                        if (String.IsNullOrEmpty(assemblyName))
                        {
                            return(null);
                        }
                        Assembly asm = Assembly.Load(assemblyName);
                        foreach (Type type in asm.GetTypes())
                        {
                            if (type.IsClass && (type.GUID == clsid))
                            {
                                return(type);
                            }
                        }
                        return(null);
                    }
                }
            }
            // We failed to get the hive information from a native process hive lets go for the alternative bitness

            using (RegistryHandle hkcr = RegistryHandle.GetBitnessHKCR(IntPtr.Size == 8 ? false : true))
            {
                if (hkcr != null)
                {
                    using (RegistryHandle clsidKey = hkcr.OpenSubKey(String.Concat("CLSID\\{", clsid.ToString(), "}\\InprocServer32")))
                    {
                        using (RegistryHandle assemblyKey = clsidKey.OpenSubKey(typeof(TypeCacheManager).Assembly.ImageRuntimeVersion))
                        {
                            string assemblyName = null;
                            if (assemblyKey == null)
                            {
                                keyName = null;
                                foreach (string subKeyName in clsidKey.GetSubKeyNames())
                                {
                                    keyName = subKeyName;
                                    if (String.IsNullOrEmpty(keyName))
                                    {
                                        continue;
                                    }
                                    using (RegistryHandle assemblyKeyAny = clsidKey.OpenSubKey(keyName))
                                    {
                                        assemblyName = (string)assemblyKeyAny.GetStringValue("Assembly");
                                        if (String.IsNullOrEmpty(assemblyName))
                                        {
                                            continue;
                                        }
                                        else
                                        {
                                            break;
                                        }
                                    }
                                }
                            }
                            else
                            {
                                assemblyName = assemblyKey.GetStringValue("Assembly");
                            }
                            if (String.IsNullOrEmpty(assemblyName))
                            {
                                return(null);
                            }
                            Assembly asm = Assembly.Load(assemblyName);
                            foreach (Type type in asm.GetTypes())
                            {
                                if (type.IsClass && (type.GUID == clsid))
                                {
                                    return(type);
                                }
                            }
                            return(null);
                        }
                    }
                }
            }
            return(null);
        }