0

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?

Hein du Plessis
  • 3,305
  • 6
  • 34
  • 51
  • Isn't that for the users of your app to deside? The print jobs áre requested. Check on app startup whether the queue exists instead. – NGLN Dec 11 '13 at 07:26
  • @NGLN I do check on start up, see a duplicate, but have no way to delete it currently. My application is the only user of the specific queue. Users do not want to see multiple copies of the same queue. it also creates a licensing problem. – Hein du Plessis Dec 11 '13 at 08:18
  • Instead of automatically creating a print queue, why not check for an existing one first and use that instead? – Andy_D Dec 11 '13 at 10:55

1 Answers1

2

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 } 
Glen Morse
  • 2,437
  • 8
  • 51
  • 102