{"record":{"id":"b48743ef6f7ed6d9","repo":"java-native-access/jna","slug":"win32exception-from-native-getlasterror-after-b48743","errorCode":null,"errorMessage":"Win32Exception from native GetLastError after SetPriorityClass failed","messagePattern":"Win32Exception from native GetLastError after SetPriorityClass failed","errorType":"error_code","errorClass":"Win32Exception","httpStatus":null,"severity":"error","filePath":"contrib/platform/src/com/sun/jna/platform/win32/Kernel32Util.java","lineNumber":1286,"sourceCode":"        final DWORD dwPriorityClass = Kernel32.INSTANCE.GetPriorityClass(Kernel32.INSTANCE.GetCurrentProcess());\n        if (!isValidPriorityClass(dwPriorityClass)) {\n            throw new Win32Exception(Kernel32.INSTANCE.GetLastError());\n        }\n        return dwPriorityClass;\n    }\n\n    /**\n     * Sets the priority class for the current process.\n     *\n     * @param dwPriorityClass The priority class for the process.\n     * @throws Win32Exception if an error occurs.\n     */\n    public static void setCurrentProcessPriority(final DWORD dwPriorityClass) {\n        if (!isValidPriorityClass(dwPriorityClass)) {\n            throw new IllegalArgumentException(\"The given priority value is invalid!\");\n        }\n        if (!Kernel32.INSTANCE.SetPriorityClass(Kernel32.INSTANCE.GetCurrentProcess(), dwPriorityClass)) {\n            throw new Win32Exception(Kernel32.INSTANCE.GetLastError());\n        }\n    }\n\n    /**\n     * Enables or disables \"background\" processing mode for the current process\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 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    /**","sourceCodeStart":1268,"sourceCodeEnd":1304,"githubUrl":"https://github.com/java-native-access/jna/blob/d036ad9781adad4b66693e8fa7098e4ac665e0a3/contrib/platform/src/com/sun/jna/platform/win32/Kernel32Util.java#L1268-L1304","documentation":"setCurrentProcessPriority(DWORD) throws this Win32Exception when the argument passes isValidPriorityClass() but the native SetPriorityClass call returns FALSE. The exception carries the GetLastError() code, most commonly ERROR_ACCESS_DENIED, because raising priority (especially REALTIME) requires elevated privileges.","triggerScenarios":"Calling Kernel32Util.setCurrentProcessPriority() with a valid class like REALTIME_PRIORITY_CLASS or HIGH_PRIORITY_CLASS from a non-elevated process on Windows; SetPriorityClass fails and GetLastError() is non-zero.","commonSituations":"Applications trying to boost their own priority without administrator rights; UAC-restricted processes; job objects or group policies limiting priority changes; attempting REALTIME_PRIORITY_CLASS which normally requires SeIncreaseBasePriorityPrivilege.","solutions":["Run the process elevated (administrator) when setting HIGH or REALTIME priority classes.","Prefer ABOVE_NORMAL_PRIORITY_CLASS instead of REALTIME/HIGH — it usually succeeds without elevation.","Inspect Win32Exception.getErrorCode() (ERROR_ACCESS_DENIED = 5) and degrade gracefully.","Wrap the call in try-catch and keep the current priority if the change fails."],"exampleFix":"// before\nKernel32Util.setCurrentProcessPriority(Kernel32.REALTIME_PRIORITY_CLASS);\n// after\ntry {\n    Kernel32Util.setCurrentProcessPriority(Kernel32.HIGH_PRIORITY_CLASS);\n} catch (Win32Exception e) {\n    LOG.warn(\"Priority change denied (code \" + e.getErrorCode() + \"), running at normal priority\");\n}","handlingStrategy":"try-catch","validationCode":"if (!Kernel32Util.isValidPriorityClass(dwPriorityClass)) {\n    throw new IllegalArgumentException(\"invalid priority class\");\n}\n// REALTIME typically requires elevation:\nboolean elevated = Kernel32.REALTIME_PRIORITY_CLASS.equals(dwPriorityClass) && !isAdmin();\nif (elevated) { /* request elevation or downgrade class */ }","typeGuard":null,"tryCatchPattern":"try {\n    Kernel32Util.setCurrentProcessPriority(cls);\n} catch (Win32Exception e) {\n    if (e.getErrorCode() == WinError.ERROR_ACCESS_DENIED) {\n        log.warn(\"Priority change denied; continuing at current priority\");\n    } else {\n        throw e;\n    }\n}","preventionTips":["Don't request REALTIME_PRIORITY_CLASS from non-elevated processes.","Prefer ABOVE_NORMAL_PRIORITY_CLASS as the highest non-elevated choice.","Treat priority changes as best-effort; never make correctness depend on them.","Check ERROR_ACCESS_DENIED (5) and document required privileges."],"tags":["win32","jna","windows","process-priority","access-denied"],"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"}