internal static UnsafeNativeMethods.IMsoComponentManager GetComponentManager(IntPtr pOriginal)
 {
     lock (_syncObject)
     {
         if (_broker == null)
         {
             object obj2;
             ((UnsafeNativeMethods.ICorRuntimeHost)RuntimeEnvironment.GetRuntimeInterfaceAsObject(typeof(UnsafeNativeMethods.CorRuntimeHost).GUID, typeof(UnsafeNativeMethods.ICorRuntimeHost).GUID)).GetDefaultDomain(out obj2);
             AppDomain currentDomain = obj2 as AppDomain;
             if (currentDomain == null)
             {
                 currentDomain = AppDomain.CurrentDomain;
             }
             if (currentDomain == AppDomain.CurrentDomain)
             {
                 _broker = new ComponentManagerBroker();
             }
             else
             {
                 _broker = GetRemotedComponentManagerBroker(currentDomain);
             }
         }
     }
     return(_broker.GetProxy((long)pOriginal));
 }
        /// <devdoc>
        ///    This method locates our per-process app domain and connects to a running
        ///    instance of ComponentManagerBroker.  That instance then demand-
        ///    creates an instance of ComponentManagerProxy for the calling thread
        ///    and returns it.
        /// </devdoc>
        internal static UnsafeNativeMethods.IMsoComponentManager GetComponentManager(IntPtr pOriginal)
        {
            lock (_syncObject) {
                if (_broker == null)
                {
                    // We need the default domain for the process. That's the domain we will use
                    // for all component managers.  There is no managed way to get this domain, however,
                    // so we use ICorRuntimeHost.
                    UnsafeNativeMethods.ICorRuntimeHost host = (UnsafeNativeMethods.ICorRuntimeHost)RuntimeEnvironment.GetRuntimeInterfaceAsObject(typeof(UnsafeNativeMethods.CorRuntimeHost).GUID, typeof(UnsafeNativeMethods.ICorRuntimeHost).GUID);
                    object domainObj;
                    int    hr = host.GetDefaultDomain(out domainObj);
                    Debug.Assert(NativeMethods.Succeeded(hr), "ICorRuntimeHost failed to return the default domain.  The only way that should happen is if it hasn't been started yet, but if it hasn't been started how are we running managed code?");
                    AppDomain domain = domainObj as AppDomain;

                    if (domain == null)
                    {
                        Debug.Assert(NativeMethods.Failed(hr) || domain != null, "ICorRuntimeHost::GetDefaultDomain succeeded but didn't retrn us an app domain.");
                        domain = AppDomain.CurrentDomain;
                    }

                    // Ok, we have a domain.  Next, check to see if it is our current domain.
                    // If it is, we bypass the CreateInstanceAndUnwrap logic because we
                    // can directly go with the broker.  In this case we will create a broker
                    // and  NOT remote it.  We will defer the remoting until we have a different
                    // domain.  To detect this, the _broker static variable will be assigned
                    // a broker in the primary app domain.  The CreateInstance code looks at this
                    // and if it is aready set, simply remotes that broker.  That is why there
                    // is a "Singleton" property on the broker -- just in case we had to create
                    // a temporary broker during CreateInstanceAndUnwrap.

                    if (domain == AppDomain.CurrentDomain)
                    {
                        _broker = new ComponentManagerBroker();
                    }
                    else
                    {
                        _broker = GetRemotedComponentManagerBroker(domain);
                    }
                }
            }

            // However we got here, we got here.  What's important is that we have a proxied instance to the broker object
            // and we can now call on it.
            //
            return(_broker.GetProxy((long)pOriginal));
        }