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

  1. Check the errorCode: ERROR_ACCESS_DENIED (5) means the account lacks job-enumeration rights; ERROR_INVALID_HANDLE (6) means the handle was invalid/closed.
  2. Re-open the printer via OpenPrinter with sufficient access rights before enumerating jobs.
  3. Verify the printer still exists and the spooler is running before calling getJobInfo1.
  4. 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

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


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)