实际上,我工作的共享打印机服务器的Java应用程序,我需要这个应用来获得它共享打印机的品牌和型号。
我知道这个问题已经被问了三分四次,但没有人似乎已经找到了答案。
我试过这样的代码:
PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null, null);
for (PrintService printer : printServices){
System.out.println(printer.getDefaultAttributeValue(PrinterMakeAndModel.class));
System.out.println(printer.getAttribute(PrinterURI.class));
}
第一印刷总是返回一个空字符串,第二个得到一个NullPointerException。
一些研究使我这个网页: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4673400
看起来这是一个已知的“错误”,我真的不明白的评价。
我想一个解决办法是通过发送SNMP请求到打印机,以获得品牌和型号,但我不知道关于SNMP的事情,我不知道有一个SNMP命令来获取品牌和型号的任何打印机。
如果有人有关于如何实现这一点的想法,或者通过使用Java方法或通过发送可以在任何操作系统上进行SNMP命令或其他任何东西,你的帮助,将不胜感激。
编辑:
下面是到同一个问题已经被问了一个主题的链接:
- 如何获得的javax.print打印机的型号?
编辑2:
解决方案:
正如我在评论说,我试图通过发送OID“1.3.6.1.2.1.25.3.2.1.3.1”到打印机获得通过SNMP的品牌和型号。 它似乎工作,但我不知道它的工作原理使用相同的OID任何打印机上,如果SNMP是目标打印机上禁用它可能会崩溃。
所以,我最终选择了用JNA和WINSPOOL.DRV获得驱动程序的名称。 它的一部分在JNA已经实现,但我不得不添加一些结构和功能。
这里的链接到现有WinspoolUtil.java和Winspool.java在JNA类。
下面是我个人的这两个类更新的代码。
WINSPOOL:
import java.util.Arrays;
import java.util.List;
import com.sun.jna.Memory;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.Structure;
import com.sun.jna.platform.win32.WinDef.DWORD;
import com.sun.jna.platform.win32.WinDef.INT_PTR;
import com.sun.jna.platform.win32.WinNT.HANDLE;
import com.sun.jna.platform.win32.WinNT.HANDLEByReference;
import com.sun.jna.ptr.IntByReference;
import com.sun.jna.win32.StdCallLibrary;
import com.sun.jna.win32.W32APIOptions;
public class WinspoolUpdate {
public interface WinspoolLib extends StdCallLibrary {
WinspoolLib INSTANCE = (WinspoolLib) Native.loadLibrary("Winspool.drv", WinspoolLib.class,
W32APIOptions.UNICODE_OPTIONS);
boolean EnumPrinters(int Flags, String Name, int Level, Pointer pPrinterEnum,
int cbBuf, IntByReference pcbNeeded, IntByReference pcReturned);
boolean GetPrinter(HANDLE hPrinter, int Level, Pointer pPrinter, int cbBuf, IntByReference pcbNeeded);
boolean OpenPrinter(String pPrinterName, HANDLEByReference phPrinter, Pointer pDefault);
public static class PRINTER_INFO_1 extends Structure {
public int Flags;
public String pDescription;
public String pName;
public String pComment;
protected List<String> getFieldOrder() {
return Arrays.asList(new String[] { "Flags", "pDescription", "pName", "pComment" });
}
public PRINTER_INFO_1() {
}
public PRINTER_INFO_1(int size) {
super(new Memory(size));
}
}
public static class PRINTER_INFO_2 extends Structure {
public String pServerName;
public String pPrinterName;
public String pShareName;
public String pPortName;
public String pDriverName;
public String pComment;
public String pLocation;
public INT_PTR pDevMode;
public String pSepFile;
public String pPrintProcessor;
public String pDatatype;
public String pParameters;
public INT_PTR pSecurityDescriptor;
public int Attributes;
public int Priority;
public int DefaultPriority;
public int StartTime;
public int UntilTime;
public int Status;
public int cJobs;
public int AveragePPM;
protected List<String> getFieldOrder() {
return Arrays.asList(new String[] { "pServerName", "pPrinterName", "pShareName", "pPortName",
"pDriverName", "pComment", "pLocation", "pDevMode", "pSepFile", "pPrintProcessor",
"pDatatype", "pParameters", "pSecurityDescriptor", "Attributes", "Priority", "DefaultPriority",
"StartTime", "UntilTime", "Status", "cJobs", "AveragePPM" });
}
public PRINTER_INFO_2() {
}
public PRINTER_INFO_2(int size) {
super(new Memory(size));
}
}
public static class PRINTER_INFO_4 extends Structure {
public String pPrinterName;
public String pServerName;
public DWORD Attributes;
protected List<String> getFieldOrder() {
return Arrays.asList(new String[] { "pPrinterName", "pServerName", "Attributes" });
}
public PRINTER_INFO_4() {
}
public PRINTER_INFO_4(int size) {
super(new Memory(size));
}
}
int PRINTER_ENUM_DEFAULT = 0x00000001;
int PRINTER_ENUM_LOCAL = 0x00000002;
int PRINTER_ENUM_CONNECTIONS = 0x00000004;
int PRINTER_ENUM_FAVORITE = 0x00000004;
int PRINTER_ENUM_NAME = 0x00000008;
int PRINTER_ENUM_REMOTE = 0x00000010;
int PRINTER_ENUM_SHARED = 0x00000020;
int PRINTER_ENUM_NETWORK = 0x00000040;
int PRINTER_ENUM_EXPAND = 0x00004000;
int PRINTER_ENUM_CONTAINER = 0x00008000;
int PRINTER_ENUM_ICONMASK = 0x00ff0000;
int PRINTER_ENUM_ICON1 = 0x00010000;
int PRINTER_ENUM_ICON2 = 0x00020000;
int PRINTER_ENUM_ICON3 = 0x00040000;
int PRINTER_ENUM_ICON4 = 0x00080000;
int PRINTER_ENUM_ICON5 = 0x00100000;
int PRINTER_ENUM_ICON6 = 0x00200000;
int PRINTER_ENUM_ICON7 = 0x00400000;
int PRINTER_ENUM_ICON8 = 0x00800000;
int PRINTER_ENUM_HIDE = 0x01000000;
}
}
WinspoolUtil:
import Model.WinspoolUpdate.WinspoolLib;
import Model.WinspoolUpdate.WinspoolLib.PRINTER_INFO_2;
import com.sun.jna.platform.win32.Kernel32;
import com.sun.jna.platform.win32.Win32Exception;
import com.sun.jna.platform.win32.WinNT.HANDLEByReference;
import com.sun.jna.platform.win32.Winspool.PRINTER_INFO_1;
import com.sun.jna.platform.win32.Winspool.PRINTER_INFO_4;
import com.sun.jna.ptr.IntByReference;
public class WinspoolUtils2 {
public static PRINTER_INFO_1[] getPrinterInfo1() {
IntByReference pcbNeeded = new IntByReference();
IntByReference pcReturned = new IntByReference();
WinspoolLib.INSTANCE.EnumPrinters(WinspoolLib.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 (!WinspoolLib.INSTANCE.EnumPrinters(WinspoolLib.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_2[] getPrinterInfo2() {
IntByReference pcbNeeded = new IntByReference();
IntByReference pcReturned = new IntByReference();
WinspoolLib.INSTANCE.EnumPrinters(WinspoolLib.PRINTER_ENUM_LOCAL,
null, 2, null, 0, pcbNeeded, pcReturned);
if (pcbNeeded.getValue() <= 0) {
return new PRINTER_INFO_2[0];
}
PRINTER_INFO_2 pPrinterEnum = new PRINTER_INFO_2(pcbNeeded.getValue());
if (!WinspoolLib.INSTANCE.EnumPrinters(WinspoolLib.PRINTER_ENUM_LOCAL,
null, 2, pPrinterEnum.getPointer(), pcbNeeded.getValue(), pcbNeeded, pcReturned)) {
throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
}
pPrinterEnum.read();
return (PRINTER_INFO_2[]) pPrinterEnum.toArray(pcReturned.getValue());
}
public static PRINTER_INFO_4[] getPrinterInfo4() {
IntByReference pcbNeeded = new IntByReference();
IntByReference pcReturned = new IntByReference();
WinspoolLib.INSTANCE.EnumPrinters(WinspoolLib.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 (!WinspoolLib.INSTANCE.EnumPrinters(WinspoolLib.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());
}
public static PRINTER_INFO_2 getPrinterInfo2(String printerName) {
IntByReference pcbNeeded = new IntByReference();
IntByReference pcReturned = new IntByReference();
HANDLEByReference pHandle = new HANDLEByReference();
WinspoolLib.INSTANCE.OpenPrinter(printerName, pHandle, null);
WinspoolLib.INSTANCE.GetPrinter(pHandle.getValue(), 2, null, 0, pcbNeeded);
if (pcbNeeded.getValue() <= 0) {
return new PRINTER_INFO_2();
}
PRINTER_INFO_2 pinfo2 = new PRINTER_INFO_2(pcbNeeded.getValue());
WinspoolLib.INSTANCE.GetPrinter(pHandle.getValue(), 2, pinfo2.getPointer(), pcbNeeded.getValue(), pcReturned);
pinfo2.read();
return (PRINTER_INFO_2) pinfo2;
}
}
一个主类调用这三个实现的功能,并显示结果:
public static void main(String[] args) {
for(PRINTER_INFO_1 printerInfo : WinspoolUtils2.getPrinterInfo1()) {
System.out.println(printerInfo.pName + ": " + printerInfo.pDescription);
}
for(PRINTER_INFO_2 printerInfo : WinspoolUtils2.getPrinterInfo2()) {
System.out.println(printerInfo.pPrinterName + ": " + printerInfo.pDriverName);
}
PRINTER_INFO_2 printerInfo = WinspoolUtils2.getPrinterInfo2("Canon iR-ADV C7000s GX300 V2.0");
System.out.println(printerInfo.pPrinterName + ": " + printerInfo.pDriverName);
}
我有点挣扎,终于得到它的工作,所以我希望这将有助于。
如果您要添加的打印后台处理程序API的一些其他功能, 在这里你可以找到这个API的引用。
注:还有一个问题,因为我想这个应用程序是多平台,这个解决方案仅适用于Windows。 因此,在未来我会找到一个解决方案,以获得打印机驱动程序在Linux操作系统和Mac OS X上的名字,我会及时向大家发布,如果我找到的东西。