{"record":{"id":"848ac39cd9bf0ca7","repo":"java-native-access/jna","slug":"win32exception-error-code-from-getlasterror-after-848ac3","errorCode":null,"errorMessage":"Win32Exception (error code from GetLastError after OpenPrinter)","messagePattern":"Win32Exception \\(error code from GetLastError after OpenPrinter\\)","errorType":"error_code","errorClass":"Win32Exception","httpStatus":null,"severity":"error","filePath":"contrib/platform/src/com/sun/jna/platform/win32/WinspoolUtil.java","lineNumber":105,"sourceCode":"        }\n\n        PRINTER_INFO_2 pPrinterEnum = new PRINTER_INFO_2(pcbNeeded.getValue());\n        if (!Winspool.INSTANCE.EnumPrinters(flags, null, 2, pPrinterEnum.getPointer(), pcbNeeded.getValue(), pcbNeeded,\n                pcReturned)) {\n            throw new Win32Exception(Kernel32.INSTANCE.GetLastError());\n        }\n\n        pPrinterEnum.read();\n        return (PRINTER_INFO_2[]) pPrinterEnum.toArray(pcReturned.getValue());\n    }\n\n    public static PRINTER_INFO_2 getPrinterInfo2(String printerName) {\n        IntByReference pcbNeeded = new IntByReference();\n        IntByReference pcReturned = new IntByReference();\n        HANDLEByReference pHandle = new HANDLEByReference();\n\n        if (!Winspool.INSTANCE.OpenPrinter(printerName, pHandle, null)) {\n            throw new Win32Exception(Kernel32.INSTANCE.GetLastError());\n        }\n\n        Win32Exception we = null;\n        PRINTER_INFO_2 pinfo2 = null;\n\n        try {\n            Winspool.INSTANCE.GetPrinter(pHandle.getValue(), 2, null, 0, pcbNeeded);\n            if (pcbNeeded.getValue() <= 0) {\n                return new PRINTER_INFO_2();\n            }\n\n            pinfo2 = new PRINTER_INFO_2(pcbNeeded.getValue());\n            if (!Winspool.INSTANCE.GetPrinter(pHandle.getValue(), 2, pinfo2.getPointer(), pcbNeeded.getValue(), pcReturned)) {\n                throw new Win32Exception(Kernel32.INSTANCE.GetLastError());\n            }\n\n            pinfo2.read();\n        } catch (Win32Exception e) {","sourceCodeStart":87,"sourceCodeEnd":123,"githubUrl":"https://github.com/java-native-access/jna/blob/d036ad9781adad4b66693e8fa7098e4ac665e0a3/contrib/platform/src/com/sun/jna/platform/win32/WinspoolUtil.java#L87-L123","documentation":"WinspoolUtil.getPrinterInfo2(String printerName) throws Win32Exception when the OpenPrinter call for the named printer fails. The exception carries Kernel32.INSTANCE.GetLastError(), most commonly ERROR_INVALID_PRINTER_NAME (1801) when the printer does not exist, or ERROR_ACCESS_DENIED when the caller lacks rights. Unlike errors 287/288 this happens before any EnumPrinters call, purely from resolving the printer name to a handle.","triggerScenarios":"Calling getPrinterInfo2(\"name\") with a name that is not an installed printer/share (typo, deleted printer, wrong server prefix like \\\\\\\\server\\\\printer), or without permission to open it.","commonSituations":"Hard-coded printer names that changed between machines/environments; reading a printer name from config with stale whitespace or wrong casing/server path; querying a network printer while offline or when the print server rejects the request.","solutions":["Verify the exact printer name via WinspoolUtil.getPrinterInfo1()/EnumPrinters or Control Panel, then pass it verbatim (including \\\\\\\\server\\\\share prefix for network printers)","Check the Win32 code in the exception: 1801 = invalid name (fix the name), 5 = access denied (grant printer permissions)","Trim/normalize the configured printer name before calling","Catch Win32Exception and fall back to default printer or skip printer-specific logic"],"exampleFix":"// before\nPRINTER_INFO_2 info = WinspoolUtil.getPrinterInfo2(cfgPrinter);\n// after\nPRINTER_INFO_2 info;\ntry {\n    info = WinspoolUtil.getPrinterInfo2(cfgPrinter.trim());\n} catch (Win32Exception e) {\n    if (e.getErrorCode() == WinError.ERROR_INVALID_PRINTER_NAME) {\n        info = null; // printer missing from config\n    } else {\n        throw e;\n    }\n}","handlingStrategy":"validation","validationCode":"String name = cfgPrinter == null ? null : cfgPrinter.trim();\nif (name == null || name.isEmpty()) {\n    throw new IllegalArgumentException(\"printer name required\");\n}","typeGuard":"boolean printerNamePlausible(String n) {\n    return n != null && !n.trim().isEmpty();\n}","tryCatchPattern":"try {\n    info = WinspoolUtil.getPrinterInfo2(name);\n} catch (Win32Exception e) {\n    if (e.getErrorCode() == WinError.ERROR_INVALID_PRINTER_NAME) {\n        info = null; // unknown printer\n    } else {\n        throw e;\n    }\n}","preventionTips":["Resolve printer names dynamically via EnumPrinters instead of hard-coding","Trim and normalize configured names; keep \\\\\\\\server\\\\share format for network printers","Check error code 1801 vs 5 to distinguish missing name from access denied"],"tags":["winspool","windows","openprinter","printer-not-found"],"backgroundTag":"resource-not-found","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"}