{"record":{"id":"2f3c90e58bbd3efe","repo":"java-native-access/jna","slug":"error-occured-when-trying-get-unknown-id","errorCode":null,"errorMessage":"Error occured when trying get Unknown Id ","messagePattern":"Error occured when trying get Unknown Id ","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":127,"sourceCode":"                HRESULT hr = ProxyObject.this.getRawDispatch().QueryInterface(new REFIID(iid), ppvObject);\n\n                if (WinNT.S_OK.equals(hr)) {\n                    Dispatch dispatch = new Dispatch(ppvObject.getValue());\n                    this.unknownId = Pointer.nativeValue(dispatch.getPointer());\n                    // QueryInterface returns a COM object pointer with a +1\n                    // reference, we must drop one,\n                    // Note: createProxy adds one;\n                    int n = dispatch.Release();\n                } else {\n                    String formatMessageFromHR = Kernel32Util.formatMessage(hr);\n                    throw new COMException(\"getUnknownId: \" + formatMessageFromHR, hr);\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 when trying get Unknown Id \", e);\n                }\n            }\n        }\n        return this.unknownId;\n    }\n\n    @Override\n    protected void finalize() throws Throwable {\n        this.dispose();\n        super.finalize();\n    }\n\n    public synchronized void dispose() {\n        if (((Dispatch) this.rawDispatch).getPointer() != Pointer.NULL) {\n            this.rawDispatch.Release();\n            ((Dispatch) this.rawDispatch).setPointer(Pointer.NULL);\n            factory.unregister(this);\n        }","sourceCodeStart":109,"sourceCodeEnd":145,"githubUrl":"https://github.com/java-native-access/jna/blob/d036ad9781adad4b66693e8fa7098e4ac665e0a3/contrib/platform/src/com/sun/jna/platform/win32/COM/util/ProxyObject.java#L109-L145","documentation":"This COMException wraps any unexpected RuntimeException raised inside ProxyObject.getUnknownId() while fetching the object's IUnknown id. COMExceptions are rethrown as-is; anything else (e.g. NPE, IllegalStateException) is wrapped with this generic message and the original cause attached. It indicates the id lookup failed for a reason other than a plain failed HRESULT.","triggerScenarios":"A RuntimeException such as NullPointerException or IllegalStateException occurs during getUnknownId() — e.g. the internal dispatch field is null because the proxy was disposed, or QueryInterface threw an unexpected runtime error.","commonSituations":"Using a proxy after unadvise/dispose lifecycle teardown; proxy created in one thread and disposed in another; NPE from annotation lookups on a malformed interface class.","solutions":["Inspect getCause() of the thrown COMException to find the real RuntimeException","Verify the proxy has not been disposed/released before calling methods that need the unknown id","Use the proxy only from the thread/apartment that created it","Catch COMException and log the full stack trace including the cause"],"exampleFix":"// before\nproxy.getIdentifer(); // after dispose -> generic wrapper\n// after\nFactory factory = new Factory(COMUtils.getCurrentCOM()) { ... };\n// keep factory/dispatch alive while using proxy; call HandlerUtil helpers","handlingStrategy":"try-catch","validationCode":"if (!COMUtils.comIsInitialized()) { throw new IllegalStateException(\"COM not initialized\"); }","typeGuard":"boolean alive = handler != null && handler.getObject() != null;","tryCatchPattern":"try { ... } catch (COMException e) { Throwable c = e.getCause(); LOG.error(\"getUnknownId wrapper failed\", c); }","preventionTips":["Always inspect getCause() on wrapper COMExceptions","Avoid using proxies after factory/dispatch disposal","Keep object lifecycle tied to the COM apartment"],"tags":["com","windows","jni","wrapper"],"backgroundTag":"internal-invariant-violation","analyzedSha":"d036ad9781adad4b66693e8fa7098e4ac665e0a3","analyzedAt":"2026-09-12T06:50:59.239Z","contentChangedAt":"2026-09-12T06:50:59.239Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}