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;
                }
            }
        }
Exemplo n.º 2
0
            internal ConnectionPointCookie(object source, object sink, Type eventInterface, bool throwException) {
                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 {
                        connectionPoint = null;
                    }

                    if (connectionPoint == null) {
                        if (throwException) {
                            throw new ArgumentException(SR.GetString(SR.AXNoEventInterface, eventInterface.Name));
                        }
                    }
                    else if (sink == null || !eventInterface.IsInstanceOfType(sink)) {
                        if (throwException) {
                            throw new InvalidCastException(SR.GetString(SR.AXNoSinkImplementation, eventInterface.Name));
                        }
                    }
                    else {
                        int hr = connectionPoint.Advise(sink, ref cookie);
                        if (hr == NativeMethods.S_OK) {
                            threadId = Thread.CurrentThread.ManagedThreadId;
                        }
                        else {
                            cookie = 0;
                            Marshal.ReleaseComObject(connectionPoint);
                            connectionPoint = null;
                            if (throwException) {
                                throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, SR.GetString(SR.AXNoSinkAdvise, eventInterface.Name), hr));
                            }
                        }
                    }
                }
                else {
                    if (throwException) {
                        throw new InvalidCastException(SR.GetString(SR.AXNoConnectionPointContainer));
                    }
                }


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

                    if (throwException) {
                        throw new ArgumentException(SR.GetString(SR.AXNoConnectionPoint, eventInterface.Name));
                    }
                }
#if DEBUG
                new EnvironmentPermission(PermissionState.Unrestricted).Assert();
                try {
                    callStack = Environment.StackTrace;
                }
                finally {
                    System.Security.CodeAccessPermission.RevertAssert();
                }
#endif
            }
Exemplo n.º 3
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;
                }
            }
        }
            /// <include file='doc\NativeMethods.uex' path='docs/doc[@for="NativeMethods.ConnectionPointCookie.ConnectionPointCookie1"]/*' />
            /// <devdoc>
            /// Creates a connection point to of the given interface type.
            /// which will call on a managed code sink that implements that interface.
            /// </devdoc>
            public ConnectionPointCookie(object source, object sink, Type eventInterface, bool throwException, out bool connected){
                connected = false;
                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) {
                        connectionPoint = null;
                    }

                    if (connectionPoint == null) {
                        ex = new ArgumentException(SR.GetString(SR.ConnPointSourceIF, eventInterface.Name ));
                    }
                    else if (sink == null || !eventInterface.IsInstanceOfType(sink)) {
                        ex = new InvalidCastException(SR.GetString(SR.ConnPointSinkIF));
                    }
                    else {
                        int hr = connectionPoint.Advise(sink, ref cookie);
                        if (hr != S_OK) {
                            cookie = 0;
                            Marshal.ReleaseComObject(connectionPoint);
                            connectionPoint = null;
                            ex = new ExternalException(SR.GetString(SR.ConnPointAdviseFailed, eventInterface.Name, hr ));
                        }
                        else {
                            connected = true;
                        }
                    }
                }
                else {
                    ex = new InvalidCastException(SR.GetString(SR.ConnPointSourceIF, "IConnectionPointContainer"));
                }


                if (throwException && (connectionPoint == null || cookie == 0)) {
                    if (connectionPoint != null) {
                        Marshal.ReleaseComObject(connectionPoint);
                    }

                    if (ex == null) {
                        throw new ArgumentException(SR.GetString(SR.ConnPointCouldNotCreate, eventInterface.Name ));
                    }
                    else {
                        throw ex;
                    }
                }

                #if DEBUG
                new EnvironmentPermission(PermissionState.Unrestricted).Assert();
                try {
                    callStack = Environment.StackTrace;
                }
                finally {
                    System.Security.CodeAccessPermission.RevertAssert();
                }
                #endif
            }
Exemplo n.º 5
0
            internal ConnectionPointCookie(object source, object sink, Type eventInterface, bool throwException)
            {
                if (source is UnsafeNativeMethods.IConnectionPointContainer cpc)
                {
                    try
                    {
                        Guid tmp = eventInterface.GUID;
                        if (cpc.FindConnectionPoint(ref tmp, out connectionPoint) != NativeMethods.S_OK)
                        {
                            connectionPoint = null;
                        }
                    }
                    catch
                    {
                        connectionPoint = null;
                    }

                    if (connectionPoint == null)
                    {
                        if (throwException)
                        {
                            throw new ArgumentException(string.Format(SR.AXNoEventInterface, eventInterface.Name));
                        }
                    }
                    else if (sink == null || !eventInterface.IsInstanceOfType(sink))
                    {
                        if (throwException)
                        {
                            throw new InvalidCastException(string.Format(SR.AXNoSinkImplementation, eventInterface.Name));
                        }
                    }
                    else
                    {
                        int hr = connectionPoint.Advise(sink, ref cookie);
                        if (hr == NativeMethods.S_OK)
                        {
                            threadId = Thread.CurrentThread.ManagedThreadId;
                        }
                        else
                        {
                            cookie = 0;
                            Marshal.ReleaseComObject(connectionPoint);
                            connectionPoint = null;
                            if (throwException)
                            {
                                throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, string.Format(SR.AXNoSinkAdvise, eventInterface.Name), hr));
                            }
                        }
                    }
                }
                else
                {
                    if (throwException)
                    {
                        throw new InvalidCastException(SR.AXNoConnectionPointContainer);
                    }
                }

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

                    if (throwException)
                    {
                        throw new ArgumentException(string.Format(SR.AXNoConnectionPoint, eventInterface.Name));
                    }
                }
#if DEBUG
                callStack = Environment.StackTrace;
#endif
            }