Solved

function to delete the default printer+ delphi

Posted on 2006-06-29
6
456 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
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

809 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