printing - How can i get a printer's make and model in Java? -
i'm working on java application shares printers server, , need application make , model of printers shares.
i know question has been asked 3 or 4 times nobody seems have found answer.
i've tried code :
printservice[] printservices = printservicelookup.lookupprintservices(null, null); (printservice printer : printservices){ system.out.println(printer.getdefaultattributevalue(printermakeandmodel.class)); system.out.println(printer.getattribute(printeruri.class)); }
the first print returns null string , second 1 gets nullpointerexception.
some researches lead me page : http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4673400
it seems it's know "bug" , don't understand evaluation.
i'm thinking workaround make , model sending snmp request printers don't know thing snmp , i'm not sure there single snmp command make , model of printer.
if has idea on how achieve this, either using java method or sending snmp commands or else can done on os, appreciated.
edit :
here link topic same question has been asked :
edit 2 :
solution :
as said in comment, tried make , model via snmp sending oid "1.3.6.1.2.1.25.3.2.1.3.1" printer. seems work i'm not sure works on printer using same oid , can crash if snmp deactivated on target printer.
so chose driver name using jna , winspool.drv. part of implemented in jna had add structures , functions.
here's link existing winspoolutil.java , winspool.java classes in jna.
and here's code personal update of these 2 classes.
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; } }
a main class calling 3 implemented functions , displaying result :
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); }
i kinda struggled work hope help.
if want add other functions of print spooler api, here can find references of api.
note : there still problem because application multi-platform , solution works windows. in future i'll have find solution printer drivers name on linux os , on mac os x. i'll keep posted if find something.
(cherry-picking parts of question answerable.)
it seems it's know "bug" , don't understand evaluation.
in fact, request enhancement (rfe) not bug.
the evaluation indicates reviewer thinks / has found way they implement on windows ... if , when around it. not workaround you.
this related question talks getting printer's model using snmp
you should able map solution using java snmp library. approach assumes printer networked , snmp capable ... guess figured out.
Comments
Post a Comment