How can I delete a print queue in Delphi

2.2k views Asked by At

I am using a PDF control suite, that creates a print queue automatically on start up, but occasionally, if the app is terminated abnormally, the queue is not removed, so on the next start up, it creates a duplicate.

I can check for the print queues to find it, using the Printers list, but I can't see how to delete a specific queue?

1

There are 1 answers

0
Glen Morse On

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 ClosePrinter 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.

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 }

    {: Kill all pending jobs on the current printer }
    Procedure PurgeJobsOnCurrentPrinter;
      Var
        hPrinter: THandle;
      Begin
        hPrinter:= GetCurrentPrinterHandle;
        try
          If not WinSpool.SetPrinter( hPrinter, 0, nil,
    PRINTER_CONTROL_PURGE )
          Then
            RaiseLastWin32Error;
        finally
          ClosePrinter( hPrinter );
        end;
      End; { PurgeJobsOnCurrentPrinter }