java-native-access/jna · error · Win32Exception

Win32Exception from native GetLastError when GetPriorityClas

Error message

Win32Exception from native GetLastError when GetPriorityClass returned invalid priority class

What it means

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.

Source

Thrown at contrib/platform/src/com/sun/jna/platform/win32/Kernel32Util.java:1270

        }

        if( W32APITypeMapper.DEFAULT == W32APITypeMapper.UNICODE ) {
            return resultMemory.getWideString(0);
        } else {
            return resultMemory.getString(0);
        }
    }

    /**
     * Gets the priority class of the current process.
     *
     * @return The priority class of the current process.
     * @throws Win32Exception if an error occurs.
     */
    public static DWORD getCurrentProcessPriority() {
        final DWORD dwPriorityClass = Kernel32.INSTANCE.GetPriorityClass(Kernel32.INSTANCE.GetCurrentProcess());
        if (!isValidPriorityClass(dwPriorityClass)) {
            throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
        }
        return dwPriorityClass;
    }

    /**
     * Sets the priority class for the current process.
     *
     * @param dwPriorityClass The priority class for the process.
     * @throws Win32Exception if an error occurs.
     */
    public static void setCurrentProcessPriority(final DWORD dwPriorityClass) {
        if (!isValidPriorityClass(dwPriorityClass)) {
            throw new IllegalArgumentException("The given priority value is invalid!");
        }
        if (!Kernel32.INSTANCE.SetPriorityClass(Kernel32.INSTANCE.GetCurrentProcess(), dwPriorityClass)) {
            throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
        }
    }

View on GitHub (pinned to d036ad9781)

Solutions

  1. Check Win32Exception.getErrorCode() (commonly ERROR_ACCESS_DENIED = 5) and run with sufficient privileges.
  2. Ensure the code queries the CURRENT process handle (Kernel32.INSTANCE.GetCurrentProcess()), which normally always permits the query.
  3. Wrap the call and fall back to a default priority class if the query fails.
  4. Verify no security software or job object is blocking PROCESS_QUERY_INFORMATION on the process.

Example fix

// before
DWORD prio = Kernel32Util.getCurrentProcessPriority();
// after
DWORD prio;
try {
    prio = Kernel32Util.getCurrentProcessPriority();
} catch (Win32Exception e) {
    LOG.warn("Could not read priority class: " + e.getErrorCode());
    prio = new DWORD(0x00000020); // NORMAL_PRIORITY_CLASS fallback
}
Defensive patterns

Strategy: fallback

Validate before calling

if (!Platform.isWindows()) {
    throw new IllegalStateException("getCurrentProcessPriority is Windows-only");
}

Try / catch

DWORD priorityClass;
try {
    priorityClass = Kernel32Util.getCurrentProcessPriority();
} catch (Win32Exception e) {
    log.warn("GetPriorityClass failed ({}), assuming NORMAL", e.getErrorCode());
    priorityClass = Kernel32.NORMAL_PRIORITY_CLASS;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

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


AI-assisted analysis of java-native-access/jna@d036ad9781 (2026-09-12). Data as JSON: /api/errors/c6ba288f146e3122. Report an issue: GitHub.