Skip to content

Commit

Permalink
Fixed bug in WinspoolUtil.getPrinterInfo1() and WinspoolUtil.getPrint…
Browse files Browse the repository at this point in the history
…erInfo4() where the first element's fields was never populated
  • Loading branch information
unknown authored and unknown committed Jan 18, 2012
1 parent 636b01b commit cbc16d2
Showing 1 changed file with 41 additions and 36 deletions.
77 changes: 41 additions & 36 deletions contrib/platform/src/com/sun/jna/platform/win32/WinspoolUtil.java
Expand Up @@ -18,43 +18,48 @@

/**
* Winspool Utility API.
*
* @author dblock[at]dblock.org
*/
public abstract class WinspoolUtil {

public static PRINTER_INFO_1[] getPrinterInfo1() {
IntByReference pcbNeeded = new IntByReference();
IntByReference pcReturned = new IntByReference();
Winspool.INSTANCE.EnumPrinters(Winspool.PRINTER_ENUM_LOCAL,
null, 1, null, 0, pcbNeeded, pcReturned);
if (pcbNeeded.getValue() <= 0) {
return new PRINTER_INFO_1[0];
}

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

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

public static PRINTER_INFO_4[] getPrinterInfo4() {
IntByReference pcbNeeded = new IntByReference();
IntByReference pcReturned = new IntByReference();
Winspool.INSTANCE.EnumPrinters(Winspool.PRINTER_ENUM_LOCAL,
null, 4, null, 0, pcbNeeded, pcReturned);
if (pcbNeeded.getValue() <= 0) {
return new PRINTER_INFO_4[0];
}

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

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

public static PRINTER_INFO_1[] getPrinterInfo1() {
IntByReference pcbNeeded = new IntByReference();
IntByReference pcReturned = new IntByReference();
Winspool.INSTANCE.EnumPrinters(Winspool.PRINTER_ENUM_LOCAL,
null, 1, null, 0, pcbNeeded, pcReturned);
if (pcbNeeded.getValue() <= 0) {
return new PRINTER_INFO_1[0];
}

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

pPrinterEnum.read();

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

public static PRINTER_INFO_4[] getPrinterInfo4() {
IntByReference pcbNeeded = new IntByReference();
IntByReference pcReturned = new IntByReference();
Winspool.INSTANCE.EnumPrinters(Winspool.PRINTER_ENUM_LOCAL,
null, 4, null, 0, pcbNeeded, pcReturned);
if (pcbNeeded.getValue() <= 0) {
return new PRINTER_INFO_4[0];
}

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

pPrinterEnum.read();

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

0 comments on commit cbc16d2

Please sign in to comment.