{"record":{"id":"8664b55006827bc1","repo":"HandyOrg/HandyControl","slug":"iconnectionpoint-advise-returned-an-invalid-cookie","errorCode":null,"errorMessage":"IConnectionPoint::Advise returned an invalid cookie.","messagePattern":"IConnectionPoint::Advise returned an invalid cookie\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Shared/Microsoft.Windows.Shell/Standard/SafeConnectionPointCookie.cs","lineNumber":27,"sourceCode":"internal sealed class SafeConnectionPointCookie : SafeHandleZeroOrMinusOneIsInvalid\n{\n    [SuppressMessage(\"Microsoft.Naming\", \"CA2204:Literals should be spelled correctly\", MessageId = \"IConnectionPoint\")]\n    [SuppressMessage(\"Microsoft.Performance\", \"CA1811:AvoidUncalledPrivateCode\")]\n    public SafeConnectionPointCookie(IConnectionPointContainer target, object sink, Guid eventId) : base(true)\n    {\n        Verify.IsNotNull<IConnectionPointContainer>(target, \"target\");\n        Verify.IsNotNull<object>(sink, \"sink\");\n        Verify.IsNotDefault<Guid>(eventId, \"eventId\");\n        this.handle = IntPtr.Zero;\n        IConnectionPoint connectionPoint = null;\n        try\n        {\n            target.FindConnectionPoint(ref eventId, out connectionPoint);\n            int num;\n            connectionPoint.Advise(sink, out num);\n            if (num == 0)\n            {\n                throw new InvalidOperationException(\"IConnectionPoint::Advise returned an invalid cookie.\");\n            }\n            this.handle = new IntPtr(num);\n            this._cp = connectionPoint;\n            connectionPoint = null;\n        }\n        finally\n        {\n            Utility.SafeRelease<IConnectionPoint>(ref connectionPoint);\n        }\n    }\n\n    [SuppressMessage(\"Microsoft.Performance\", \"CA1811:AvoidUncalledPrivateCode\")]\n    public void Disconnect()\n    {\n        this.ReleaseHandle();\n    }\n\n    [SuppressMessage(\"Microsoft.Design\", \"CA1031:DoNotCatchGeneralExceptionTypes\")]","sourceCodeStart":9,"sourceCodeEnd":45,"githubUrl":"https://github.com/HandyOrg/HandyControl/blob/2c0875ebd67326e0c67282967e3e809c69282fee/src/Shared/Microsoft.Windows.Shell/Standard/SafeConnectionPointCookie.cs#L9-L45","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Verify the event interface IID passed to the constructor is supported by the target COM object","Check that the COM object is fully initialized before subscribing","Registation/repair the COM component if Advise misbehaves","Wrap construction in try-catch and fall back to polling or alternate event mechanisms"],"exampleFix":"// before\nvar cookie = new SafeConnectionPointCookie(target, sink, typeof(DWebBrowserEvents2).GUID); // throws if cookie==0\n// after\nif (target == null) throw new ArgumentNullException(\"target\");\ntry { var cookie = new SafeConnectionPointCookie(target, sink, typeof(DWebBrowserEvents2).GUID); }\ncatch (InvalidOperationException) { /* subscribe via alternate mechanism */ }","handlingStrategy":"try-catch","validationCode":"if (target == null || sink == null) throw new ArgumentNullException(nameof(target));\n// verify the connection point exists before constructing the cookie\ntarget.FindConnectionPoint(ref eventIid, out var cp);","typeGuard":"static bool IsAdviseCookieValid(int cookie) => cookie != 0;","tryCatchPattern":"try { cookie = new SafeConnectionPointCookie(target, sink, iid); }\ncatch (InvalidOperationException ex) { Log.Warn(\"COM Advise failed with invalid cookie\", ex); UseAlternateEventSource(); }","preventionTips":["Confirm the COM object supports the event interface IID before subscribing","Keep COM object references alive for the lifetime of the subscription","Test on a clean machine to rule out broken COM registration"],"tags":["com-interop","connection-point","advise","events"],"backgroundTag":"unexpected-response-shape","analyzedSha":"2c0875ebd67326e0c67282967e3e809c69282fee","analyzedAt":"2026-09-14T14:45:29.754Z","contentChangedAt":"2026-09-14T14:45:29.754Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}