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

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]
// ****************************************************************************
// u_PrinterManipulation.pas 12/Dec/2006
// Written by Brian Layman
// Visit him at http://www.TheCodeCave.com
//
// A quick program to demonstrate the deletion of a printer.
//
// Usage: PrinterManipulation.exe Just click on the button 1 to populate and
// select a printer off of the list. Use button2 or button3 for two
// different deletion methods. Double clicking the list is the same as
// clicking button3.
//
// History:
// 04/Dec/2006 – BL – Created
//
// ****************************************************************************
interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
ListBox1: TListBox;
Button2: TButton;
Label1: TLabel;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure ListBox1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end; // TForm1

var
Form1: TForm1;

implementation

{$R *.DFM}
uses
WinSpool, Printers;

type
{******************************************************************************
TPrinterDevice
******************************************************************************}
TPrinterDevice = class
Driver, Device, Port: String;
constructor Create(ADriver, ADevice, APort: PChar);
function IsEqual(ADriver, ADevice, APort: PChar): Boolean;
end; // TPrinterDevice

{******************************************************************************
Create
******************************************************************************}
constructor TPrinterDevice.Create(ADriver, ADevice, APort: PChar);
begin // Create
inherited Create;
Driver := ADriver;
Device := ADevice;
Port := APort;
end; // Create

{******************************************************************************
IsEqual
******************************************************************************}
function TPrinterDevice.IsEqual(ADriver, ADevice, APort: PChar): Boolean;
begin // IsEqual
Result := (Device = ADevice) and ((Port = ”) or (Port = APort));
end; // IsEqual

{******************************************************************************
TForm1
******************************************************************************}
{******************************************************************************
Button1Click
******************************************************************************}
procedure TForm1.Button1Click(Sender: TObject);
var
indx: Integer;
begin // Button1Click
ListBox1.Clear;
for indx := 0 to (Printer.Printers.Count – 1)
do ListBox1.Items.Add(Printer.Printers[indx]);
end; // Button1Click

{******************************************************************************
ListBox1Click
******************************************************************************}
procedure TForm1.ListBox1Click(Sender: TObject);
var
hPrinter : Cardinal;
PrinterDef: PRINTER_DEFAULTS;
pPrinterName: PChar;
begin // ListBox1Click
pPrinterName := PChar(ListBox1.Items[ListBox1.Itemindex]);
FillChar(PrinterDef, sizeof(PrinterDef), #0);
PrinterDef.DesiredAccess := PRINTER_ALL_ACCESS;

if (OpenPrinter(pPrinterName, hPrinter, @PrinterDef))
then begin
if DeletePrinter(hPrinter)
then ShowMessage(‘Deletion Succcesful’);
ClosePrinter(hPrinter);
end
else ShowMessage(‘Could not open’);
end; // ListBox1Click

{******************************************************************************
Button2Click
******************************************************************************}
procedure TForm1.Button2Click(Sender: TObject);
var
ADevice: PChar;
PrinterHandle: THandle;
begin // Button2Click
with TPrinterDevice(Printer.Printers.Objects[ListBox1.ItemIndex])
do begin
GetMem(ADevice, Length(Device));
StrCopy(ADevice, PChar(Device));
end;
OpenPrinter(ADevice, PrinterHandle, nil);
if DeletePrinter(PrinterHandle) then ShowMessage(‘Deletion Succcesful’);
ClosePrinter(PrinterHandle);
FreeMem(ADevice);
end; // Button2Click

end.
[/delphi]

Add a Comment

Your email address will not be published. Required fields are marked *