public static ComEventSinksContainer FromRuntimeCallableWrapper(object rcw, bool createIfNotFound)
        {
            // !!! Marshal.Get/SetComObjectData has a LinkDemand for UnmanagedCode which will turn into
            // a full demand. We need to avoid this by making this method SecurityCritical
            object data = Marshal.GetComObjectData(rcw, _ComObjectEventSinksKey);

            if (data != null || createIfNotFound == false)
            {
                return((ComEventSinksContainer)data);
            }

            lock (_ComObjectEventSinksKey) {
                data = Marshal.GetComObjectData(rcw, _ComObjectEventSinksKey);
                if (data != null)
                {
                    return((ComEventSinksContainer)data);
                }

                ComEventSinksContainer comEventSinks = new ComEventSinksContainer();
                if (!Marshal.SetComObjectData(rcw, _ComObjectEventSinksKey, comEventSinks))
                {
                    throw Error.SetComObjectDataFailed();
                }

                return(comEventSinks);
            }
        }
Exemple #2
0
        public static ComEventSink FromRuntimeCallableWrapper(object rcw, Guid sourceIid, bool createIfNotFound)
        {
            List <ComEventSink> comEventSinks = ComEventSinksContainer.FromRuntimeCallableWrapper(rcw, createIfNotFound);

            if (comEventSinks == null)
            {
                return(null);
            }

            ComEventSink comEventSink = null;

            lock (comEventSinks) {
                foreach (ComEventSink sink in comEventSinks)
                {
                    if (sink._sourceIid == sourceIid)
                    {
                        comEventSink = sink;
                        break;
                    }

                    if (sink._sourceIid == Guid.Empty)
                    {
                        // we found a ComEventSink object that
                        // was previously disposed. Now we will reuse it.
                        sink.Initialize(rcw, sourceIid);
                        comEventSink = sink;
                    }
                }

                if (comEventSink == null && createIfNotFound)
                {
                    comEventSink = new ComEventSink(rcw, sourceIid);
                    comEventSinks.Add(comEventSink);
                }
            }

            return(comEventSink);
        }