Skip to content

Commit

Permalink
Check OpenPrinter
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanRF committed Dec 22, 2015
1 parent bbf91bd commit be67ac8
Showing 1 changed file with 7 additions and 12 deletions.
19 changes: 7 additions & 12 deletions contrib/platform/src/com/sun/jna/platform/win32/WinspoolUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,15 @@ private static PRINTER_INFO_2[] getPrinterInfo2(int flags) {
IntByReference pcbNeeded = new IntByReference();
IntByReference pcReturned = new IntByReference();
Winspool.INSTANCE.EnumPrinters(flags, null, 2, null, 0, pcbNeeded, pcReturned);
if (pcbNeeded.getValue() <= 0) {
if (pcbNeeded.getValue() <= 0)
return new PRINTER_INFO_2[0];
}

PRINTER_INFO_2 pPrinterEnum = new PRINTER_INFO_2(pcbNeeded.getValue());
if (!Winspool.INSTANCE.EnumPrinters(flags, null, 2, pPrinterEnum.getPointer(), pcbNeeded.getValue(),
pcbNeeded, pcReturned)) {
if (!Winspool.INSTANCE.EnumPrinters(flags, null, 2, pPrinterEnum.getPointer(), pcbNeeded.getValue(), pcbNeeded,
pcReturned))
throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
}

pPrinterEnum.read();

return (PRINTER_INFO_2[]) pPrinterEnum.toArray(pcReturned.getValue());
}

Expand All @@ -88,18 +85,16 @@ public static PRINTER_INFO_2 getPrinterInfo2(String printerName) {
IntByReference pcReturned = new IntByReference();
HANDLEByReference pHandle = new HANDLEByReference();

Winspool.INSTANCE.OpenPrinter(printerName, pHandle, null);
if (!Winspool.INSTANCE.OpenPrinter(printerName, pHandle, null))
throw new Win32Exception(Kernel32.INSTANCE.GetLastError());

Winspool.INSTANCE.GetPrinter(pHandle.getValue(), 2, null, 0, pcbNeeded);
if (pcbNeeded.getValue() <= 0) {
if (pcbNeeded.getValue() <= 0)
return new PRINTER_INFO_2();
}

PRINTER_INFO_2 pinfo2 = new PRINTER_INFO_2(pcbNeeded.getValue());

if (!Winspool.INSTANCE.GetPrinter(pHandle.getValue(), 2, pinfo2.getPointer(), pcbNeeded.getValue(), pcReturned)) {
if (!Winspool.INSTANCE.GetPrinter(pHandle.getValue(), 2, pinfo2.getPointer(), pcbNeeded.getValue(), pcReturned))
throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
}

pinfo2.read();
return pinfo2;
Expand Down

0 comments on commit be67ac8

Please sign in to comment.