首页  编辑  

打印机状态检测

Tags: /超级猛料/Print.打印/   Date Created:

可用CREATEFILE以PRN为文件名生成一个文件,检测返回值,若成功则打印机可用,否则显示打印机故障。生成关于PRN的文件句柄后,可象普通文件一样向文件写入内容,比如WRITE,写完后关闭文件。

//从并行端口读取打印机状态

function GetPrinterStatus:byte;

asm

 MOV DX,$379;

 IN AL,DX;

end;

//获取打印机是否出错

function CheckPrinter:boolean;

var

 temp:byte;

begin

 temp:=GetPrinterStatus;

 Result:=not ( ((temp and $80)=0) //打印机忙

 or ((temp and $20)<>0)  //打印机缺纸

 or ((temp and $10)=0)    //打印机未联机

 or ((temp and $08)=0) ); //打印机出错;

end;

function GetCurrentPrinterStatus: DWORD;

var

 hPrinter: THandle;

 Device : array[0..255] of char;

 Driver : array[0..255] of char;

 Port   : array[0..255] of char;

 hDeviceMode: THandle;

 bytesNeeded: Cardinal;

 pPI: PPrinterInfo2;

 Defaults: TPrinterDefaults;

begin

 Assert( Printer.PrinterIndex >= 0 );

 Printer.GetPrinter(Device, Driver, Port, hDeviceMode);

 FillChar( Defaults, Sizeof(Defaults), 0 );

 Defaults.DesiredAccess:=

   PRINTER_ACCESS_ADMINISTER or PRINTER_ACCESS_USE;

 Win32Check(WinSpool.OpenPrinter(@Device, hPrinter, @Defaults ));

 try

   WinSpool.GetPrinter(

     hPrinter,

     2,

     Nil, 0, @bytesNeeded );

   GetMem( pPI, bytesNeeded );

   try

     Win32Check(WinSpool.GetPrinter(

       hPrinter, 2,

       pPI, bytesNeeded, @bytesNeeded ));

     Result := pPI^.Status;

   finally

     FreeMem( pPI );

   end;

 finally

   WinSpool.ClosePrinter( hPrinter );

 end;

end;