{"record":{"id":"d99170d21f1ac22d","repo":"java-native-access/jna","slug":"win32exception-error-code-from-getlasterror-after-enumjobs","errorCode":null,"errorMessage":"Win32Exception (error code from GetLastError after EnumJobs)","messagePattern":"Win32Exception \\(error code from GetLastError after EnumJobs\\)","errorType":"error_code","errorClass":"Win32Exception","httpStatus":null,"severity":"error","filePath":"contrib/platform/src/com/sun/jna/platform/win32/WinspoolUtil.java","lineNumber":182,"sourceCode":"        IntByReference pcReturned = new IntByReference();\n        Winspool.INSTANCE.EnumJobs(phPrinter.getValue(), 0, 255, 1, null, 0,\n                pcbNeeded, pcReturned);\n        if (pcbNeeded.getValue() <= 0) {\n            return new JOB_INFO_1[0];\n        }\n\n        int lastError = ERROR_SUCCESS;\n        JOB_INFO_1 pJobEnum;\n        do {\n            pJobEnum = new JOB_INFO_1(pcbNeeded.getValue());\n            if (!Winspool.INSTANCE.EnumJobs(phPrinter.getValue(), 0, 255, 1,\n                    pJobEnum.getPointer(), pcbNeeded.getValue(), pcbNeeded,\n                    pcReturned)) {\n                lastError = Kernel32.INSTANCE.GetLastError();\n            }\n        } while (lastError == ERROR_INSUFFICIENT_BUFFER);\n        if (lastError != ERROR_SUCCESS) {\n            throw new Win32Exception(lastError);\n        }\n        if (pcReturned.getValue() <= 0) {\n            return new JOB_INFO_1[0];\n        }\n\n        pJobEnum.read();\n\n        return (JOB_INFO_1[]) pJobEnum.toArray(pcReturned.getValue());\n    }\n\n}","sourceCodeStart":164,"sourceCodeEnd":193,"githubUrl":"https://github.com/java-native-access/jna/blob/d036ad9781adad4b66693e8fa7098e4ac665e0a3/contrib/platform/src/com/sun/jna/platform/win32/WinspoolUtil.java#L164-L193","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nJOB_INFO_1[] jobs = WinspoolUtil.getJobInfo1(handle);\n// after\ntry {\n    JOB_INFO_1[] jobs = WinspoolUtil.getJobInfo1(handle);\n} catch (Win32Exception e) {\n    LOG.warn(\"EnumJobs failed: \" + e.getErrorCode());\n    jobs = new JOB_INFO_1[0];\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n    JOB_INFO_1[] jobs = WinspoolUtil.getJobInfo1(handle);\n} catch (Win32Exception e) {\n    if (e.getErrorCode() == 5) { /* no job permission */ }\n    jobs = new JOB_INFO_1[0];\n}","preventionTips":["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."],"tags":["windows","printing","win32","jobs"],"backgroundTag":"api-error-response","analyzedSha":"d036ad9781adad4b66693e8fa7098e4ac665e0a3","analyzedAt":"2026-09-12T06:50:59.239Z","contentChangedAt":"2026-09-12T06:50:59.239Z","schemaVersion":2},"datasetVersion":"2026-09-19T12:17:13.211Z"}