{"record":{"id":"100a01c6d2dd00af","repo":"java-native-access/jna","slug":"win32exception-from-native-getlasterror-when-kernel32util","errorCode":null,"errorMessage":"Win32Exception from native GetLastError when GetThreadPriority returned invalid priority","messagePattern":"Win32Exception from native GetLastError when GetThreadPriority returned invalid priority","errorType":"error_code","errorClass":"Win32Exception","httpStatus":null,"severity":"error","filePath":"contrib/platform/src/com/sun/jna/platform/win32/Kernel32Util.java","lineNumber":1313,"sourceCode":"     */\n    public static void setCurrentProcessBackgroundMode(final boolean enable) {\n        // Note: PROCESS_MODE_BACKGROUN_{BEGIN,END} only works with the \"current\" process handle!\n        final DWORD dwPriorityClass = enable ? Kernel32.PROCESS_MODE_BACKGROUND_BEGIN : Kernel32.PROCESS_MODE_BACKGROUND_END;\n        if (!Kernel32.INSTANCE.SetPriorityClass(Kernel32.INSTANCE.GetCurrentProcess(), dwPriorityClass)) {\n            throw new Win32Exception(Kernel32.INSTANCE.GetLastError());\n        }\n    }\n\n    /**\n     * Gets the priority value of the current thread.\n     *\n     * @return The priority value of the current thread.\n     * @throws Win32Exception if an error occurs.\n     */\n    public static int getCurrentThreadPriority() {\n        final int nPriority = Kernel32.INSTANCE.GetThreadPriority(Kernel32.INSTANCE.GetCurrentThread());\n        if (!isValidThreadPriority(nPriority)) {\n            throw new Win32Exception(Kernel32.INSTANCE.GetLastError());\n        }\n        return nPriority;\n    }\n\n    /**\n     * Sets the priority value for the current thread.\n     *\n     * @param nPriority The priority value for the thread.\n     * @throws Win32Exception if an error occurs.\n     */\n    public static void setCurrentThreadPriority(final int nPriority) {\n        if (!isValidThreadPriority(nPriority)) {\n            throw new IllegalArgumentException(\"The given priority value is invalid!\");\n        }\n        if (!Kernel32.INSTANCE.SetThreadPriority(Kernel32.INSTANCE.GetCurrentThread(), nPriority)) {\n            throw new Win32Exception(Kernel32.INSTANCE.GetLastError());\n        }\n    }","sourceCodeStart":1295,"sourceCodeEnd":1331,"githubUrl":"https://github.com/java-native-access/jna/blob/d036ad9781adad4b66693e8fa7098e4ac665e0a3/contrib/platform/src/com/sun/jna/platform/win32/Kernel32Util.java#L1295-L1331","documentation":"getCurrentThreadPriority() calls GetThreadPriority on the current thread's pseudo-handle; GetThreadPriority signals failure by returning THREAD_PRIORITY_ERROR_RETURN, which the library detects via isValidThreadPriority() and reports as a Win32Exception built from GetLastError().","triggerScenarios":"Calling Kernel32Util.getCurrentThreadPriority() when GetThreadPriority fails — e.g. the thread handle is invalid or lacks THREAD_QUERY_INFORMATION rights (unusual with the current-thread pseudo-handle, but possible under strict security contexts or mocks).","commonSituations":"Security-hardened environments stripping thread access rights; native-layer failures; test harnesses forcing GetThreadPriority to return THREAD_PRIORITY_ERROR_RETURN.","solutions":["Read Win32Exception.getErrorCode() (often ERROR_ACCESS_DENIED = 5) to identify the native cause.","Use the pseudo-handle from Kernel32.INSTANCE.GetCurrentThread(), which normally always succeeds for queries.","Wrap in try-catch with a THREAD_PRIORITY_NORMAL fallback.","Check that no thread library or mocking layer is corrupting the returned priority value."],"exampleFix":"// before\nint prio = Kernel32Util.getCurrentThreadPriority();\n// after\nint prio;\ntry {\n    prio = Kernel32Util.getCurrentThreadPriority();\n} catch (Win32Exception e) {\n    LOG.warn(\"Thread priority query failed: \" + e.getErrorCode());\n    prio = WinBase.THREAD_PRIORITY_NORMAL;\n}","handlingStrategy":"fallback","validationCode":"if (!Platform.isWindows()) {\n    throw new IllegalStateException(\"getCurrentThreadPriority is Windows-only\");\n}","typeGuard":null,"tryCatchPattern":"int threadPriority;\ntry {\n    threadPriority = Kernel32Util.getCurrentThreadPriority();\n} catch (Win32Exception e) {\n    log.warn(\"GetThreadPriority failed ({}), assuming NORMAL\", e.getErrorCode());\n    threadPriority = WinBase.THREAD_PRIORITY_NORMAL;\n}","preventionTips":["Treat thread priority reads as best-effort with a THREAD_PRIORITY_NORMAL fallback.","Use the current-thread pseudo-handle so query rights are normally satisfied.","Log e.getErrorCode() to spot security contexts that deny thread queries.","Avoid building control flow that depends on successful priority reads."],"tags":["win32","jna","windows","thread-priority","native-call"],"backgroundTag":"permission-denied","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"}