S
shade
Unregistered / Unconfirmed
GUEST, unregistred user!
我想在一个线程中进行打印,但是一但我在线程中利用Openprinter获取了打印机句柄,
在其他线程或主进程中就不能再调用openprinter了,所有的调用都会失败,返回错误
是非法的参数,但如果线程没有启动就不会有问题。
代码如下:
线程:
procedure TJobthread.Execute;
var
hPrinter : THandle;
iErrorword;
begin
if not WinSpool.OpenPrinter('HP LaserJet 4VC', hPrinter, nil) then
begin
iError:=GetLasterror;
if ERROR_ACCESS_DENIED=iError then
raise exception.Create(' ERROR_ACCESS_DENIED')
else
raise exception.Create(SysErrorMessage(iError));
end;
try
While truedo
begin
//some code use printer such as StartDoc
if Terminated then
Break;
end //while
finally
ClosePrinter(hPrinter);
end;
end;
主进程中:
procedure TfrmMain.Button3Click(Sender: TObject);
var
i:integer;
hPrinter : THandle;
iErrorword;
b:boolean;
sName:string;
begin
if not WinSpool.OpenPrinter('HP LaserJet 4VC', hPrinter, nil) then
begin
iError:=GetLasterror;
if ERROR_ACCESS_DENIED=iError then
raise exception.Create(' ERROR_ACCESS_DENIED')
else
raise exception.Create(SysErrorMessage(iError));
end;
end;
在其他线程或主进程中就不能再调用openprinter了,所有的调用都会失败,返回错误
是非法的参数,但如果线程没有启动就不会有问题。
代码如下:
线程:
procedure TJobthread.Execute;
var
hPrinter : THandle;
iErrorword;
begin
if not WinSpool.OpenPrinter('HP LaserJet 4VC', hPrinter, nil) then
begin
iError:=GetLasterror;
if ERROR_ACCESS_DENIED=iError then
raise exception.Create(' ERROR_ACCESS_DENIED')
else
raise exception.Create(SysErrorMessage(iError));
end;
try
While truedo
begin
//some code use printer such as StartDoc
if Terminated then
Break;
end //while
finally
ClosePrinter(hPrinter);
end;
end;
主进程中:
procedure TfrmMain.Button3Click(Sender: TObject);
var
i:integer;
hPrinter : THandle;
iErrorword;
b:boolean;
sName:string;
begin
if not WinSpool.OpenPrinter('HP LaserJet 4VC', hPrinter, nil) then
begin
iError:=GetLasterror;
if ERROR_ACCESS_DENIED=iError then
raise exception.Create(' ERROR_ACCESS_DENIED')
else
raise exception.Create(SysErrorMessage(iError));
end;
end;