Author: Jonas Bilinkevicius
I can stop and delete current printing jobs from the "Printer Manager". Can I do it
in my code, too? I want to stop all print jobs and delete them in my program.
Answer:
Try the PurgeJobsOnCurrentPrinter procedure given below. Not tested!
1 uses2 winspool, printers;
3 4 {GetCurrentPrinterHandle:5 Retrieves the handle of the current printer and returns an API printer handle for 6 the7 current printer. Uses WinSpool.OpenPrinter to get a printer handle. The caller takes8 ownership of the handle and must call ClosePrinter on it once the handle is no 9 longer10 needed. Failing to do that creates a serious resource leak! Raises EWin32Error if 11 the12 OpenPrinter call fails.}13 14 function GetCurrentPrinterHandle: THandle;
15 const16 Defaults: TPrinterDefaults = (pDatatype: nil; pDevMode: nil; DesiredAccess:
17 PRINTER_ACCESS_USE or PRINTER_ACCESS_ADMINISTER);
18 var19 Device, Driver, Port: array[0..255] of char;
20 hDeviceMode: THandle;
21 begin22 Printer.GetPrinter(Device, Driver, Port, hDeviceMode);
23 ifnot OpenPrinter(@Device, Result, @Defaults) then24 RaiseLastWin32Error;
25 end;
26 27 {Kill all pending jobs on the current printer}28 29 procedure PurgeJobsOnCurrentPrinter;
30 var31 hPrinter: THandle;
32 begin33 hPrinter := GetCurrentPrinterHandle;
34 try35 ifnot WinSpool.SetPrinter(hPrinter, 0, nil, PRINTER_CONTROL_PURGE) then36 RaiseLastWin32Error;
37 finally38 ClosePrinter(hPrinter);
39 end;
40 end;