如何查询打印机状态(100分)

  • 主题发起人 主题发起人 歪就歪
  • 开始时间 开始时间

歪就歪

Unregistered / Unconfirmed
GUEST, unregistred user!
我需要查询打印机当前的状态(在线、缺纸等),但用如下的代码每次<br>得到的STATUS都是0,毛病出哪儿了?无论我把打印机的纸拿走,或是<br>把打印机关掉,都是一个结果,我想,也许要是把打印机吞到肚子里,可<br>能让STATUS改变。<br><br>另外,Printer.Printers[0]得到的不光是打印机的名字,还有一个<br>“on Prt1”或者是"on //SomeNetworkComputerName",我只好在下<br>面代码里,用POS把它去掉。<br><br>我曾查找了Expert-Exchange,没能解决这问题,请各位大侠帮忙。<br>我给出了源码,也请麻烦回应源码,这样能立即知道是否WORK<br><br>function TForm2.CheckPrn: Integer;<br>var i: integer;<br>&nbsp; hPrn: THandle;<br>&nbsp; sPrn: String;<br>&nbsp; pInfo2: PPrinterInfo2;<br>&nbsp; pcbNeeded : DWORD;<br>&nbsp;<br>begin<br>&nbsp; GetMem(pInfo2, 1024);<br>&nbsp; sPrn := Printer.Printers[0];<br>&nbsp; i:= Pos(' on ', sPrn);<br>&nbsp; sPrn := Copy(sPrn, 1, i-1);<br>&nbsp; if OpenPrinter(PChar(sPrn), hPrn, nil) then begin<br>&nbsp; &nbsp; if GetPrinter(hPrn, 2, pInfo2, 1024, @pcbNeeded) then begin<br>&nbsp; &nbsp; &nbsp; ListBox1.Items.Add(pInfo2.pServerName);<br>&nbsp; &nbsp; &nbsp; ListBox1.Items.Add(pInfo2.pPrinterName);<br>&nbsp; &nbsp; &nbsp; ListBox1.Items.Add(pInfo2.pDriverName);<br>&nbsp; &nbsp; &nbsp; ListBox1.Items.Add(pInfo2.pParameters);<br>&nbsp; &nbsp; &nbsp; ListBox1.Items.Add(IntToStr(pInfo2.Status)); &nbsp;//这总是0<br>&nbsp; &nbsp; end;<br>&nbsp; end<br>&nbsp; else<br>&nbsp; &nbsp; showmessage('notwork');<br>end;
 
你用的95还是NT?<br>Windows NT下是不是只有这两种状态?<br>&nbsp; PRINTER_STATUS_PAUSED<br>&nbsp; PRINTER_STATUS_PENDING_DELETION <br>
 
为何不用WINDOWS API GETPRINTER,DELPHI也有一个此名的函数,但此函数非彼函数,看一下WIN32S PREFENCE,你会查到的,打印机状态应有尽有。达10种之多。<br>关于API调用你一定会,我就不罗嗦了。<br>快分分吧!我快穷死了。
 
Wrench: <br><br>Thanks, I use Win98. As matter of fact, no matter what system, I<br>should have a non-zero value on Status. But strange things is I never<br>got non zero value from GetPrinter API unless the printer do not<br>exist.<br><br><br>阿朱:<br><br>Sorry, it seems not easy money. "为何不用WINDOWS API GETPRINTER" means<br>you did NOT READ my code :-(. I just use Windows.GetPrinter, but it is not work. In above my code, I got status from PRINTER_INFO_2. But<br>it always 0. that is my question.<br><br>Heeeeeeeeeeeelp!<br><br>
 
help! please continue...
 
Heart is too soft to suffer [Xin(1) Tai(4) Ruan(3)]<br><br>.......<br>It is late night without sleep, are you still waiting for her?<br>[Ye(4) shen(1) le ni(3) hai(2) bu(4) xiang(2) shui(4), <br>ni(3) hai(2) zai(4) xiang(3) zhe(4) ta me<br><br>I am waiting...<br><br>More money for answer, 100 more dollar for this question.<br>
 
Hello? there is still no answer on this question.<br><br>Please continue. &nbsp;Total question value 200.
 
我现在没打印机<br>爱莫能助
 
我想这么多款...,总有一款适合你:)<br><br>var <br>pinfo: pPRINTERINFO2; <br>pprt,a: Cardinal; <br>b:PDWORD; <br>begin <br>b:=allocmem(256); <br>pinfo:=AllocMem(1000); <br>if OpenPrinter('HP DJ 200',pprt,nil) then <br>begin <br>a:=1000; <br>if getprinter(pprt,2,pinfo,a,b) then <br>showmessage(pinfo.pPrinterName+' ststus:'+inttostr(pinfo.Status)); <br>ClosePrinter(pprt); <br>end; <br><br>freemem(b); <br>freemem(pinfo); <br>end; <br><br>PRINTER_INFO_2.Status : <br>Windows NT: <br>PRINTER_STATUS_PAUSED <br>PRINTER_STATUS_PENDING_DELETION <br>Windows 95: <br>PRINTER_STATUS_BUSY <br>PRINTER_STATUS_DOOR_OPEN <br>PRINTER_STATUS_ERROR <br>PRINTER_STATUS_INITIALIZING <br>PRINTER_STATUS_IO_ACTIVE <br>PRINTER_STATUS_MANUAL_FEED <br>PRINTER_STATUS_NO_TONER <br>PRINTER_STATUS_NOT_AVAILABLE <br>PRINTER_STATUS_OFFLINE <br>PRINTER_STATUS_OUT_OF_MEMORY <br>PRINTER_STATUS_OUTPUT_BIN_FULL <br>PRINTER_STATUS_PAGE_PUNT <br>PRINTER_STATUS_PAPER_JAM <br>PRINTER_STATUS_PAPER_OUT <br>PRINTER_STATUS_PAPER_PROBLEM <br>PRINTER_STATUS_PAUSED <br>PRINTER_STATUS_PENDING_DELETION <br>PRINTER_STATUS_PRINTING <br>PRINTER_STATUS_PROCESSING <br>PRINTER_STATUS_TONER_LOW <br>PRINTER_STATUS_UNAVAILABLE <br>PRINTER_STATUS_USER_INTERVENTION <br>PRINTER_STATUS_WAITING <br>PRINTER_STATUS_WARMING_UP <br>////////////////////////////////////////////////////////////////// <br>unit PrinterStatus; <br><br>interface <br><br>uses <br>Windows, Messages, WinProcs, SysUtils, Classes, Graphics, Controls, Forms, Dialogs; <br><br><br>// TPrinterstatus <br>// Komponente zum Abfragen des aktuellen Druckerportstatus <br>// Programmiert 2000 von K. Otto <br>// funktioniert unter Win 9x, jedoch nicht unter Win NT <br>// --------------------------------- <br>// Status: Freeware incl. Sourcecode <br>// --------------------------------- <br>// Diese Komponente beruht auf einem Beitrag von Robert Vivrette <br>// f黵 "The unofficial Newsletter of Delphi Users" <br>// http://www.undu.com/articles/990228a.html <br><br>type <br>TPrinterStatus = class(TComponent) <br>private <br>{ Private-Deklarationen } <br>fStatus : Byte; <br>fLPT : Integer; <br>Function GetTimeOut : Boolean; <br>Function GetIOError : Boolean; <br>Function GetPrinterSelected : Boolean; <br>Function GetOutOfPaper : Boolean; <br>Function GetAcknowledgement : Boolean; <br>Function GetPrinterBusy : Boolean; <br>protected <br>{ Protected-Deklarationen } <br>public <br>{ Public-Deklarationen } <br>Procedure CheckPrinterStatus; // Liest den Druckerstatus der angegeben LPT-Schnittstelle <br>Constructor Create(AOwner : TComponent); Override; <br>Property TimeOut : Boolean Read GetTimeOut; <br>Property IOError : Boolean Read GetIOError; <br>Property PrinterSelected : Boolean Read GetPrinterSelected; <br>Property OutOfPaper : Boolean Read GetOutOfPaper; <br>Property Acknowledgement : Boolean Read GetAcknowledgement; <br>Property Busy : Boolean Read GetPrinterBusy; <br>published <br>{ Published-Deklarationen } <br>Property LPT : Integer Read fLPT Write fLPT; <br>end; <br><br>procedure Register; <br><br>implementation <br><br>procedure Register; <br>begin <br>RegisterComponents('Eigene', [TPrinterStatus]); <br>end; <br><br>Function TPrinterStatus.GetTimeOut : Boolean; <br>Begin <br>Result:=(fStatus and $01)=$01; <br>End; <br><br>Function TPrinterStatus.GetIOError : Boolean; <br>Begin <br>Result:=(fStatus and $08)=$08; <br>End; <br><br>Function TPrinterStatus.GetPrinterSelected : Boolean; <br>Begin <br>Result:=(fStatus and $10)=$10; <br>End; <br><br>Function TPrinterStatus.GetOutOfPaper : Boolean; <br>Begin <br>Result:=(fStatus and $20)=$20; <br>End; <br><br>Function TPrinterStatus.GetAcknowledgement : Boolean; <br>Begin <br>Result:=(fStatus and $40)=$40; <br>End; <br><br>Function TPrinterStatus.GetPrinterBusy : Boolean; <br>Begin <br>Result:=not ((fStatus and $80)=$80); <br>End; <br><br>Procedure TPrinterStatus.CheckPrinterStatus; <br>Var <br>Status : Byte; <br>CheckLPT : Word; <br>Begin <br>Status:=0; <br>If (fLPT&gt;=1) and (fLPT&lt;=3) Then <br>Begin <br>CheckLPT:=fLPT-1; <br>asm <br>mov dx,CheckLPT; <br>mov al,0; <br>mov ah,2; <br>int 17h; <br>mov &amp;Status, ah; <br>End; <br>End; <br>fStatus:=Status; <br>End; <br><br>Constructor TPrinterStatus.Create(AOwner : TComponent); <br>Begin <br>Inherited Create(AOwner); <br>fLPT:=1; <br>fStatus:=0; <br>End; <br><br>end. <br>////////////////////////////////////////////////////// <br>用法: <br><br>if not PrinterStatus1.PrinterReady(0) then //0 = current printerport <br>ShowMessage(PrinterStatus1.StatusMsg) else {print print print} ; <br><br>unit PrinterStatus; <br><br>interface <br><br>uses <br>Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs; <br><br>type <br>TPrinterStatus = class(TComponent) <br>private <br>{ Private declarations } <br>FPort : Word; <br>FStatusStr : string; <br>protected <br>{ Protected declarations } <br>public <br>{ Public declarations } <br>function PrinterReady(LPT: Word): boolean; <br>published <br>{ Published declarations } <br>property StatusMsg: string read FStatusStr; <br>end; <br><br>procedure Register; <br><br>implementation <br>uses Printers; <br><br>procedure Register; <br>begin <br>RegisterComponents('Win95', [TPrinterStatus]); <br>end; <br><br>const <br>PrnReady = $90; <br>OffLine = $00; <br>OffLine2 = $10; {NEW LINE} <br>PaperOut = $20; <br>PaperOut2 = $30; {NEW LINE} <br>HookedButOff = $80; {NEW LINE} <br>NoConnect = $B0; {MODIFIED LINE} <br><br>{NOCONNECT = $30 FOR SOME COMPUTERS BY STU} <br><br>function TPrinterStatus.PrinterReady(LPT: Word): boolean; <br>var <br>ErrorCode, C : BYTE; <br>code, x : integer; <br>s : string; <br><br>function GetPrinterStatus (LPT: Word): Byte; <br>{Pass 1 in LPT for LPT1} <br>begin <br>asm <br>mov ah,2 <br>mov dx,LPT <br>dec dx <br>int $17 <br>mov @Result,ah <br>end; <br>end; {GetPrinterStatus} <br><br><br>begin <br>result := false; //assume not <br><br>FPort := LPT; <br>if FPort = 0 then begin {if no port specified then try to set port to current <br>printer port} <br>{printer name} <br>s := Printer.Printers[Printer.PrinterIndex]; <br>if Pos('FPort',s) &lt;&gt; 0 then begin <br>s := Copy(s, Pos('FPort',s) +3, 1); <br>Val(s,x,code); <br>if code &lt;&gt; 0 then FPort := 1 else FPort := x; <br>end else FPort := 1; {default to LPT1} <br>end; <br><br>{valid LPT is 1..4} <br>if (FPort &gt; 4) or (FPort &lt; 1) then begin <br>raise ERangeError.CreateFmt( <br>'LPT%d is not within the valid range of %d..%d', <br>[FPort, 1, 4]); <br>exit; <br>end; <br><br><br>ErrorCode := GetPrinterStatus(FPort); <br><br>ErrorCode := ErrorCode and $B0; {NEW LINE} <br><br>C := ERRORCODE shl 6; {ALWAYS MEANS NOTHING CONNECTED} <br><br>if C &gt; 0 then ERRORCODE := $B0; {ELEMINATES NO LPT3 AND NOTHING CONNECTED} <br><br>case ErrorCode of <br>PrnReady : begin FStatusStr := 'Printer Ready'; result := true; <br>end; <br>NoConnect : FStatusStr := 'Printer not connected'; <br>Offline,OffLine2 : FStatusStr := 'Printer off line'; {Modified} <br>PaperOut,PaperOut2 : FStatusStr := 'Printer out of paper'; {Modified} <br>HookedButOff : FStatusStr := 'Printer connected but turned off'; {New} <br>else <br>FStatusStr := 'Printer error code: ' + IntToStr(ErrorCode); <br>end; <br><br>end; <br><br><br>end. <br><br>
 
hubdog:<br>呵呵,我服了,你哪儿弄了这么多,我还没试验,先把分给了。<br><br>其实,那第一种方法就是我自己用的那法子,却总出不来结果,可能<br>是我什么地方弄错了,或者什么其他原因。至于你提的那汇编的方法,<br>在NT上大概也不行。<br><br>
 
pInfo2: PPrinterInfo2是个什么类型,编译通不过,是不是需要在那利声明一下
 
来自:wrench, 时间:2000-4-27 9:44:00, ID:232443 <br>&nbsp;<br>&nbsp;<br>来自:阿朱, 时间:2000-4-27 13:30:00, ID:232642 <br>&nbsp;<br>&nbsp;<br>来自:y9y, 时间:2000-4-27 21:06:00, ID:233072 <br>&nbsp;<br>&nbsp;<br>来自:y9y, 时间:2000-4-27 23:59:00, ID:233237 <br>&nbsp;<br>&nbsp;<br>来自:y9y, 时间:2000-4-28 0:49:00, ID:233271 <br>&nbsp;<br>&nbsp;<br>来自:y9y, 时间:2000-4-28 20:59:00, ID:233642 <br>&nbsp;<br>&nbsp;<br>来自:歪就歪, 时间:2000-5-3 8:43:00, ID:236890 <br>&nbsp;<br>&nbsp;<br>来自:wrench, 时间:2000-5-3 13:33:00, ID:237076 <br>&nbsp;<br>&nbsp;<br>来自:hubdog, 时间:2000-5-5 8:22:00, ID:238339 <br>&nbsp;<br>&nbsp;<br>来自:歪就歪, 时间:2000-5-5 8:39:00, ID:238351 <br>&nbsp;<br>&nbsp;<br>来自:zhangwq, 时间:2001-9-26 8:13:00, ID:645102 <br>&nbsp;<br>
 
有谁测来吗?<br>给一份给我吗?
 
没有呀,谁测出来了?还有呀,有没有状态对应表呀?
 
我刚才测试了一下第二个方法,结果是这样的,我机器还没有设置打印机,结果检测出来是“打印忙”,怎么回事?
 
想不到一个打印机的状态问题几乎难到所有的“富翁”了<br>我今天花了八个小时,眼睛开始流眼泪了,把所有“打印”相关的分类看完了<br>可惜................!<br>谁来救命啊???????<br>顺便提一下我的问题所在吧,TM210(并口)做小票打印,打三四个小时不打了,W2K提示“打印机无效”,改用直接写并口的方式,一次最多只能打四行,多了它干脆丢掉、也只能工作二三小时就罢工!<br>如何是好?我猜测可能是打印任务累积得太多而让它望而生畏了,所罢工(未经证实)!<br>打算是检测打印机的状态或打印任务数量来控制是否继续分配新的打印任务,可是............我不会,多多指教!本人的分全给你了!
 

Similar threads

后退
顶部