示例#1
0
        /// <summary>
        ///     Advises the specified connection point container.
        /// </summary>
        /// <param name="connectionPointContainer">The connection point container.</param>
        /// <exception cref="ArgumentException">An feature progress connection point could not be found.</exception>
        public void Advise(IConnectionPointContainer connectionPointContainer)
        {
            IEnumConnectionPoints enumConnectionPoints;

            connectionPointContainer.EnumConnectionPoints(out enumConnectionPoints);
            enumConnectionPoints.Reset();

            IConnectionPoint connectionPoint;
            var guid = typeof(IFeatureProgress).GUID;

            uint pcFetched;

            enumConnectionPoints.RemoteNext(1, out connectionPoint, out pcFetched);
            while (connectionPoint != null)
            {
                Guid connectionGuid;
                connectionPoint.GetConnectionInterface(out connectionGuid);

                if (connectionGuid == guid)
                {
                    break;
                }

                enumConnectionPoints.RemoteNext(1, out connectionPoint, out pcFetched);
            }

            if (connectionPoint == null)
            {
                throw new ArgumentException("An feature progress connection point could not be found.");
            }

            uint connectionPointCookie;

            connectionPoint.Advise(this, out connectionPointCookie);
        }
        /// <summary>
        /// Creates a connection point to of the given interface type,
        /// which will call on a managed code sink that implements that interface.
        /// </summary>
        /// <param name='source'>
        /// The object that exposes the events. This object must implement IConnectionPointContainer or an InvalidCastException will be thrown.
        /// </param>
        /// <param name='sink'>
        /// The object to sink the events. This object must implement the interface eventInterface, or an InvalidCastException is thrown.
        /// </param>
        /// <param name='iid'>
        /// The GUID of the event interface to sink. The sink object must support this interface and the source object must expose it through it's ConnectionPointContainer.
        /// </param>
        /// <param name='throwException'>
        /// If true, exceptions described will be thrown, otherwise object will silently fail to connect.
        /// </param>
        public ConnectionPointCookie(object source, object sink, Guid iid, bool throwException)
        {
            Exception ex = null;

            if (source is IConnectionPointContainer)
            {
                IConnectionPointContainer cpc = (IConnectionPointContainer)source;
                IEnumConnectionPoints     checkPoints;
                cpc.EnumConnectionPoints(out checkPoints);
                try
                {
                    Guid tmp = iid;
                    cpc.FindConnectionPoint(ref tmp, out connectionPoint);
                }
                catch (Exception)
                {
                    connectionPoint = null;
                }

                if (connectionPoint == null)
                {
                    ex = new ArgumentException("The source object does not expose the " + iid + " event interface");
                }
//                else if (!Type.GetTypeFromCLSID(iid).IsInstanceOfType(sink) && !(iid.Equals(typeof(Interop.IPropertyNotifySink).GUID)))
//                {
//                    ex = new InvalidCastException("The sink object does not implement the eventInterface");
//                }
                else
                {
                    try
                    {
                        connectionPoint.Advise(sink, out cookie);
                    }
                    catch (Exception)
                    {
                        cookie          = 0;
                        connectionPoint = null;
                        ex = new Exception("IConnectionPoint::Advise failed for event interface '" + iid + "'");
                    }
                }
            }
            else
            {
                ex = new InvalidCastException("The source object does not expost IConnectionPointContainer");
            }

            if (throwException && (connectionPoint == null || cookie == 0))
            {
                if (ex == null)
                {
                    throw new ArgumentException("Could not create connection point for event interface '" + iid + "'");
                }
                else
                {
                    throw ex;
                }
            }
        }
示例#3
0
        /// <summary>
        /// try to find connection point by EnumConnectionPoints
        /// </summary>
        private static string EnumConnectionPoint(ICOMObject comInstance, IConnectionPointContainer connectionPointContainer, ref IConnectionPoint point, params string[] sinkIds)
        {
            IConnectionPoint[]    points     = new IConnectionPoint[1];
            IEnumConnectionPoints enumPoints = null;

            try
            {
                connectionPointContainer.EnumConnectionPoints(out enumPoints);
                while (enumPoints.Next(1, points, IntPtr.Zero) == 0) // S_OK = 0 , S_FALSE = 1
                {
                    if (null == points[0])
                    {
                        break;
                    }

                    Guid interfaceGuid;
                    points[0].GetConnectionInterface(out interfaceGuid);

                    for (int i = sinkIds.Length; i > 0; i--)
                    {
                        string id = interfaceGuid.ToString().Replace("{", "").Replace("}", "");
                        if (true == sinkIds[i - 1].Equals(id, StringComparison.InvariantCultureIgnoreCase))
                        {
                            Marshal.ReleaseComObject(enumPoints);
                            enumPoints = null;
                            point      = points[0];
                            return(id);
                        }
                    }
                }
                return(null);
            }
            catch (Exception throwedException)
            {
                comInstance.Console.WriteException(throwedException);
                return(null);
            }
            finally
            {
                if (null != enumPoints)
                {
                    Marshal.ReleaseComObject(enumPoints);
                }
            }
        }
示例#4
0
        /// <summary>
        /// try to find connection point by EnumConnectionPoints
        /// </summary>
        private static string EnumConnectionPoint(COMObject comInstance, IConnectionPointContainer connectionPointContainer, ref IConnectionPoint point, params string[] sinkIds)
        {
            IConnectionPoint[] points = new IConnectionPoint[1];
            IEnumConnectionPoints enumPoints = null;
            try
            {
                connectionPointContainer.EnumConnectionPoints(out enumPoints);
                while (enumPoints.Next(1, points, IntPtr.Zero) == 0) // S_OK = 0 , S_FALSE = 1
                {
                    if (null == points[0])
                        break;

                    Guid interfaceGuid;
                    points[0].GetConnectionInterface(out interfaceGuid);

                    for (int i = sinkIds.Length; i > 0; i--)
                    {
                        string id = interfaceGuid.ToString().Replace("{", "").Replace("}", "");
                        if (true == sinkIds[i - 1].Equals(id, StringComparison.InvariantCultureIgnoreCase))
                        {
                            Marshal.ReleaseComObject(enumPoints);
                            enumPoints = null;
                            point = points[0];
                            return id;
                        }
                    }
                }
                return null;
            }
            catch (Exception throwedException)
            {
                comInstance.Console.WriteException(throwedException);
                return null;
            }
            finally
            {
                if (null != enumPoints)
                    Marshal.ReleaseComObject(enumPoints);
            }
        }