java-native-access/jna · error · Win32Exception
Win32Exception(Kernel32.INSTANCE.GetLastError())
Error message
Win32Exception(Kernel32.INSTANCE.GetLastError())
What it means
PsapiUtil.enumProcesses lists process IDs with PSAPI EnumProcesses, growing the buffer until it holds all PIDs. When EnumProcesses returns FALSE the library throws Win32Exception built from Kernel32 GetLastError, carrying the Win32 error code and message.
Solutions
- Read the Win32 error code from Win32Exception; ERROR_ACCESS_DENIED means run with sufficient privileges
- Run the application under an account permitted to enumerate processes (elevated or a standard service account)
- Catch Win32Exception and degrade gracefully (e.g. return an empty/known list) if enumeration is non-essential
- Verify no security software injects/fails PSAPI calls; test on a clean host
Example fix
// before
int[] pids = PsapiUtil.enumProcesses();
// after
int[] pids;
try {
pids = PsapiUtil.enumProcesses();
} catch (Win32Exception e) {
LOG.warn("enumProcesses failed: " + e.getErrorCode());
pids = new int[0];
} Defensive patterns
Strategy: try-catch
Try / catch
try { return PsapiUtil.enumProcesses(); } catch (Win32Exception e) { LOG.warn("EnumProcesses failed: " + e.getErrorCode()); return new int[0]; } Prevention
- Run under an account permitted to enumerate processes
- Expect failure in hardened/containerized Windows environments and degrade gracefully
- Check the wrapped Win32 error code to distinguish access denied from transient failures
When it happens
Trigger: EnumProcesses failing with FALSE — e.g. ERROR_ACCESS_DENIED in restricted security contexts, or a native/PSAPI failure while sizing the buffer in the growth loop (size grows by 1024 entries per iteration).
Common situations: Running inside restricted service accounts or hardened environments where process enumeration is blocked; sandboxed/containerized Windows processes with limited API access; debugging hooks (antivirus) interfering with PSAPI calls.
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(Native.getLastError())
- Error occured when trying get Unknown Id
- getUnknownId
- PdhException(result)
- Ras32Exception(err)
AI-assisted analysis of java-native-access/jna@d036ad9781 (2026-09-12).
Data as JSON: /api/errors/2caeb3fc1ab580cc.
Report an issue: GitHub.
Appendix: source
Thrown at contrib/platform/src/com/sun/jna/platform/win32/PsapiUtil.java:53
*
* @author Torbjörn Svensson, azoff[at]svenskalinuxforeningen.se
*/
public abstract class PsapiUtil {
/**
* Retrieves the process identifier for each process object in the system.
*
* @return Array of pids
*/
public static int[] enumProcesses() {
int size = 0;
int[] lpidProcess = null;
IntByReference lpcbNeeded = new IntByReference();
do {
size += 1024;
lpidProcess = new int[size];
if (!Psapi.INSTANCE.EnumProcesses(lpidProcess, size * DWORD.SIZE, lpcbNeeded)) {
throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
}
} while (size == lpcbNeeded.getValue() / DWORD.SIZE);
return Arrays.copyOf(lpidProcess, lpcbNeeded.getValue() / DWORD.SIZE);
}
/**
* Retrieves the name of the executable file for the specified process.
*
* @param hProcess
* A handle to the process. The handle must have the
* PROCESS_QUERY_INFORMATION or PROCESS_QUERY_LIMITED_INFORMATION
* access right. For more information, see Process Security and
* Access Rights. <br>
* Windows Server 2003 and Windows XP: The handle must have the
* PROCESS_QUERY_INFORMATION access right.
* @return ame of the executable file for the specified process.
* @throws Win32Exception in case an error occursView on GitHub (pinned to d036ad9781)