大部分项目中有需要检测打印机的,那么我们用COM来实现:
(根据Delphi下的COM编程技术 - 1)
代码如下:
unit UprintMain; {$WARN SYMBOL_PLATFORM OFF} interface uses Windows, ActiveX, Classes, ComObj; type Iprinter= interface ['{14EC88EC-9F6F-449F-BEAC-3A3BF2837A58}']//这个ID是接口Iprinter//IID //function list function checkPrinterStatus(LPTPort:word):Byte;stdcall;//safecall end; TCprintSupport = class(TComObject,Iprinter) protected //Declare ICalculator methods here function checkPrinterStatus(LPTPort:word):Byte;stdcall;//safecall function testFunc(inputVal:Integer):integer;stdcall; //safecall end; const Class_CprintSupport: TGUID = '{6D380B4D-1B5C-4246-8587-F64C59158963}';//GUID implementation uses ComServ; function TCprintSupport.checkPrinterStatus(LPTPort:word):Byte; var statusLPT:Byte; checkLPT:Word; begin statusLPT:= 0; try if(LPTPort>=1)and(LPTPort<=3)then begin checkLPT:=LPTPort-1; asm mov dx,checkLPT; mov al,0; mov ah,2; int 17H; mov &statusLPT,ah; end; end; except Result:=0; end; Result:=statusLPT; end; initialization //初始化建立唯一的guid值的com对象 TComObjectFactory.Create(ComServer, TCprintSupport, Class_CprintSupport, 'CprintSupport', 'printer support plate01', ciMultiInstance, tmApartment); end.
主代码为端口检测:
asm
mov dx,checkLPT;
mov al,0;
mov ah,2;
int 17H;
mov &statusLPT,ah;
end;
工作原理:调用中断INT 17H,读取打印机的状态字节加以分析,即可得到其当前状态!
返回值有以下几种:
01H:超时
08H:I/O错误
10H:打印机未联机
20H:打印机缺纸
80H:打印机空