java-native-access/jna · error · com.sun.jna.platform.win32.COM.COMException

Error occured in advise when trying to connect the listener

Error message

Error occured in advise when trying to connect the listener <comEventCallbackListener>

What it means

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().

Source

Thrown at contrib/platform/src/com/sun/jna/platform/win32/COM/util/ProxyObject.java:321

            // collected.
            comEventCallbackListener.setDispatchCallbackListener(rawListener);
            // set the dispatch listener to listen to events from the connection
            // point
            final DWORDByReference pdwCookie = new DWORDByReference();
            HRESULT hr = rawCp.Advise(rawListener, pdwCookie);
            int n = rawCp.Release(); // release before check in case check
            // throws exception
            COMUtils.checkRC(hr);

            // return the cookie so that a call to stop listening can be made
            return new ComEventCallbackCookie(pdwCookie.getValue());

        } catch (RuntimeException e) {
            // Do not rewrap COMException
            if (e instanceof COMException) {
                throw e;
            } else {
                throw new COMException("Error occured in advise when trying to connect the listener " + comEventCallbackListener, e);
            }
        }
    }

    @Override
    public void unadvise(Class<?> comEventCallbackInterface, final IComEventCallbackCookie cookie) throws COMException {
        assert COMUtils.comIsInitialized() : "COM not initialized";

        try {
            ComInterface comInterfaceAnnotation = comEventCallbackInterface.getAnnotation(ComInterface.class);
            if (null == comInterfaceAnnotation) {
                throw new COMException(
                        "unadvise: Interface must define a value for iid via the ComInterface annotation");
            }
            IID iid = this.getIID(comInterfaceAnnotation);

            final ConnectionPoint rawCp = this.fetchRawConnectionPoint(iid);

View on GitHub (pinned to d036ad9781)

Solutions

  1. Inspect getCause() to find the real failure (QueryInterface failure, connection point not found)
  2. Confirm the @ComInterface iid matches an event sink IID the object actually supports
  3. Verify the COM object supports IConnectionPointContainer (EnumConnectionPoints)
  4. Keep the proxy alive and on its original thread while advising

Example fix

// before
proxy.advise(WrongIidEvents.class, listener); // fails
// after
@ComInterface(iid="{correct-sink-guid}")
interface SinkEvents extends IComEventCallbackListener { ... }
proxy.advise(SinkEvents.class, listener);
Defensive patterns

Strategy: try-catch

Validate before calling

if (!iface.isAnnotationPresent(ComInterface.class)) throw new IllegalArgumentException("missing @ComInterface");

Type guard

boolean canAdvise = iface.isAnnotationPresent(ComInterface.class) && !iface.getAnnotation(ComInterface.class).iid().isEmpty();

Try / catch

try { proxy.advise(iface, listener); } catch (COMException e) { LOG.error("advise failed for " + listener, e.getCause()); }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "API request failed": what wrapped HTTP errors from external APIs mean and how to find the real cause — this error's family across 29 libraries.

Related errors


AI-assisted analysis of java-native-access/jna@d036ad9781 (2026-09-12). Data as JSON: /api/errors/c139eb793c629346. Report an issue: GitHub.