{"record":{"id":"c6ba288f146e3122","repo":"java-native-access/jna","slug":"win32exception-from-native-getlasterror-when","errorCode":null,"errorMessage":"Win32Exception from native GetLastError when GetPriorityClass returned invalid priority class","messagePattern":"Win32Exception from native GetLastError when GetPriorityClass returned invalid priority class","errorType":"error_code","errorClass":"Win32Exception","httpStatus":null,"severity":"error","filePath":"contrib/platform/src/com/sun/jna/platform/win32/Kernel32Util.java","lineNumber":1270,"sourceCode":"        }\n\n        if( W32APITypeMapper.DEFAULT == W32APITypeMapper.UNICODE ) {\n            return resultMemory.getWideString(0);\n        } else {\n            return resultMemory.getString(0);\n        }\n    }\n\n    /**\n     * Gets the priority class of the current process.\n     *\n     * @return The priority class of the current process.\n     * @throws Win32Exception if an error occurs.\n     */\n    public static DWORD getCurrentProcessPriority() {\n        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    }","sourceCodeStart":1252,"sourceCodeEnd":1288,"githubUrl":"https://github.com/java-native-access/jna/blob/d036ad9781adad4b66693e8fa7098e4ac665e0a3/contrib/platform/src/com/sun/jna/platform/win32/Kernel32Util.java#L1252-L1288","documentation":"getCurrentProcessPriority() calls GetPriorityClass for the current process; the Win32 API signals failure by returning 0, which the library detects indirectly via isValidPriorityClass(). When the returned DWORD is not a known priority class, the library throws a Win32Exception from native GetLastError() because the documentation states the API fails only by returning an invalid value.","triggerScenarios":"Calling Kernel32Util.getCurrentProcessPriority() when GetPriorityClass returns a value outside the known priority-class constants (e.g. 0), usually because the process handle lacks PROCESS_QUERY_INFORMATION / PROCESS_QUERY_LIMITED_INFORMATION rights.","commonSituations":"Running under a service or restricted token where the process handle does not permit priority queries; sandboxed environments; security-hardened hosts stripping process access rights.","solutions":["Check Win32Exception.getErrorCode() (commonly ERROR_ACCESS_DENIED = 5) and run with sufficient privileges.","Ensure the code queries the CURRENT process handle (Kernel32.INSTANCE.GetCurrentProcess()), which normally always permits the query.","Wrap the call and fall back to a default priority class if the query fails.","Verify no security software or job object is blocking PROCESS_QUERY_INFORMATION on the process."],"exampleFix":"// before\nDWORD prio = Kernel32Util.getCurrentProcessPriority();\n// after\nDWORD prio;\ntry {\n    prio = Kernel32Util.getCurrentProcessPriority();\n} catch (Win32Exception e) {\n    LOG.warn(\"Could not read priority class: \" + e.getErrorCode());\n    prio = new DWORD(0x00000020); // NORMAL_PRIORITY_CLASS fallback\n}","handlingStrategy":"fallback","validationCode":"if (!Platform.isWindows()) {\n    throw new IllegalStateException(\"getCurrentProcessPriority is Windows-only\");\n}","typeGuard":null,"tryCatchPattern":"DWORD priorityClass;\ntry {\n    priorityClass = Kernel32Util.getCurrentProcessPriority();\n} catch (Win32Exception e) {\n    log.warn(\"GetPriorityClass failed ({}), assuming NORMAL\", e.getErrorCode());\n    priorityClass = Kernel32.NORMAL_PRIORITY_CLASS;\n}","preventionTips":["Assume priority queries can fail under restricted tokens; always provide a NORMAL fallback.","Run diagnostic tools under the same security context as your service to reproduce failures.","Avoid relying on priority-class reads for critical control flow.","Check ERROR_ACCESS_DENIED (5) to detect under-privileged execution early at startup."],"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"}