Date: Tue, 01 May 2001 15:51:58 +0200 Newsgroups: borland.public.delphi.winapi Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit X-Newsreader: Virtual Access by Atlantic Coast PLC, http://www.atlantic-coast.com/va Organization: TeamB Message-ID: Subject: Re: Printer Status in WinNT From: "Peter Below (TeamB)" <100113.1101@compuXXserve.com> Reply-To: 100113.1101@compuXXserve.com References: <3aed70d9$1_2@dnews> NNTP-Posting-Host: 62.226.187.51 X-Trace: dnews 988724951 62.226.187.51 (1 May 2001 06:49:11 -0700) Lines: 90 Path: dnews Xref: dnews borland.public.delphi.winapi:134077 In article <3aed70d9$1_2@dnews>, Raga wrote: > Is there any way, I can get the Printer staus in WINNT(ie., Out of paper > etc). I understand that Port checking will not work in NT because it uses > Winspool. After you send off a print job you can use EnumJobs to check the fate of the job. When it runs into an error the spooler error status is returned for the job. There seems to be no way to get a reliably indication of the printers status *before* sending off a job. Uses WinSpool, that is where EnumJobs is declared for you. Example for EnumJobs Uses Winspool, printers; {-- GetCurrentPrinterHandle -------------------------------------------} {: Retrieves the handle of the current printer @Returns an API printer handle for the current printer @Desc Uses WinSpool.OpenPrinter to get a printer handle. The caller takes ownership of the handle and must call CloseHandle on it once the handle is no longer needed. Failing to do that creates a serious resource leak!

Requires Printers and WinSpool in the Uses clause. @Raises EWin32Error if the OpenPrinter call fails. }{ Created 30.9.2000 by P. Below -----------------------------------------------------------------------} Function GetCurrentPrinterHandle: THandle; Const Defaults: TPrinterDefaults = ( pDatatype : nil; pDevMode : nil; DesiredAccess : PRINTER_ACCESS_USE or PRINTER_ACCESS_ADMINISTER ); Var Device, Driver, Port : array[0..255] of char; hDeviceMode: THandle; Begin { GetCurrentPrinterHandle } Printer.GetPrinter(Device, Driver, Port, hDeviceMode); If not OpenPrinter(@Device, Result, @Defaults) Then RaiseLastWin32Error; End; { GetCurrentPrinterHandle } Function SafePChar( 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', [SafePChar(pJ^[i].pDocument), pJ^[i].Status, SafePChar(pJ^[i].pStatus)] )); finally ClosePrinter( hPrinter ); end; end; Peter Below (TeamB) 100113.1101@compuserve.com) No e-mail responses, please, unless explicitly requested! Note: I'm unable to visit the newsgroups every day at the moment, so be patient if you don't get a reply immediately.