/// <summary> /// Disconnect the current connection point. If the object is not connected, /// this method will do nothing. /// </summary> public void Disconnect() { if (connectionPoint != null && cookie != 0) { try { connectionPoint.Unadvise(cookie); } catch (Exception ex) when(!ClientUtils.IsCriticalException(ex)) { } finally { cookie = 0; } try { Marshal.ReleaseComObject(connectionPoint); } catch (Exception ex) when(!ClientUtils.IsCriticalException(ex)) { } finally { connectionPoint = null; } } }
internal unsafe ConnectionPointCookie(object source, object sink, Type eventInterface, bool throwException) { if (source is Ole32.IConnectionPointContainer cpc) { try { Guid tmp = eventInterface.GUID; if (cpc.FindConnectionPoint(&tmp, out _connectionPoint) != HRESULT.S_OK) { _connectionPoint = null; } } catch { _connectionPoint = null; } if (_connectionPoint is null) { if (throwException) { throw new ArgumentException(string.Format(SR.AXNoEventInterface, eventInterface.Name)); } } else if (sink is null || !eventInterface.IsInstanceOfType(sink)) { if (throwException) { throw new InvalidCastException(string.Format(SR.AXNoSinkImplementation, eventInterface.Name)); } } else { uint tempCookie = 0; HRESULT hr = _connectionPoint.Advise(sink, &tempCookie); if (hr == HRESULT.S_OK) { _cookie = tempCookie; _threadId = Environment.CurrentManagedThreadId; } else { _cookie = 0; Marshal.ReleaseComObject(_connectionPoint); _connectionPoint = null; if (throwException) { throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, string.Format(SR.AXNoSinkAdvise, eventInterface.Name), hr)); } } } }
internal unsafe ConnectionPointCookie(object source, object sink, Type eventInterface, bool throwException) { if (source is Ole32.IConnectionPointContainer cpc) { try { Guid tmp = eventInterface.GUID; if (cpc.FindConnectionPoint(&tmp, out connectionPoint) != HRESULT.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 { uint tempCookie = 0; HRESULT hr = connectionPoint.Advise(sink, &tempCookie); if (hr == HRESULT.S_OK) { cookie = tempCookie; 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 }