{"record":{"id":"848567aa6f09af27","repo":"java-native-access/jna","slug":"win32exception-error-code-from-native-getlasterror-after","errorCode":null,"errorMessage":"Win32Exception (error code from Native.getLastError after FindFirstUrlCacheEntry)","messagePattern":"Win32Exception \\(error code from Native\\.getLastError after FindFirstUrlCacheEntry\\)","errorType":"error_code","errorClass":"Win32Exception","httpStatus":null,"severity":"error","filePath":"contrib/platform/src/com/sun/jna/platform/win32/WininetUtil.java","lineNumber":72,"sourceCode":"        int lastError = 0;\n\n        // return\n        Map<String, String> cacheItems = new LinkedHashMap<>();\n\n        try {\n            IntByReference size = new IntByReference();\n\n            // for every entry, we call the API twice:\n            // once to get the size into the IntByReference\n            // then again to get the actual item\n            cacheHandle = Wininet.INSTANCE.FindFirstUrlCacheEntry(null, null, size);\n            lastError = Native.getLastError();\n\n            // if there's nothing in the cache, we're done.\n            if (lastError == WinError.ERROR_NO_MORE_ITEMS) {\n                return cacheItems;\n            } else if (lastError != WinError.ERROR_SUCCESS && lastError != WinError.ERROR_INSUFFICIENT_BUFFER) {\n                throw new Win32Exception(lastError);\n            }\n\n            INTERNET_CACHE_ENTRY_INFO entry = new INTERNET_CACHE_ENTRY_INFO(size.getValue());\n            cacheHandle = Wininet.INSTANCE.FindFirstUrlCacheEntry(null, entry, size);\n\n            if (cacheHandle == null) {\n                throw new Win32Exception(Native.getLastError());\n            }\n\n            items.add(entry);\n\n            while (true) {\n                size = new IntByReference();\n\n                // for every entry, we call the API twice:\n                // once to get the size into the IntByReference\n                // then again to get the actual item\n                boolean result = Wininet.INSTANCE.FindNextUrlCacheEntry(cacheHandle, null, size);","sourceCodeStart":54,"sourceCodeEnd":90,"githubUrl":"https://github.com/java-native-access/jna/blob/d036ad9781adad4b66693e8fa7098e4ac665e0a3/contrib/platform/src/com/sun/jna/platform/win32/WininetUtil.java#L54-L90","documentation":"WininetUtil.getCache() throws a Win32Exception carrying the Win32 error code returned by Native.getLastError() when the first call to FindFirstUrlCacheEntry fails with an error other than ERROR_NO_MORE_ITEMS, ERROR_SUCCESS, or ERROR_INSUFFICIENT_BUFFER. The exception's error code maps to a human-readable Win32 message, telling you why cache enumeration could not start (e.g. access denied or the URL cache service is unavailable).","triggerScenarios":"Wininet.INSTANCE.FindFirstUrlCacheEntry(null, null, size) fails and GetLastError is not one of the three tolerated codes — e.g. ERROR_FILE_NOT_FOUND / ERROR_INTERNET_* conditions, or the WinINet cache is inaccessible for the current user/session.","commonSituations":"Running under a service account or session with no WinINet cache (cache APIs are per-user and require an interactive user profile); calling from an elevated process into a different user's cache; restricted environments where URLMON/WinINet caching is disabled by policy.","solutions":["Check the exception's Win32 error code (we.getHR().intValue() / HRESULT from code) to identify the precise cause, then address that condition (e.g. run as an interactive user with a cache)","Handle ERROR_NO_MORE_ITEMS and ERROR_INSUFFICIENT_BUFFER as non-fatal — the library already does; ensure you are not confusing a first-call buffer retry with a hard failure","Verify the process runs in a user session with a WinINet profile loaded (not a session-0 service without user profile)","Wrap getCache() in try/catch (Win32Exception) and fall back to an empty list or alternate enumeration mechanism"],"exampleFix":"// before\nList<INTERNET_CACHE_ENTRY_INFO> items = WininetUtil.getCache();\n// after\nList<INTERNET_CACHE_ENTRY_INFO> items;\ntry {\n    items = WininetUtil.getCache();\n} catch (Win32Exception e) {\n    log.warn(\"IE cache enumeration failed: \" + e.getMessage());\n    items = Collections.emptyList();\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n    items = WininetUtil.getCache();\n} catch (Win32Exception e) {\n    switch (e.getErrorCode()) { /* inspect HRESULT */ }\n    items = Collections.emptyList();\n}","preventionTips":["Only call WinINet cache APIs from an interactive user session","Treat cache data as optional/best-effort","Log the Win32 error code for diagnosis"],"tags":["wininet","windows","url-cache","win32-exception"],"backgroundTag":"api-error-response","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"}