windows - USB-Printer printing progress information -
i need write program finds connected usb-printers of windows pc , send file selected printer list. these 2 points no problem , work pretty fine
first point
var printerquery = new managementobjectsearcher("select * win32_printer"); = 1; foreach (var printer in printerquery.get()) { var name = printer.getpropertyvalue("name"); var status = printer.getpropertyvalue("status"); var isdefault = printer.getpropertyvalue("default"); var isnetworkprinter = printer.getpropertyvalue("network"); var portname = printer.getpropertyvalue("portname"); bool connected = (bool)printer.getpropertyvalue("workoffline"); var caption = printer.getpropertyvalue("caption"); string s = "name: " + name; string nbr = ""; if (i.tostring().length < 2) nbr = "0"; nbr += i.tostring(); listboxusbinfo.items.add(nbr + ": " + s); listboxusbinfo.items.add(" status: " + status); listboxusbinfo.items.add(" default: " + isdefault); listboxusbinfo.items.add(" network: " + isnetworkprinter); listboxusbinfo.items.add(" portname: " + portname); if ( connected) listboxusbinfo.items.add(" connected: true"); if (!connected) listboxusbinfo.items.add(" connected: false"); listboxusbinfo.items.add(" caption: " + caption); i++; }
second point
is quite easy:
the class "rawprinterhelper
" known in forum, think
rawprinterhelper.sendfiletoprinter("printername", "filename");
but problem.
i must print large files (more 100.000 pages) , operator wants see, how many pages printed. possible information? example every 3 seconds, can display @ on screen.
here more information rawprinterhelper class. contains no callback. what must define callback function?
public class rawprinterhelper { // structure , api declarions: [structlayout(layoutkind.sequential, charset = charset.ansi)] public class docinfoa { [marshalas(unmanagedtype.lpstr)] public string pdocname; [marshalas(unmanagedtype.lpstr)] public string poutputfile; [marshalas(unmanagedtype.lpstr)] public string pdatatype; } [dllimport("winspool.drv", entrypoint = "openprintera", setlasterror = true, charset = charset.ansi, exactspelling = true, callingconvention = callingconvention.stdcall)] public static extern bool openprinter([marshalas(unmanagedtype.lpstr)] string szprinter, out intptr hprinter, intptr pd); [dllimport("winspool.drv", entrypoint = "closeprinter", setlasterror = true, exactspelling = true, callingconvention = callingconvention.stdcall)] public static extern bool closeprinter(intptr hprinter); [dllimport("winspool.drv", entrypoint = "startdocprintera", setlasterror = true, charset = charset.ansi, exactspelling = true, callingconvention = callingconvention.stdcall)] public static extern bool startdocprinter(intptr hprinter, int32 level, [in, marshalas(unmanagedtype.lpstruct)] docinfoa di); [dllimport("winspool.drv", entrypoint = "enddocprinter", setlasterror = true, exactspelling = true, callingconvention = callingconvention.stdcall)] public static extern bool enddocprinter(intptr hprinter); [dllimport("winspool.drv", entrypoint = "startpageprinter", setlasterror = true, exactspelling = true, callingconvention = callingconvention.stdcall)] public static extern bool startpageprinter(intptr hprinter); [dllimport("winspool.drv", entrypoint = "endpageprinter", setlasterror = true, exactspelling = true, callingconvention = callingconvention.stdcall)] public static extern bool endpageprinter(intptr hprinter); [dllimport("winspool.drv", entrypoint = "writeprinter", setlasterror = true, exactspelling = true, callingconvention = callingconvention.stdcall)] public static extern bool writeprinter(intptr hprinter, intptr pbytes, int32 dwcount, out int32 dwwritten); // sendbytestoprinter() // when function given printer name , unmanaged array // of bytes, function sends bytes print queue. // returns true on success, false on failure. public static bool sendbytestoprinter(string szprintername, intptr pbytes, int32 dwcount) { int32 dwerror = 0, dwwritten = 0; intptr hprinter = new intptr(0); docinfoa di = new docinfoa(); bool bsuccess = false; // assume failure unless succeed. di.pdocname = "my c#.net raw document"; di.pdatatype = "raw"; // open printer. if (openprinter(szprintername.normalize(), out hprinter, intptr.zero)) { // start document. if (startdocprinter(hprinter, 1, di)) { // start page. if (startpageprinter(hprinter)) { // write bytes. bsuccess = writeprinter(hprinter, pbytes, dwcount, out dwwritten); endpageprinter(hprinter); } enddocprinter(hprinter); } closeprinter(hprinter); } // if did not succeed, getlasterror may give more information // why not. if (bsuccess == false) { dwerror = marshal.getlastwin32error(); } return bsuccess; } public static bool sendfiletoprinter(string szprintername, string szfilename) { // open file. filestream fs = new filestream(szfilename, filemode.open); // create binaryreader on file. binaryreader br = new binaryreader(fs); // dim array of bytes big enough hold file's contents. byte[] bytes = new byte[fs.length]; bool bsuccess = false; // unmanaged pointer. intptr punmanagedbytes = new intptr(0); int nlength; nlength = convert.toint32(fs.length); // read contents of file array. bytes = br.readbytes(nlength); // allocate unmanaged memory bytes. punmanagedbytes = marshal.alloccotaskmem(nlength); // copy managed byte array unmanaged array. marshal.copy(bytes, 0, punmanagedbytes, nlength); // send unmanaged bytes printer. bsuccess = sendbytestoprinter(szprintername, punmanagedbytes, nlength); // free unmanaged memory allocated earlier. marshal.freecotaskmem(punmanagedbytes); fs.close(); return bsuccess; } public static bool sendstringtoprinter(string szprintername, string szstring) { intptr pbytes; int32 dwcount; // how many characters in string? dwcount = szstring.length; // assume printer expecting ansi text, , convert // string ansi text. pbytes = marshal.stringtocotaskmemansi(szstring); // send converted ansi string printer. sendbytestoprinter(szprintername, pbytes, dwcount); marshal.freecotaskmem(pbytes); return true; } } // public class rawprinterhelper
Comments
Post a Comment