internal ConnectionPointCookie(object source, object sink, Type eventInterface)
        {
            Exception ex = null;

            if (source is UnsafeNativeMethods.IConnectionPointContainer)
            {
                UnsafeNativeMethods.IConnectionPointContainer connectionPointContainer = (UnsafeNativeMethods.IConnectionPointContainer)source;
                try
                {
                    Guid guid = eventInterface.GUID;
                    if (connectionPointContainer.FindConnectionPoint(ref guid, out this.connectionPoint) != 0)
                    {
                        this.connectionPoint = null;
                    }
                }
                catch (Exception ex2)
                {
                    if (CriticalExceptions.IsCriticalException(ex2))
                    {
                        throw;
                    }
                    this.connectionPoint = null;
                }
                if (this.connectionPoint == null)
                {
                    ex = new ArgumentException(SR.Get("AxNoEventInterface", new object[]
                    {
                        eventInterface.Name
                    }));
                }
                else if (sink == null || (!eventInterface.IsInstanceOfType(sink) && !Marshal.IsComObject(sink)))
                {
                    ex = new InvalidCastException(SR.Get("AxNoSinkImplementation", new object[]
                    {
                        eventInterface.Name
                    }));
                }
                else
                {
                    int num = this.connectionPoint.Advise(sink, ref this.cookie);
                    if (num != 0)
                    {
                        this.cookie = 0;
                        Marshal.FinalReleaseComObject(this.connectionPoint);
                        this.connectionPoint = null;
                        ex = new InvalidOperationException(SR.Get("AxNoSinkAdvise", new object[]
                        {
                            eventInterface.Name,
                            num
                        }));
                    }
                }
            }
            else
            {
                ex = new InvalidCastException(SR.Get("AxNoConnectionPointContainer"));
            }
            if (this.connectionPoint != null && this.cookie != 0)
            {
                return;
            }
            if (this.connectionPoint != null)
            {
                Marshal.FinalReleaseComObject(this.connectionPoint);
            }
            if (ex == null)
            {
                throw new ArgumentException(SR.Get("AxNoConnectionPoint", new object[]
                {
                    eventInterface.Name
                }));
            }
            throw ex;
        }
Ejemplo n.º 2
0
        /// Creates a connection point to of the given interface type.
        /// which will call on a managed code sink that implements that interface.
        internal ConnectionPointCookie(object source, object sink, Type eventInterface)
        {
            Exception ex = null;

            if (source is UnsafeNativeMethods.IConnectionPointContainer)
            {
                UnsafeNativeMethods.IConnectionPointContainer cpc = (UnsafeNativeMethods.IConnectionPointContainer)source;

                try
                {
                    Guid tmp = eventInterface.GUID;
                    if (cpc.FindConnectionPoint(ref tmp, out connectionPoint) != NativeMethods.S_OK)
                    {
                        connectionPoint = null;
                    }
                }
                catch (Exception e)
                {
                    if (CriticalExceptions.IsCriticalException(e))
                    {
                        throw;
                    }

                    connectionPoint = null;
                }

                if (connectionPoint == null)
                {
                    ex = new ArgumentException(SR.Get(SRID.AxNoEventInterface, eventInterface.Name));
                }
                // IsComObject(sink): this is the case of a managed sink object wrapped in IDispatchSTAForwarder -
                // see WebBrowser.CreateSink().
                else if (sink == null || !eventInterface.IsInstanceOfType(sink) && !Marshal.IsComObject(sink))
                {
                    ex = new InvalidCastException(SR.Get(SRID.AxNoSinkImplementation, eventInterface.Name));
                }
                else
                {
                    int hr = connectionPoint.Advise(sink, ref cookie);
                    if (hr != NativeMethods.S_OK)
                    {
                        cookie = 0;
                        Marshal.FinalReleaseComObject(connectionPoint);
                        connectionPoint = null;
                        ex = new InvalidOperationException(SR.Get(SRID.AxNoSinkAdvise, eventInterface.Name, hr));
                    }
                }
            }
            else
            {
                ex = new InvalidCastException(SR.Get(SRID.AxNoConnectionPointContainer));
            }


            if (connectionPoint == null || cookie == 0)
            {
                if (connectionPoint != null)
                {
                    Marshal.FinalReleaseComObject(connectionPoint);
                }

                if (ex == null)
                {
                    throw new ArgumentException(SR.Get(SRID.AxNoConnectionPoint, eventInterface.Name));
                }
                else
                {
                    throw ex;
                }
            }
        }