Solved

function to delete the default printer+ delphi

Posted on 2006-06-29
6
462 Views
Last Modified: 2010-04-05
hi there!!!

i urgently need a function in delphi that deletes the default printer. i have a function that resumes the default printer. hope this helps.please help asap

thanks



**********************************************************************
procedure ResumePrinter;
var
  hPrinter : THandle;
  PrinterName : PChar;
  PrinterDefaults : TPrinterDefaultsA;
  DevMode : PDeviceModeA;
  Needed : integer;
  handle:thandle;
  begin
  try
    PrinterName := PChar(Printer.Printers[Printer.PrinterIndex]);
    WinSpool.OpenPrinter(PrinterName,hPrinter,nil);
    Needed := WinSpool.DocumentProperties(Handle,hPrinter,PrinterName,DevMode^,DevMode^,0);
      if Needed < 0 then
        begin
          exit;
        end;
  GetMem(DevMode,Needed);
  WinSpool.DocumentProperties(Handle,hPrinter,PrinterName,DevMode^,DevMode^,DM_COPY);
  PrinterDefaults.pDatatype := nil;
  PrinterDefaults.pDevMode := @DevMode^;
  PrinterDefaults.DesiredAccess := PRINTER_ALL_ACCESS;
  ClosePrinter(hPrinter);
  if OpenPrinter(PrinterName,hPrinter,@PrinterDefaults) then
    begin
      if WinSpool.SetPrinter(hPrinter,0,nil,PRINTER_CONTROL_RESUME) then
        begin
          ClosePrinter(hPrinter);
        end
    end;
  ClosePrinter(hPrinter);
  FreeMem(DevMode);
  except
  on e: exception do HandleException(e, 'ResumePrinter2000');
  end;
  end;
**********************************************************
0
Comment
Question by:nikolaosk
  • 3
  • 3
6 Comments
 
LVL 10

Expert Comment

by:atul_parmar
ID: 17010047
procedure DeletePrinter;
var
  hPrinter : THandle;
  PrinterName : PChar;
  begin
    PrinterName := PChar(Printer.Printers[Printer.PrinterIndex]);
    OpenPrinter(PrinterName,hPrinter,nil);
    DeletePrinter(hPrinter);
  end;
0
 

Author Comment

by:nikolaosk
ID: 17011292

hi there atul,

your solution does not work. i have a dummy printer that i call it "test". i set it as the default printer. then i run your code. it does not work. the printer is not deleted. i use windows xp english version

thanks

nikos
0
 
LVL 10

Accepted Solution

by:
atul_parmar earned 500 total points
ID: 17016044
Nikos, you are right it was due to access permissions. Use the following version.

procedure DeletePrinter;
var
  hPrinter : THandle;
  PrinterName : PChar;
  r : longbool;
  d : PRINTER_DEFAULTS;
begin
  FillChar(d, SizeOf(PRINTER_DEFAULTS), #0);
  d.DesiredAccess := PRINTER_ALL_ACCESS;
  PrinterName := PChar(Printer.Printers[Printer.PrinterIndex]);
  OpenPrinter(PrinterName,hPrinter, @d);
  r := WinSpool.DeletePrinter(hPrinter);
  ClosePrinter(hPrinter);
  if r then
    ShowMessage('Printer deleted successfully');
end;
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:nikolaosk
ID: 17016464
hi there atul.

your code works.
can you change the procedure below to the
procedure DeletePrinter(const printername: WideString);

so i can pass the name of any printer and not just the default i want to delete?

thanks
0
 
LVL 10

Expert Comment

by:atul_parmar
ID: 17016482
Because the openstring requires AnsiString, wide string should not be used at it can be failed when converted to AnsiString.

procedure DeletePrinter(const printername: string);
var
  hPrinter : THandle;
  r : longbool;
  d : PRINTER_DEFAULTS;
begin
  FillChar(d, SizeOf(PRINTER_DEFAULTS), #0);
  d.DesiredAccess := PRINTER_ALL_ACCESS;
  OpenPrinter(PChar(PrinterName),hPrinter, @d);
  r := WinSpool.DeletePrinter(hPrinter);
  ClosePrinter(hPrinter);
  if r then
    ShowMessage('Printer deleted successfully');
end;
0
 

Author Comment

by:nikolaosk
ID: 17017413
thanks it works
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question