{"record":{"id":"c139eb793c629346","repo":"java-native-access/jna","slug":"error-occured-in-advise-when-trying-to-connect-the-listener","errorCode":null,"errorMessage":"Error occured in advise when trying to connect the listener <comEventCallbackListener>","messagePattern":"Error occured in advise when trying to connect the listener <comEventCallbackListener>","errorType":"exception","errorClass":"com.sun.jna.platform.win32.COM.COMException","httpStatus":null,"severity":"error","filePath":"contrib/platform/src/com/sun/jna/platform/win32/COM/util/ProxyObject.java","lineNumber":321,"sourceCode":"            // collected.\n            comEventCallbackListener.setDispatchCallbackListener(rawListener);\n            // set the dispatch listener to listen to events from the connection\n            // point\n            final DWORDByReference pdwCookie = new DWORDByReference();\n            HRESULT hr = rawCp.Advise(rawListener, pdwCookie);\n            int n = rawCp.Release(); // release before check in case check\n            // throws exception\n            COMUtils.checkRC(hr);\n\n            // return the cookie so that a call to stop listening can be made\n            return new ComEventCallbackCookie(pdwCookie.getValue());\n\n        } catch (RuntimeException e) {\n            // Do not rewrap COMException\n            if (e instanceof COMException) {\n                throw e;\n            } else {\n                throw new COMException(\"Error occured in advise when trying to connect the listener \" + comEventCallbackListener, e);\n            }\n        }\n    }\n\n    @Override\n    public void unadvise(Class<?> comEventCallbackInterface, final IComEventCallbackCookie cookie) throws COMException {\n        assert COMUtils.comIsInitialized() : \"COM not initialized\";\n\n        try {\n            ComInterface comInterfaceAnnotation = comEventCallbackInterface.getAnnotation(ComInterface.class);\n            if (null == comInterfaceAnnotation) {\n                throw new COMException(\n                        \"unadvise: Interface must define a value for iid via the ComInterface annotation\");\n            }\n            IID iid = this.getIID(comInterfaceAnnotation);\n\n            final ConnectionPoint rawCp = this.fetchRawConnectionPoint(iid);\n","sourceCodeStart":303,"sourceCodeEnd":339,"githubUrl":"https://github.com/java-native-access/jna/blob/d036ad9781adad4b66693e8fa7098e4ac665e0a3/contrib/platform/src/com/sun/jna/platform/win32/COM/util/ProxyObject.java#L303-L339","documentation":"This COMException is the generic wrapper thrown by ProxyObject.advise() when connecting the event listener fails with a non-COMException RuntimeException. It wraps the original exception as the cause and includes the listener object in the message. The actual failure (e.g. missing connection point, IID mismatch) is in getCause().","triggerScenarios":"advise() fails during fetchRawConnectionPoint, FindConnectionPoint, or IConnectionPoint.Advise due to unexpected runtime exceptions — e.g. the COM object does not support IConnectionPointContainer or the IID has no matching connection point.","commonSituations":"Subscribing to events on a COM object that doesn't expose the requested event interface; typos in the @ComInterface iid; calling advise after the COM object was released.","solutions":["Inspect getCause() to find the real failure (QueryInterface failure, connection point not found)","Confirm the @ComInterface iid matches an event sink IID the object actually supports","Verify the COM object supports IConnectionPointContainer (EnumConnectionPoints)","Keep the proxy alive and on its original thread while advising"],"exampleFix":"// before\nproxy.advise(WrongIidEvents.class, listener); // fails\n// after\n@ComInterface(iid=\"{correct-sink-guid}\")\ninterface SinkEvents extends IComEventCallbackListener { ... }\nproxy.advise(SinkEvents.class, listener);","handlingStrategy":"try-catch","validationCode":"if (!iface.isAnnotationPresent(ComInterface.class)) throw new IllegalArgumentException(\"missing @ComInterface\");","typeGuard":"boolean canAdvise = iface.isAnnotationPresent(ComInterface.class) && !iface.getAnnotation(ComInterface.class).iid().isEmpty();","tryCatchPattern":"try { proxy.advise(iface, listener); } catch (COMException e) { LOG.error(\"advise failed for \" + listener, e.getCause()); }","preventionTips":["Confirm the object supports IConnectionPointContainer before advising","Use the exact sink IID from the type library","Keep listener/proxy lifetime synchronized"],"tags":["com","events","callback","wrapper"],"backgroundTag":"api-request-failed","analyzedSha":"d036ad9781adad4b66693e8fa7098e4ac665e0a3","analyzedAt":"2026-09-12T06:50:59.239Z","contentChangedAt":"2026-09-12T06:50:59.239Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}