HandyOrg/HandyControl · error · InvalidOperationException

IConnectionPoint::Advise returned an invalid cookie.

Error message

IConnectionPoint::Advise returned an invalid cookie.

What it means

SafeConnectionPointCookie's constructor subscribes a COM event sink via IConnectionPoint::Advise. If Advise succeeds but returns a cookie of 0 (an invalid registration handle), the wrapper throws InvalidOperationException because it cannot later unadvise the sink.

Solutions

  1. Verify the event interface IID passed to the constructor is supported by the target COM object
  2. Check that the COM object is fully initialized before subscribing
  3. Registation/repair the COM component if Advise misbehaves
  4. Wrap construction in try-catch and fall back to polling or alternate event mechanisms

Example fix

// before
var cookie = new SafeConnectionPointCookie(target, sink, typeof(DWebBrowserEvents2).GUID); // throws if cookie==0
// after
if (target == null) throw new ArgumentNullException("target");
try { var cookie = new SafeConnectionPointCookie(target, sink, typeof(DWebBrowserEvents2).GUID); }
catch (InvalidOperationException) { /* subscribe via alternate mechanism */ }
Defensive patterns

Strategy: try-catch

Validate before calling

if (target == null || sink == null) throw new ArgumentNullException(nameof(target));
// verify the connection point exists before constructing the cookie
target.FindConnectionPoint(ref eventIid, out var cp);

Type guard

static bool IsAdviseCookieValid(int cookie) => cookie != 0;

Try / catch

try { cookie = new SafeConnectionPointCookie(target, sink, iid); }
catch (InvalidOperationException ex) { Log.Warn("COM Advise failed with invalid cookie", ex); UseAlternateEventSource(); }

Prevention

When it happens

Trigger: Calling new SafeConnectionPointCookie(...) (typically via a COM event helper like GetConnectionPoint) on a COM object whose FindConnectionPoint/Advise pair returns a zero cookie — e.g. the source object does not properly register the connection.

Common situations: COM interop with IE/Shell objects (e.g. Internet Explorer automation, taskbar/event hooks) where the target component misbehaves or the event interface IID is wrong; running in environments with broken COM registration.

Related errors


AI-assisted analysis of HandyOrg/HandyControl@2c0875ebd6 (2026-09-14). Data as JSON: /api/errors/8664b55006827bc1. Report an issue: GitHub.

Appendix: source

Thrown at src/Shared/Microsoft.Windows.Shell/Standard/SafeConnectionPointCookie.cs:27

internal sealed class SafeConnectionPointCookie : SafeHandleZeroOrMinusOneIsInvalid
{
    [SuppressMessage("Microsoft.Naming", "CA2204:Literals should be spelled correctly", MessageId = "IConnectionPoint")]
    [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
    public SafeConnectionPointCookie(IConnectionPointContainer target, object sink, Guid eventId) : base(true)
    {
        Verify.IsNotNull<IConnectionPointContainer>(target, "target");
        Verify.IsNotNull<object>(sink, "sink");
        Verify.IsNotDefault<Guid>(eventId, "eventId");
        this.handle = IntPtr.Zero;
        IConnectionPoint connectionPoint = null;
        try
        {
            target.FindConnectionPoint(ref eventId, out connectionPoint);
            int num;
            connectionPoint.Advise(sink, out num);
            if (num == 0)
            {
                throw new InvalidOperationException("IConnectionPoint::Advise returned an invalid cookie.");
            }
            this.handle = new IntPtr(num);
            this._cp = connectionPoint;
            connectionPoint = null;
        }
        finally
        {
            Utility.SafeRelease<IConnectionPoint>(ref connectionPoint);
        }
    }

    [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
    public void Disconnect()
    {
        this.ReleaseHandle();
    }

    [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]

View on GitHub (pinned to 2c0875ebd6)