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]
// ****************************************************************************
// 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]

Share and Enjoy:
  • del.icio.us
  • Fark
  • Reddit
  • Digg
  • DZone
  • email
  • Facebook
  • FriendFeed
  • Google Bookmarks
  • Netvibes
  • Ping.fm
  • Posterous
  • Slashdot
  • StumbleUpon
  • Suggest to Techmeme via Twitter
  • Technorati
  • Tumblr
  • Yahoo! Bookmarks
  • Add to favorites
  • Blogosphere News
  • HackerNews
  • Identi.ca
  • LinkedIn
  • MySpace
  • Print
  • Yahoo! Buzz

Comments

No Comments Yet.

Got something to say?





Who is Brian Layman

I am a WordPress expert living in North East Ohio. I am part of the ever expanding Open Source Internet workforce. I am able to stay at home, with my wife and four home schooled kids, while working as the Senior Developer for b5media - a blogging network that has hosted over 300+

I co-host the NEO WordPress Monthly meetup. I am the board chair of our local church. I host and have provided development services for clients such TV personalities Rhett and Link as well as corporations such as Borland International.

In my spare time I try to sneak out, canoe, mountain bike and camp as often as I can. Sometimes I also defend the earth against zombies and aliens, but usually not during the camping trips.

Services Provided

In providing hosting, email, theme and plugin development to my clients, I function as a single point of contact answering to the needs of their expanding sites.

My service portfolio includes but is not limited to WordPress hosting, optimization, theme development and custom plugin creation. Community creation via vBulletin, Ning and BuddyPress and bbpress

I also am well experienced in site conversion, transition and merges. To clarify this, website technologies change and giving up your data is not an option. I have transitioned literally hundreds of sites from one platform to another.

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