java-native-access/jna · error · Win32Exception
Win32Exception from native GetLastError when GetThreadPriori
Error message
Win32Exception from native GetLastError when GetThreadPriority returned invalid priority
What it means
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().
Source
Thrown at contrib/platform/src/com/sun/jna/platform/win32/Kernel32Util.java:1313
*/
public static void setCurrentProcessBackgroundMode(final boolean enable) {
// Note: PROCESS_MODE_BACKGROUN_{BEGIN,END} only works with the "current" process handle!
final DWORD dwPriorityClass = enable ? Kernel32.PROCESS_MODE_BACKGROUND_BEGIN : Kernel32.PROCESS_MODE_BACKGROUND_END;
if (!Kernel32.INSTANCE.SetPriorityClass(Kernel32.INSTANCE.GetCurrentProcess(), dwPriorityClass)) {
throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
}
}
/**
* Gets the priority value of the current thread.
*
* @return The priority value of the current thread.
* @throws Win32Exception if an error occurs.
*/
public static int getCurrentThreadPriority() {
final int nPriority = Kernel32.INSTANCE.GetThreadPriority(Kernel32.INSTANCE.GetCurrentThread());
if (!isValidThreadPriority(nPriority)) {
throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
}
return nPriority;
}
/**
* Sets the priority value for the current thread.
*
* @param nPriority The priority value for the thread.
* @throws Win32Exception if an error occurs.
*/
public static void setCurrentThreadPriority(final int nPriority) {
if (!isValidThreadPriority(nPriority)) {
throw new IllegalArgumentException("The given priority value is invalid!");
}
if (!Kernel32.INSTANCE.SetThreadPriority(Kernel32.INSTANCE.GetCurrentThread(), nPriority)) {
throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
}
}View on GitHub (pinned to d036ad9781)
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.
Example fix
// before
int prio = Kernel32Util.getCurrentThreadPriority();
// after
int prio;
try {
prio = Kernel32Util.getCurrentThreadPriority();
} catch (Win32Exception e) {
LOG.warn("Thread priority query failed: " + e.getErrorCode());
prio = WinBase.THREAD_PRIORITY_NORMAL;
} Defensive patterns
Strategy: fallback
Validate before calling
if (!Platform.isWindows()) {
throw new IllegalStateException("getCurrentThreadPriority is Windows-only");
} Try / catch
int threadPriority;
try {
threadPriority = Kernel32Util.getCurrentThreadPriority();
} catch (Win32Exception e) {
log.warn("GetThreadPriority failed ({}), assuming NORMAL", e.getErrorCode());
threadPriority = WinBase.THREAD_PRIORITY_NORMAL;
} Prevention
- 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.
When it happens
Trigger: 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).
Common situations: Security-hardened environments stripping thread access rights; native-layer failures; test harnesses forcing GetThreadPriority to return THREAD_PRIORITY_ERROR_RETURN.
Understand the failure class
Background: Permission denied / not authorized / 403 Forbidden: access-control rejections when the caller lacks the required role, grant, or ownership — this error's family across 18 libraries.
Related errors
- Win32Exception from native GetLastError after SetThreadPrior
- Win32Exception from native GetLastError after ExpandEnvironm
- Win32Exception from native GetLastError after SetThreadPrior
- LookupAccountNameW was expected to fail with ERROR_INSUFFICI
- Expected GetTokenInformation to fail with ERROR_INSUFFICIENT
AI-assisted analysis of java-native-access/jna@d036ad9781 (2026-09-12).
Data as JSON: /api/errors/100a01c6d2dd00af.
Report an issue: GitHub.