How to delete a printer from a command line or from Delphi

December 4, 2006

The command line for deleting a printer looks like this:

rundll32 printui.dll PrintUIEntry /dn /n\\machine\printer

I could not get that to work for my current situation. So I wrote a Delphi program that did the same.

You can down load it here.
PrinterManipulation.zip

You can use this next source code clip instead of downloading if you like. It assumes there are three buttons on the page and one listbox on a form named Form1. After creating those controls, select all of the pascal source text AFTER the “unit” line and paste this source code over top of it. After that, double click on all of the buttons, compile and you’ll have a peice of running code.

DELPHI:
  1. // ****************************************************************************
  2. //  u_PrinterManipulation.pas                                      12/Dec/2006
  3. //  Written by Brian Layman
  4. //  Visit him at http://www.TheCodeCave.com
  5. //
  6. //  A quick program to demonstrate the deletion of a printer.
  7. //
  8. //  Usage: PrinterManipulation.exe Just click on the button 1 to populate and
  9. //  select a printer off of the list.  Use button2 or button3 for two
  10. //  different deletion methods.  Double clicking the list is the same as
  11. //  clicking button3.
  12. //
  13. //  History:
  14. //     04/Dec/2006 – BL – Created
  15. //
  16. // ****************************************************************************
  17. interface
  18.  
  19. uses
  20.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  21.   StdCtrls;
  22.  
  23. type
  24.   TForm1 = class(TForm)
  25.     Button1: TButton;
  26.     ListBox1: TListBox;
  27.     Button2: TButton;
  28.     Label1: TLabel;
  29.     Button3: TButton;
  30.     procedure Button1Click(Sender: TObject);
  31.     procedure Button2Click(Sender: TObject);
  32.     procedure ListBox1Click(Sender: TObject);
  33.   private
  34.     { Private declarations }
  35.   public
  36.     { Public declarations }
  37.   end// TForm1
  38.  
  39. var
  40.   Form1: TForm1;
  41.  
  42. implementation
  43.  
  44. {$R *.DFM}
  45. uses
  46. WinSpool, Printers;
  47.  
  48. type
  49. {******************************************************************************
  50.   TPrinterDevice
  51. ******************************************************************************}
  52.   TPrinterDevice = class
  53.     Driver, Device, Port: String;
  54.     constructor Create(ADriver, ADevice, APort: PChar);
  55.     function IsEqual(ADriver, ADevice, APort: PChar): Boolean;
  56.   end// TPrinterDevice
  57.  
  58. {******************************************************************************
  59.   Create
  60. ******************************************************************************}
  61. constructor TPrinterDevice.Create(ADriver, ADevice, APort: PChar);
  62. begin // Create
  63.   inherited Create;
  64.   Driver := ADriver;
  65.   Device := ADevice;
  66.   Port := APort;
  67. end// Create
  68.  
  69. {******************************************************************************
  70.   IsEqual
  71. ******************************************************************************}
  72. function TPrinterDevice.IsEqual(ADriver, ADevice, APort: PChar): Boolean;
  73. begin // IsEqual
  74.   Result := (Device = ADevice) and ((Port = ) or (Port = APort));
  75. end// IsEqual
  76.  
  77. {******************************************************************************
  78.   TForm1
  79. ******************************************************************************}
  80. {******************************************************************************
  81.   Button1Click
  82. ******************************************************************************}
  83. procedure TForm1.Button1Click(Sender: TObject);
  84. var
  85.   indx: Integer;
  86. begin // Button1Click
  87.   ListBox1.Clear;
  88.   for indx := 0 to (Printer.Printers.Count1)
  89.   do ListBox1.Items.Add(Printer.Printers[indx]);
  90. end// Button1Click
  91.  
  92. {******************************************************************************
  93.   ListBox1Click
  94. ******************************************************************************}
  95. procedure TForm1.ListBox1Click(Sender: TObject);
  96. var
  97.   hPrinter  : Cardinal;
  98.   PrinterDef: PRINTER_DEFAULTS;
  99.   pPrinterName: PChar;
  100. begin // ListBox1Click
  101.   pPrinterName := PChar(ListBox1.Items[ListBox1.Itemindex]);
  102.   FillChar(PrinterDef, sizeof(PrinterDef), #0);
  103.   PrinterDef.DesiredAccess := PRINTER_ALL_ACCESS;
  104.  
  105.   if (OpenPrinter(pPrinterName, hPrinter, @PrinterDef))
  106.   then begin
  107.     if DeletePrinter(hPrinter)
  108.     then ShowMessage(‘Deletion Succcesful’);
  109.     ClosePrinter(hPrinter);
  110.   end
  111.   else ShowMessage(‘Could not open’);
  112. end// ListBox1Click
  113.  
  114. {******************************************************************************
  115.   Button2Click
  116. ******************************************************************************}
  117. procedure TForm1.Button2Click(Sender: TObject);
  118. var
  119.   ADevice: PChar;
  120.   PrinterHandle: THandle;
  121. begin // Button2Click
  122.   with TPrinterDevice(Printer.Printers.Objects[ListBox1.ItemIndex])
  123.   do begin
  124.     GetMem(ADevice, Length(Device));
  125.     StrCopy(ADevice, PChar(Device));
  126.   end;
  127.   OpenPrinter(ADevice, PrinterHandle, nil);
  128.   if DeletePrinter(PrinterHandle) then ShowMessage(‘Deletion Succcesful’);
  129.   ClosePrinter(PrinterHandle);
  130.   FreeMem(ADevice);
  131. end// Button2Click
  132.  
  133. end.

Comments

No Comments Yet.

Got something to say?





Cave Drips...

  • So the question is "Is your site at Something.wordpress.com or do you have your own URL?" If they pay for a custom URL, they will know that. 1 day ago
  • Rotating icepack and heating pad. Stupid Porch. Think I'll finally hit the doc up about my knee... #fb 1 day ago
  • More updates...

viagra 50 mg indian version of viagra cialis cheapest viagra india online viagra cost comparison viagra for sale without prescription generic tadalafil online buy viagra in korea indian levitra discount cialis online viagra prescription over the counter vardenafil cialis otc cialis no rx cialis 30 mg viagra ranbaxy buy levitra in uk cialis low price tadalafil tablets 10mg cheap viagra fast shipping cheap generic levitra cialis discount cialis 5mg viagra discount prices buy levitra without prescription vardenafil online generic levitra canada viagra professional price cheapest sildenafil citrate indian version of cialis viagra lowest price viagra online prescriptions tadalafil 10mg levitra over the counter levitra prescriptions online buy viagra without a prescription liquid tadalafil citrate buy viagra prescription online tadalafil 20mg india india viagra generic sildenafil citrate for sale vardenafil hcl 10mg cialis discount coupon buy levitra australia viagra over the counter in canada liquid sildenafil tadalafil price comparison viagra cost in india cialis mail order sildenafil sales buy vardenafil cialis offer cheap vardenafil generic cialis no prescription viagra tabs generic indian names viagra price canada vardenafil hcl 20 mg generic viagra without prescription viagra by scilla biotechnologies buy generic cialis free viagra viagra over the counter viagra pills kamagra 100 mg cialis from india tadalafil australia tadalafil 20mg tablets tadalafil soft tabs sildenafil pills viagra no prescription required generic viagra paypal tadalafil online indian viagra cost tadalafil online pharmacy generic soft viagra sildenafil soft tablets viagra generic names buy viagra in ireland levitra without prescription levitra online purchase cialis pill indian tadalafil levitra 5mg cialis cost per pill tadalafil oral jelly sildenafil no prescription vardenafil price generic cialis 10mg cheap cialis no prescription order sildenafil citrate indian generic viagra blue viagra buy cialis usa apcalis 20mg tablets viagra overnight delivery sildenafil india purchase viagra without a prescription viagra prescriptions order viagra without prescription viagra with no prescription levitra for sale purchase viagra canada discount levitra viagra 200mg cheap viagra 100mg cialis overnight delivery buy sildenafil online viagra made in india cialis tabs 10mg viagra indian pharmacy viagra for sale in ireland viagra uk prices buy viagra in europe generic cialis india levitra online viagra for sale india buy viagra in dublin generic cialis soft tabs viagra 50mg cost generic sildenafil 100mg tadalafil generic viagra super active 100 mg kamagra 100mg sildenafil 100 mg tablets cialis no prescription viagra low price online cialis suhagra tablets buy cialis daily use tadalafil sample cialis prices viagra prescription online buy cialis pill kamagra from india cialis online levitra mg vigora india vardenafil 10 mg sildenafil citrate 100mg buy viagra in india buy cialis professional viagra in india buy viagra in singapore generic revatio viagra substitutes sildenafil canada viagra no script cheap kamagra viagra retail price cheap lovegra order viagra uk buy cialis in mexico viagra prescription price purchase cialis online without prescription online cialis prescription ranbaxy caverta buy viagra in hong kong sildenafil price cialis mastercard buy viagra in england viagra mail order canada cialis tablets for sale order cialis cialis soft tabs generic levitra india tadalafil prices cheap sildenafil citrate tablets cialis online prescriptions cialis 5 mg daily levitra prices prescriptions viagra viagra over the counter alternative cialis 20 mg tablets cialis generic india cialis prescribing cialis 20mg daily sildenafil 50 mg viagra drug prices tadalafil generic india cialis sale viagra prices buy viagra 50 mg levitra pharmacy buy viagra generic viagra prescription drug cialis daily cost vardenafil uk viagra soft tabs online buy viagra super active cialis 10mg price 25mg viagra silagra 100mg online viagra prescriptions cialis prescription cheap cialis india revatio 20 mg indian equivalent of viagra tadalafil india viagra capsules cheapest viagra buy cialis without prescription tadalafil overnight cheap tadalafil online purchase viagra online no prescription