filewrite函数如果用做向并口打印机发要打印的信息,如何判断打印机是否安装?(100分)

  • 主题发起人 主题发起人 TonyStar
  • 开始时间 开始时间
T

TonyStar

Unregistered / Unconfirmed
GUEST, unregistred user!
filewrite函数如果用做向并口打印机发要打印的信息,如果打印机没有安装,
程序便会死掉,也就是在执行到filewrite函数时就永久等待了。怎样才能避免
出现这样的情况?能事先判断打印机是否安装吗?
 
ComPort 是一个票据打印机的控件,很好用的,你可以去找一找
try
Comprot.Active := True;
except
ShowMessage('没连接打印机!');
end;
 
来自Hubdog的葵花宝典。
Add a message handler for it to your main form, private section:

procedure WMSpoolerstatus( var msg: TWMSpoolerstatus );
message WM_SPOOLERSTATUS;

Hit Shift-Ctrl-C to make the IDE add a implementation for the method.
Add an inherited statement. msg.JobStatus will be 0 if the spooler thinks
it is healthy and < 0 if it has an error. From the list of error codes
found in Windows.PAS (above PR_JOBSTATUS) a missing network printer may not
be seen as an error, so look at msg.JobsLeft. If this never decreases in
the messages you see the spooler cannot get rid of the jobs. Of course it is
hard to tell when enough time has passed so you might become suspicious.

You can try to use the WinSpool.EnumJobs API to query the spooler directly.
Example below. See the diverse JOB_STATUS* codes given in the help topic for
JOB_INFO_1 in win32.hlp. Note that the Status field is a bitset, it can contain
more than one of the status codes. Use expressions like

if (Status and JOB_STATUS_OFFLINE) <> 0 then

... printer offline

uses Winspool, Printers;

function GetCurrentPrinterHandle: THandle;
var
Device, Driver, Port : array[0..255] of char;
hDeviceMode: THandle;
begin

Printer.GetPrinter(Device, Driver, Port, hDeviceMode);
if not OpenPrinter(@Device, Result, nil) then

RaiseLastWin32Error;
end;


Function SavePChar( p: PChar ): PChar;
const error: PChar = 'Nil';
begin

if not assigned( p ) then

result := error
else

result := p;
end;


procedure TForm1.Button2Click(Sender: TObject);
type
TJobs = Array [0..1000] of JOB_INFO_1;
PJobs = ^TJobs;
var
hPrinter : THandle;
bytesNeeded, numJobs, i: Cardinal;
pJ: PJobs;
begin

hPrinter:= GetCurrentPrinterHandle;
try
EnumJobs( hPrinter, 0, 1000, 1, Nil, 0, bytesNeeded,
numJobs );
pJ := AllocMem( bytesNeeded );
If not EnumJobs( hPrinter, 0, 1000, 1, pJ, bytesNeeded,
bytesNeeded, numJobs )
then

RaiseLastWin32Error;

memo1.clear;
if numJobs = 0 then

memo1.lines.add('No jobs in queue')
else

For i:= 0 to Pred(numJobs)do

memo1.lines.add( Format(
'Job %s, Status (%d): %s',
[SavePChar(pJ^.pDocument), pJ^.Status, SavePChar(pJ^.pStatus)] ));
finally
ClosePrinter( hPrinter );
end;

end;

 
var fdevice:pchar;
fdriver:pchar;
fport:pchar;
fhandle:thandle;
currentprintname:string;
begin

getmem(fdevice,255);
getmem(fdrive,255);
getmem(fport,255);
printer.getprinter(fdevice,fdriver,fport,fhandle);
currentprintname;fdevice;
if fdevice<>nil then
freemem(fdevice,255);
if fderiver<>nil then
freemem(fderiver,255);
if fport<>nil then
freemem(fport,255);
if currentprintname='' then

showmessage('你还未安装打印机');



 
to all:
我的打印机驱动是自己做的,所以不需要在WINDOWS里安装打印机。当然也就不能用Printer
的方法去做。而不管有没有打印机,我现在是想解决的是 用写文件的方法直接向并口发数据
如果并口没有连接打印机会永久等待的问题。
 
那就不好办了,自己编程查询,同时硬件也需要相应。
类似Windows的查找新硬件的方法吧。
我没有好的思路了。
 
谢了。
还有人知道吗?
 
有人知道吗?
 
你的打印机是自己做的嘛?(不是开玩笑,微打就有很多自己做的,买打印头就可以了,通信协议自定)
 
我用的是票据打印机,它们的驱动程序大部分都是我根据它们的指令集自己做的。
其实我的问题可以说都跟打印机没什么关系了,只需要解决向未接设备的并口发
数据而程序不死的问题就行了。 真的就没有人知道了吗?
 
我也想解决,而且就算我连打印机它也一样死机,只打一行就死了,怎么办?[?]
 

Similar threads

S
回复
0
查看
1K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
900
SUNSTONE的Delphi笔记
S
D
回复
0
查看
867
DelphiTeacher的专栏
D
D
回复
0
查看
836
DelphiTeacher的专栏
D
后退
顶部