{"record":{"id":"3bf5d537fe5aa187","repo":"java-native-access/jna","slug":"win32exception-error-code-from-getlasterror-after","errorCode":null,"errorMessage":"Win32Exception (error code from GetLastError after EnumPrinters)","messagePattern":"Win32Exception \\(error code from GetLastError after EnumPrinters\\)","errorType":"error_code","errorClass":"Win32Exception","httpStatus":null,"severity":"error","filePath":"contrib/platform/src/com/sun/jna/platform/win32/WinspoolUtil.java","lineNumber":56,"sourceCode":" *\n * @author dblock[at]dblock.org, Ivan Ridao Freitas, Padrus, Artem Vozhdayenko\n */\npublic abstract class WinspoolUtil {\n\n    public static PRINTER_INFO_1[] getPrinterInfo1() {\n        IntByReference pcbNeeded = new IntByReference();\n        IntByReference pcReturned = new IntByReference();\n        Winspool.INSTANCE.EnumPrinters(Winspool.PRINTER_ENUM_LOCAL, null, 1,\n                null, 0, pcbNeeded, pcReturned);\n        if (pcbNeeded.getValue() <= 0) {\n            return new PRINTER_INFO_1[0];\n        }\n\n        PRINTER_INFO_1 pPrinterEnum = new PRINTER_INFO_1(pcbNeeded.getValue());\n        if (!Winspool.INSTANCE.EnumPrinters(Winspool.PRINTER_ENUM_LOCAL, null,\n                1, pPrinterEnum.getPointer(), pcbNeeded.getValue(), pcbNeeded,\n                pcReturned)) {\n            throw new Win32Exception(Kernel32.INSTANCE.GetLastError());\n        }\n\n        pPrinterEnum.read();\n\n        return (PRINTER_INFO_1[]) pPrinterEnum.toArray(pcReturned.getValue());\n    }\n\n    public static PRINTER_INFO_2[] getPrinterInfo2() {\n        return getPrinterInfo2(Winspool.PRINTER_ENUM_LOCAL);\n    }\n\n    /**\n     * Returns printers that are physically attached to the local machine as\n     * well as remote printers to which it has a network connection.\n     */\n    public static PRINTER_INFO_2[] getAllPrinterInfo2() {\n        // When Name is NULL, setting Flags to PRINTER_ENUM_LOCAL | PRINTER_ENUM_CONNECTIONS\n        // enumerates printers that are installed on the local machine.","sourceCodeStart":38,"sourceCodeEnd":74,"githubUrl":"https://github.com/java-native-access/jna/blob/d036ad9781adad4b66693e8fa7098e4ac665e0a3/contrib/platform/src/com/sun/jna/platform/win32/WinspoolUtil.java#L38-L74","documentation":"WinspoolUtil.getPrinterInfo1() throws Win32Exception when the second (buffered) EnumPrinters call — with level 1 and PRINTER_ENUM_LOCAL — returns false. The exception is constructed from Kernel32.INSTANCE.GetLastError() so its message is the real Win32 failure (e.g. ERROR_ACCESS_DENIED, RPC failures). EnumPrinters is called twice: first to learn the needed buffer size, then to fill it; only failures of the buffered call throw here.","triggerScenarios":"Calling WinspoolUtil.getPrinterInfo1() when EnumPrinters(PRINTER_ENUM_LOCAL, null, 1, buffer, pcbNeeded, pcReturned) fails on the buffered call — spooler service (Spooler) not running, access denied, or no printers and the call returns an unexpected error.","commonSituations":"Running in a service/session where the print spooler is stopped or disabled; restricted service accounts without printer access; containers or minimal Windows installs with no print subsystem.","solutions":["Verify the Windows Print Spooler service is running (sc query spooler; net start spooler)","Check the exception's error code: ERROR_ACCESS_DENIED implies insufficient permissions — run as a user with printer access","Confirm the process runs in an interactive user session (printer enumeration requires a loaded user profile in many configurations)","Catch Win32Exception and treat 'no printers' as an empty result rather than a hard failure"],"exampleFix":"// before\nPRINTER_INFO_1[] printers = WinspoolUtil.getPrinterInfo1();\n// after\nPRINTER_INFO_1[] printers;\ntry {\n    printers = WinspoolUtil.getPrinterInfo1();\n} catch (Win32Exception e) {\n    printers = new PRINTER_INFO_1[0];\n}","handlingStrategy":"try-catch","validationCode":"// check spooler is reachable before calling\nboolean spoolerUp = runsWinSpoolerService(); // e.g. sc query spooler equivalent","typeGuard":null,"tryCatchPattern":"try {\n    printers = WinspoolUtil.getPrinterInfo1();\n} catch (Win32Exception e) {\n    log.error(\"EnumPrinters failed: \" + e.getMessage());\n    printers = new PRINTER_INFO_1[0];\n}","preventionTips":["Ensure the Print Spooler service is running","Run with a user profile and printer permissions","Handle empty-printer environments gracefully"],"tags":["winspool","windows","printers","win32-exception"],"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-14T05:17:10.506Z"}