java-native-access/jna · error · Win32Exception
Win32Exception (error code from GetLastError after EnumJobs)
Error message
Win32Exception (error code from GetLastError after EnumJobs)
What it means
WinspoolUtil.getJobInfo1() calls EnumJobs to list print jobs for a printer handle. The loop retries on ERROR_INSUFFICIENT_BUFFER, but if any other error occurs (lastError != ERROR_SUCCESS after the retry loop), it throws a Win32Exception carrying that GetLastError code. Enumerating the printer's job queue failed.
Solutions
- Check the errorCode: ERROR_ACCESS_DENIED (5) means the account lacks job-enumeration rights; ERROR_INVALID_HANDLE (6) means the handle was invalid/closed.
- Re-open the printer via OpenPrinter with sufficient access rights before enumerating jobs.
- Verify the printer still exists and the spooler is running before calling getJobInfo1.
- Catch Win32Exception and return an empty job list when job info is optional.
Example fix
// before
JOB_INFO_1[] jobs = WinspoolUtil.getJobInfo1(handle);
// after
try {
JOB_INFO_1[] jobs = WinspoolUtil.getJobInfo1(handle);
} catch (Win32Exception e) {
LOG.warn("EnumJobs failed: " + e.getErrorCode());
jobs = new JOB_INFO_1[0];
} Defensive patterns
Strategy: try-catch
Try / catch
try {
JOB_INFO_1[] jobs = WinspoolUtil.getJobInfo1(handle);
} catch (Win32Exception e) {
if (e.getErrorCode() == 5) { /* no job permission */ }
jobs = new JOB_INFO_1[0];
} Prevention
- Keep the printer handle open and valid for the duration of job enumeration.
- Request adequate access rights (e.g. PRINTER_ACCESS_USE) at OpenPrinter time.
- Retry once with a reopened handle if ERROR_INVALID_HANDLE occurs.
- Handle printers with no jobs as the empty-array case the API already provides.
When it happens
Trigger: Calling getJobInfo1 with an invalid printer handle, a printer with ERROR_INVALID_PARAMETER conditions, or a printer that is out of scope / access denied for job enumeration.
Common situations: Printer handle obtained from a printer that was since closed or removed; querying jobs on a network printer without job-read permission; spooler service issues.
Related errors
- Win32Exception (error code from GetLastError after…
- Expected GetTokenInformation to fail with…
- Failed to access the print server
- Failed to find privilege
- Failed to get a change handle
AI-assisted analysis of java-native-access/jna@d036ad9781 (2026-09-12).
Data as JSON: /api/errors/d99170d21f1ac22d.
Report an issue: GitHub.
Appendix: source
Thrown at contrib/platform/src/com/sun/jna/platform/win32/WinspoolUtil.java:182
IntByReference pcReturned = new IntByReference();
Winspool.INSTANCE.EnumJobs(phPrinter.getValue(), 0, 255, 1, null, 0,
pcbNeeded, pcReturned);
if (pcbNeeded.getValue() <= 0) {
return new JOB_INFO_1[0];
}
int lastError = ERROR_SUCCESS;
JOB_INFO_1 pJobEnum;
do {
pJobEnum = new JOB_INFO_1(pcbNeeded.getValue());
if (!Winspool.INSTANCE.EnumJobs(phPrinter.getValue(), 0, 255, 1,
pJobEnum.getPointer(), pcbNeeded.getValue(), pcbNeeded,
pcReturned)) {
lastError = Kernel32.INSTANCE.GetLastError();
}
} while (lastError == ERROR_INSUFFICIENT_BUFFER);
if (lastError != ERROR_SUCCESS) {
throw new Win32Exception(lastError);
}
if (pcReturned.getValue() <= 0) {
return new JOB_INFO_1[0];
}
pJobEnum.read();
return (JOB_INFO_1[]) pJobEnum.toArray(pcReturned.getValue());
}
}View on GitHub (pinned to d036ad9781)