{"record":{"id":"728808d46ca52346","repo":"java-native-access/jna","slug":"win32exception-from-native-getlasterror-after-728808","errorCode":null,"errorMessage":"Win32Exception from native GetLastError after SetThreadPriority failed","messagePattern":"Win32Exception from native GetLastError after SetThreadPriority failed","errorType":"error_code","errorClass":"Win32Exception","httpStatus":null,"severity":"error","filePath":"contrib/platform/src/com/sun/jna/platform/win32/Kernel32Util.java","lineNumber":1329,"sourceCode":"        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    }\n\n    /**\n     * Enables or disables \"background\" processing mode for the current thread\n     *\n     * @param enable If true, enables \"background\" processing mode, otherwise disables it.\n     * @throws Win32Exception if an error occurs.\n     */\n    public static void setCurrentThreadBackgroundMode(final boolean enable) {\n        // Note: THREAD_MODE_BACKGROUND_{BEGIN,END} only works with the \"current\" thread handle!\n        final int nPriority = enable ? Kernel32.THREAD_MODE_BACKGROUND_BEGIN : Kernel32.THREAD_MODE_BACKGROUND_END;\n        if (!Kernel32.INSTANCE.SetThreadPriority(Kernel32.INSTANCE.GetCurrentThread(), nPriority)) {\n            throw new Win32Exception(Kernel32.INSTANCE.GetLastError());\n        }\n    }\n\n    /**","sourceCodeStart":1311,"sourceCodeEnd":1347,"githubUrl":"https://github.com/java-native-access/jna/blob/d036ad9781adad4b66693e8fa7098e4ac665e0a3/contrib/platform/src/com/sun/jna/platform/win32/Kernel32Util.java#L1311-L1347","documentation":"setCurrentThreadPriority(int) throws this Win32Exception when the priority value is valid but the native SetThreadPriority call returns FALSE, with the message carrying GetLastError(). Common native error codes include ERROR_INVALID_PARAMETER (background-mode constants used incorrectly) and ERROR_ACCESS_DENIED.","triggerScenarios":"Calling Kernel32Util.setCurrentThreadPriority() with a valid priority on a thread whose handle lacks THREAD_SET_INFORMATION rights, or passing THREAD_MODE_BACKGROUND_* constants to SetThreadPriority in a way the OS rejects.","commonSituations":"Modifying priorities of threads owned by other processes without rights; restricted tokens/services; job objects that disallow priority changes; misusing background-mode constants as priorities.","solutions":["Inspect Win32Exception.getErrorCode() to identify the native failure (5=access denied, 87=invalid parameter).","Operate on the current thread via Kernel32.INSTANCE.GetCurrentThread() pseudo-handle, which normally permits priority changes.","Wrap in try-catch and degrade to the previous priority on failure.","Ensure the thread is not in a state/job that forbids priority changes; avoid passing background-mode constants here."],"exampleFix":"// before\nKernel32Util.setCurrentThreadPriority(WinBase.THREAD_PRIORITY_HIGHEST);\n// after\ntry {\n    Kernel32Util.setCurrentThreadPriority(WinBase.THREAD_PRIORITY_HIGHEST);\n} catch (Win32Exception e) {\n    LOG.warn(\"SetThreadPriority failed (\" + e.getErrorCode() + \"), keeping current priority\");\n}","handlingStrategy":"try-catch","validationCode":"if (!Kernel32Util.isValidThreadPriority(nPriority)) {\n    throw new IllegalArgumentException(\"invalid thread priority\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    Kernel32Util.setCurrentThreadPriority(nPriority);\n} catch (Win32Exception e) {\n    if (e.getErrorCode() == WinError.ERROR_ACCESS_DENIED) {\n        log.warn(\"Thread priority change denied; keeping current priority\");\n    } else {\n        throw e;\n    }\n}","preventionTips":["Set priorities only on the current thread (pseudo-handle) to avoid access-rights issues.","Treat priority changes as best-effort hints, not guarantees.","Check ERROR_ACCESS_DENIED (5) and ERROR_INVALID_PARAMETER (87) in logs.","Never pass THREAD_MODE_BACKGROUND_* constants to setCurrentThreadPriority."],"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"}